Color.h 16 KB

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