Color.h 16 KB

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