Color.h 13 KB

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