Color.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Assertions.h>
  8. #include <AK/Format.h>
  9. #include <AK/Forward.h>
  10. #include <AK/SIMD.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <LibIPC/Forward.h>
  13. #include <math.h>
  14. namespace Gfx {
  15. enum class ColorRole;
  16. typedef u32 ARGB32;
  17. struct HSV {
  18. double hue { 0 };
  19. double saturation { 0 };
  20. double value { 0 };
  21. };
  22. struct YUV {
  23. float y { 0 };
  24. float u { 0 };
  25. float v { 0 };
  26. };
  27. class Color {
  28. public:
  29. enum NamedColor {
  30. Transparent,
  31. Black,
  32. White,
  33. Red,
  34. Green,
  35. Cyan,
  36. Blue,
  37. Yellow,
  38. Magenta,
  39. DarkGray,
  40. MidGray,
  41. LightGray,
  42. WarmGray,
  43. DarkCyan,
  44. DarkGreen,
  45. DarkBlue,
  46. DarkRed,
  47. MidCyan,
  48. MidGreen,
  49. MidRed,
  50. MidBlue,
  51. MidMagenta,
  52. };
  53. constexpr Color() = default;
  54. constexpr Color(NamedColor);
  55. constexpr Color(u8 r, u8 g, u8 b)
  56. : m_value(0xff000000 | (r << 16) | (g << 8) | b)
  57. {
  58. }
  59. constexpr Color(u8 r, u8 g, u8 b, u8 a)
  60. : m_value((a << 24) | (r << 16) | (g << 8) | b)
  61. {
  62. }
  63. static constexpr Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
  64. static constexpr Color from_argb(unsigned argb) { return Color(argb); }
  65. static constexpr Color from_cmyk(float c, float m, float y, float k)
  66. {
  67. auto r = static_cast<u8>(255.0f * (1.0f - c) * (1.0f - k));
  68. auto g = static_cast<u8>(255.0f * (1.0f - m) * (1.0f - k));
  69. auto b = static_cast<u8>(255.0f * (1.0f - y) * (1.0f - k));
  70. return Color(r, g, b);
  71. }
  72. static constexpr Color from_yuv(YUV const& yuv) { return from_yuv(yuv.y, yuv.u, yuv.v); }
  73. static constexpr Color from_yuv(float y, float u, float v)
  74. {
  75. // https://www.itu.int/rec/R-REC-BT.1700-0-200502-I/en Table 4, Items 8 and 9 arithmetically inverted
  76. float r = y + v / 0.877f;
  77. float b = y + u / 0.493f;
  78. float g = (y - 0.299f * r - 0.114f * b) / 0.587f;
  79. r = clamp(r, 0.0f, 1.0f);
  80. g = clamp(g, 0.0f, 1.0f);
  81. b = clamp(b, 0.0f, 1.0f);
  82. return { static_cast<u8>(floorf(r * 255.0f)), static_cast<u8>(floorf(g * 255.0f)), static_cast<u8>(floorf(b * 255.0f)) };
  83. }
  84. // https://www.itu.int/rec/R-REC-BT.1700-0-200502-I/en Table 4
  85. constexpr YUV to_yuv() const
  86. {
  87. float r = red() / 255.0f;
  88. float g = green() / 255.0f;
  89. float b = blue() / 255.0f;
  90. // Item 8
  91. float y = 0.299f * r + 0.587f * g + 0.114f * b;
  92. // Item 9
  93. float u = 0.493f * (b - y);
  94. float v = 0.877f * (r - y);
  95. y = clamp(y, 0.0f, 1.0f);
  96. u = clamp(u, -1.0f, 1.0f);
  97. v = clamp(v, -1.0f, 1.0f);
  98. return { y, u, v };
  99. }
  100. static constexpr Color from_hsl(float h_degrees, float s, float l) { return from_hsla(h_degrees, s, l, 1.0); }
  101. static constexpr Color from_hsla(float h_degrees, float s, float l, float a)
  102. {
  103. // Algorithm from https://www.w3.org/TR/css-color-3/#hsl-color
  104. float h = clamp(h_degrees / 360.0f, 0.0f, 1.0f);
  105. s = clamp(s, 0.0f, 1.0f);
  106. l = clamp(l, 0.0f, 1.0f);
  107. a = clamp(a, 0.0f, 1.0f);
  108. // HOW TO RETURN hue.to.rgb(m1, m2, h):
  109. auto hue_to_rgb = [](float m1, float m2, float h) -> float {
  110. // IF h<0: PUT h+1 IN h
  111. if (h < 0.0f)
  112. h = h + 1.0f;
  113. // IF h>1: PUT h-1 IN h
  114. if (h > 1.0f)
  115. h = h - 1.0f;
  116. // IF h*6<1: RETURN m1+(m2-m1)*h*6
  117. if (h * 6.0f < 1.0f)
  118. return m1 + (m2 - m1) * h * 6.0f;
  119. // IF h*2<1: RETURN m2
  120. if (h * 2.0f < 1.0f)
  121. return m2;
  122. // IF h*3<2: RETURN m1+(m2-m1)*(2/3-h)*6
  123. if (h * 3.0f < 2.0f)
  124. return m1 + (m2 - m1) * (2.0f / 3.0f - h) * 6.0f;
  125. // RETURN m1
  126. return m1;
  127. };
  128. // SELECT:
  129. // l<=0.5: PUT l*(s+1) IN m2
  130. float m2;
  131. if (l <= 0.5f)
  132. m2 = l * (s + 1.0f);
  133. // ELSE: PUT l+s-l*s IN m2
  134. else
  135. m2 = l + s - l * s;
  136. // PUT l*2-m2 IN m1
  137. float m1 = l * 2.0f - m2;
  138. // PUT hue.to.rgb(m1, m2, h+1/3) IN r
  139. float r = hue_to_rgb(m1, m2, h + 1.0f / 3.0f);
  140. // PUT hue.to.rgb(m1, m2, h ) IN g
  141. float g = hue_to_rgb(m1, m2, h);
  142. // PUT hue.to.rgb(m1, m2, h-1/3) IN b
  143. float b = hue_to_rgb(m1, m2, h - 1.0f / 3.0f);
  144. // RETURN (r, g, b)
  145. u8 r_u8 = clamp(lroundf(r * 255.0f), 0, 255);
  146. u8 g_u8 = clamp(lroundf(g * 255.0f), 0, 255);
  147. u8 b_u8 = clamp(lroundf(b * 255.0f), 0, 255);
  148. u8 a_u8 = clamp(lroundf(a * 255.0f), 0, 255);
  149. return Color(r_u8, g_u8, b_u8, a_u8);
  150. }
  151. constexpr u8 red() const { return (m_value >> 16) & 0xff; }
  152. constexpr u8 green() const { return (m_value >> 8) & 0xff; }
  153. constexpr u8 blue() const { return m_value & 0xff; }
  154. constexpr u8 alpha() const { return (m_value >> 24) & 0xff; }
  155. constexpr void set_alpha(u8 value)
  156. {
  157. m_value &= 0x00ffffff;
  158. m_value |= value << 24;
  159. }
  160. constexpr void set_red(u8 value)
  161. {
  162. m_value &= 0xff00ffff;
  163. m_value |= value << 16;
  164. }
  165. constexpr void set_green(u8 value)
  166. {
  167. m_value &= 0xffff00ff;
  168. m_value |= value << 8;
  169. }
  170. constexpr void set_blue(u8 value)
  171. {
  172. m_value &= 0xffffff00;
  173. m_value |= value;
  174. }
  175. constexpr Color with_alpha(u8 alpha) const
  176. {
  177. return Color((m_value & 0x00ffffff) | alpha << 24);
  178. }
  179. constexpr Color blend(Color source) const
  180. {
  181. if (!alpha() || source.alpha() == 255)
  182. return source;
  183. if (!source.alpha())
  184. return *this;
  185. #ifdef __SSE__
  186. using AK::SIMD::i32x4;
  187. const i32x4 color = {
  188. red(),
  189. green(),
  190. blue()
  191. };
  192. const i32x4 source_color = {
  193. source.red(),
  194. source.green(),
  195. source.blue()
  196. };
  197. int const d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
  198. const i32x4 out = (color * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source_color) / d;
  199. return Color(out[0], out[1], out[2], d / 255);
  200. #else
  201. int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
  202. u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
  203. u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
  204. u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
  205. u8 a = d / 255;
  206. return Color(r, g, b, a);
  207. #endif
  208. }
  209. Color mixed_with(Color const& other, float weight) const;
  210. Color interpolate(Color const& other, float weight) const noexcept
  211. {
  212. u8 r = red() + round_to<u8>(static_cast<float>(other.red() - red()) * weight);
  213. u8 g = green() + round_to<u8>(static_cast<float>(other.green() - green()) * weight);
  214. u8 b = blue() + round_to<u8>(static_cast<float>(other.blue() - blue()) * weight);
  215. u8 a = alpha() + round_to<u8>(static_cast<float>(other.alpha() - alpha()) * weight);
  216. return Color(r, g, b, a);
  217. }
  218. constexpr Color multiply(Color const& other) const
  219. {
  220. return Color(
  221. red() * other.red() / 255,
  222. green() * other.green() / 255,
  223. blue() * other.blue() / 255,
  224. alpha() * other.alpha() / 255);
  225. }
  226. constexpr float distance_squared_to(Color const& other) const
  227. {
  228. int a = other.red() - red();
  229. int b = other.green() - green();
  230. int c = other.blue() - blue();
  231. int d = other.alpha() - alpha();
  232. return (a * a + b * b + c * c + d * d) / (4.0f * 255.0f * 255.0f);
  233. }
  234. constexpr u8 luminosity() const
  235. {
  236. return (red() * 0.2126f + green() * 0.7152f + blue() * 0.0722f);
  237. }
  238. constexpr float contrast_ratio(Color const& other)
  239. {
  240. auto l1 = luminosity();
  241. auto l2 = other.luminosity();
  242. auto darkest = min(l1, l2) / 255.;
  243. auto brightest = max(l1, l2) / 255.;
  244. return (brightest + 0.05) / (darkest + 0.05);
  245. }
  246. constexpr Color to_grayscale() const
  247. {
  248. auto gray = luminosity();
  249. return Color(gray, gray, gray, alpha());
  250. }
  251. constexpr Color sepia(float amount = 1.0f) const
  252. {
  253. auto blend_factor = 1.0f - amount;
  254. auto r1 = 0.393f + 0.607f * blend_factor;
  255. auto r2 = 0.769f - 0.769f * blend_factor;
  256. auto r3 = 0.189f - 0.189f * blend_factor;
  257. auto g1 = 0.349f - 0.349f * blend_factor;
  258. auto g2 = 0.686f + 0.314f * blend_factor;
  259. auto g3 = 0.168f - 0.168f * blend_factor;
  260. auto b1 = 0.272f - 0.272f * blend_factor;
  261. auto b2 = 0.534f - 0.534f * blend_factor;
  262. auto b3 = 0.131f + 0.869f * blend_factor;
  263. auto r = red();
  264. auto g = green();
  265. auto b = blue();
  266. return Color(
  267. clamp(lroundf(r * r1 + g * r2 + b * r3), 0, 255),
  268. clamp(lroundf(r * g1 + g * g2 + b * g3), 0, 255),
  269. clamp(lroundf(r * b1 + g * b2 + b * b3), 0, 255),
  270. alpha());
  271. }
  272. constexpr Color darkened(float amount = 0.5f) const
  273. {
  274. return Color(red() * amount, green() * amount, blue() * amount, alpha());
  275. }
  276. constexpr Color lightened(float amount = 1.2f) const
  277. {
  278. return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
  279. }
  280. Vector<Color> shades(u32 steps, float max = 1.f) const;
  281. Vector<Color> tints(u32 steps, float max = 1.f) const;
  282. constexpr Color saturated_to(float saturation) const
  283. {
  284. auto hsv = to_hsv();
  285. auto alpha = this->alpha();
  286. auto color = Color::from_hsv(hsv.hue, static_cast<double>(saturation), hsv.value);
  287. color.set_alpha(alpha);
  288. return color;
  289. }
  290. constexpr Color inverted() const
  291. {
  292. return Color(~red(), ~green(), ~blue(), alpha());
  293. }
  294. constexpr Color xored(Color const& other) const
  295. {
  296. return Color(((other.m_value ^ m_value) & 0x00ffffff) | (m_value & 0xff000000));
  297. }
  298. constexpr ARGB32 value() const { return m_value; }
  299. constexpr bool operator==(Color const& other) const
  300. {
  301. return m_value == other.m_value;
  302. }
  303. String to_string() const;
  304. String to_string_without_alpha() const;
  305. static Optional<Color> from_string(StringView);
  306. constexpr HSV to_hsv() const
  307. {
  308. HSV hsv;
  309. double r = static_cast<double>(red()) / 255.0;
  310. double g = static_cast<double>(green()) / 255.0;
  311. double b = static_cast<double>(blue()) / 255.0;
  312. double max = AK::max(AK::max(r, g), b);
  313. double min = AK::min(AK::min(r, g), b);
  314. double chroma = max - min;
  315. if (!chroma)
  316. hsv.hue = 0.0;
  317. else if (max == r)
  318. hsv.hue = (60.0 * ((g - b) / chroma)) + 360.0;
  319. else if (max == g)
  320. hsv.hue = (60.0 * ((b - r) / chroma)) + 120.0;
  321. else
  322. hsv.hue = (60.0 * ((r - g) / chroma)) + 240.0;
  323. if (hsv.hue >= 360.0)
  324. hsv.hue -= 360.0;
  325. if (!max)
  326. hsv.saturation = 0;
  327. else
  328. hsv.saturation = chroma / max;
  329. hsv.value = max;
  330. VERIFY(hsv.hue >= 0.0 && hsv.hue < 360.0);
  331. VERIFY(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  332. VERIFY(hsv.value >= 0.0 && hsv.value <= 1.0);
  333. return hsv;
  334. }
  335. static constexpr Color from_hsv(double hue, double saturation, double value)
  336. {
  337. return from_hsv({ hue, saturation, value });
  338. }
  339. static constexpr Color from_hsv(HSV const& hsv)
  340. {
  341. VERIFY(hsv.hue >= 0.0 && hsv.hue < 360.0);
  342. VERIFY(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  343. VERIFY(hsv.value >= 0.0 && hsv.value <= 1.0);
  344. double hue = hsv.hue;
  345. double saturation = hsv.saturation;
  346. double value = hsv.value;
  347. int high = static_cast<int>(hue / 60.0) % 6;
  348. double f = (hue / 60.0) - high;
  349. double c1 = value * (1.0 - saturation);
  350. double c2 = value * (1.0 - saturation * f);
  351. double c3 = value * (1.0 - saturation * (1.0 - f));
  352. double r = 0;
  353. double g = 0;
  354. double b = 0;
  355. switch (high) {
  356. case 0:
  357. r = value;
  358. g = c3;
  359. b = c1;
  360. break;
  361. case 1:
  362. r = c2;
  363. g = value;
  364. b = c1;
  365. break;
  366. case 2:
  367. r = c1;
  368. g = value;
  369. b = c3;
  370. break;
  371. case 3:
  372. r = c1;
  373. g = c2;
  374. b = value;
  375. break;
  376. case 4:
  377. r = c3;
  378. g = c1;
  379. b = value;
  380. break;
  381. case 5:
  382. r = value;
  383. g = c1;
  384. b = c2;
  385. break;
  386. }
  387. u8 out_r = (u8)(r * 255);
  388. u8 out_g = (u8)(g * 255);
  389. u8 out_b = (u8)(b * 255);
  390. return Color(out_r, out_g, out_b);
  391. }
  392. constexpr Color suggested_foreground_color() const
  393. {
  394. return luminosity() < 128 ? Color::White : Color::Black;
  395. }
  396. private:
  397. constexpr explicit Color(ARGB32 argb)
  398. : m_value(argb)
  399. {
  400. }
  401. ARGB32 m_value { 0 };
  402. };
  403. constexpr Color::Color(NamedColor named)
  404. {
  405. if (named == Transparent) {
  406. m_value = 0;
  407. return;
  408. }
  409. struct {
  410. u8 r;
  411. u8 g;
  412. u8 b;
  413. } rgb;
  414. switch (named) {
  415. case Black:
  416. rgb = { 0, 0, 0 };
  417. break;
  418. case White:
  419. rgb = { 255, 255, 255 };
  420. break;
  421. case Red:
  422. rgb = { 255, 0, 0 };
  423. break;
  424. case Green:
  425. rgb = { 0, 255, 0 };
  426. break;
  427. case Cyan:
  428. rgb = { 0, 255, 255 };
  429. break;
  430. case DarkCyan:
  431. rgb = { 0, 127, 127 };
  432. break;
  433. case MidCyan:
  434. rgb = { 0, 192, 192 };
  435. break;
  436. case Blue:
  437. rgb = { 0, 0, 255 };
  438. break;
  439. case Yellow:
  440. rgb = { 255, 255, 0 };
  441. break;
  442. case Magenta:
  443. rgb = { 255, 0, 255 };
  444. break;
  445. case DarkGray:
  446. rgb = { 64, 64, 64 };
  447. break;
  448. case MidGray:
  449. rgb = { 127, 127, 127 };
  450. break;
  451. case LightGray:
  452. rgb = { 192, 192, 192 };
  453. break;
  454. case MidGreen:
  455. rgb = { 0, 192, 0 };
  456. break;
  457. case MidBlue:
  458. rgb = { 0, 0, 192 };
  459. break;
  460. case MidRed:
  461. rgb = { 192, 0, 0 };
  462. break;
  463. case MidMagenta:
  464. rgb = { 192, 0, 192 };
  465. break;
  466. case DarkGreen:
  467. rgb = { 0, 128, 0 };
  468. break;
  469. case DarkBlue:
  470. rgb = { 0, 0, 128 };
  471. break;
  472. case DarkRed:
  473. rgb = { 128, 0, 0 };
  474. break;
  475. case WarmGray:
  476. rgb = { 212, 208, 200 };
  477. break;
  478. default:
  479. VERIFY_NOT_REACHED();
  480. break;
  481. }
  482. m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
  483. }
  484. }
  485. using Gfx::Color;
  486. namespace AK {
  487. template<>
  488. struct Formatter<Gfx::Color> : public Formatter<StringView> {
  489. ErrorOr<void> format(FormatBuilder&, Gfx::Color const&);
  490. };
  491. }
  492. namespace IPC {
  493. template<>
  494. bool encode(Encoder&, Gfx::Color const&);
  495. template<>
  496. ErrorOr<void> decode(Decoder&, Gfx::Color&);
  497. }