Color.h 20 KB

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