Color.h 16 KB

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