Color.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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(const Color& 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(const Color& 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 darkened(float amount = 0.5f) const
  206. {
  207. return Color(red() * amount, green() * amount, blue() * amount, alpha());
  208. }
  209. constexpr Color lightened(float amount = 1.2f) const
  210. {
  211. return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
  212. }
  213. Vector<Color> shades(u32 steps, float max = 1.f) const
  214. {
  215. float shade = 1.f;
  216. float step = max / steps;
  217. Vector<Color> shades;
  218. for (u32 i = 0; i < steps; i++) {
  219. shade -= step;
  220. shades.append(this->darkened(shade));
  221. }
  222. return shades;
  223. }
  224. Vector<Color> tints(u32 steps, float max = 1.f) const
  225. {
  226. float shade = 1.f;
  227. float step = max / steps;
  228. Vector<Color> tints;
  229. for (u32 i = 0; i < steps; i++) {
  230. shade += step;
  231. tints.append(this->lightened(shade));
  232. }
  233. return tints;
  234. }
  235. constexpr Color inverted() const
  236. {
  237. return Color(~red(), ~green(), ~blue(), alpha());
  238. }
  239. constexpr Color xored(const Color& other) const
  240. {
  241. return Color(((other.m_value ^ m_value) & 0x00ffffff) | (m_value & 0xff000000));
  242. }
  243. constexpr RGBA32 value() const { return m_value; }
  244. constexpr bool operator==(const Color& other) const
  245. {
  246. return m_value == other.m_value;
  247. }
  248. constexpr bool operator!=(const Color& other) const
  249. {
  250. return m_value != other.m_value;
  251. }
  252. String to_string() const;
  253. String to_string_without_alpha() const;
  254. static Optional<Color> from_string(const StringView&);
  255. constexpr HSV to_hsv() const
  256. {
  257. HSV hsv;
  258. double r = static_cast<double>(red()) / 255.0;
  259. double g = static_cast<double>(green()) / 255.0;
  260. double b = static_cast<double>(blue()) / 255.0;
  261. double max = AK::max(AK::max(r, g), b);
  262. double min = AK::min(AK::min(r, g), b);
  263. double chroma = max - min;
  264. if (!chroma)
  265. hsv.hue = 0.0;
  266. else if (max == r)
  267. hsv.hue = (60.0 * ((g - b) / chroma)) + 360.0;
  268. else if (max == g)
  269. hsv.hue = (60.0 * ((b - r) / chroma)) + 120.0;
  270. else
  271. hsv.hue = (60.0 * ((r - g) / chroma)) + 240.0;
  272. if (hsv.hue >= 360.0)
  273. hsv.hue -= 360.0;
  274. if (!max)
  275. hsv.saturation = 0;
  276. else
  277. hsv.saturation = chroma / max;
  278. hsv.value = max;
  279. VERIFY(hsv.hue >= 0.0 && hsv.hue < 360.0);
  280. VERIFY(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  281. VERIFY(hsv.value >= 0.0 && hsv.value <= 1.0);
  282. return hsv;
  283. }
  284. static constexpr Color from_hsv(double hue, double saturation, double value)
  285. {
  286. return from_hsv({ hue, saturation, value });
  287. }
  288. static constexpr Color from_hsv(const HSV& hsv)
  289. {
  290. VERIFY(hsv.hue >= 0.0 && hsv.hue < 360.0);
  291. VERIFY(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  292. VERIFY(hsv.value >= 0.0 && hsv.value <= 1.0);
  293. double hue = hsv.hue;
  294. double saturation = hsv.saturation;
  295. double value = hsv.value;
  296. int high = static_cast<int>(hue / 60.0) % 6;
  297. double f = (hue / 60.0) - high;
  298. double c1 = value * (1.0 - saturation);
  299. double c2 = value * (1.0 - saturation * f);
  300. double c3 = value * (1.0 - saturation * (1.0 - f));
  301. double r = 0;
  302. double g = 0;
  303. double b = 0;
  304. switch (high) {
  305. case 0:
  306. r = value;
  307. g = c3;
  308. b = c1;
  309. break;
  310. case 1:
  311. r = c2;
  312. g = value;
  313. b = c1;
  314. break;
  315. case 2:
  316. r = c1;
  317. g = value;
  318. b = c3;
  319. break;
  320. case 3:
  321. r = c1;
  322. g = c2;
  323. b = value;
  324. break;
  325. case 4:
  326. r = c3;
  327. g = c1;
  328. b = value;
  329. break;
  330. case 5:
  331. r = value;
  332. g = c1;
  333. b = c2;
  334. break;
  335. }
  336. u8 out_r = (u8)(r * 255);
  337. u8 out_g = (u8)(g * 255);
  338. u8 out_b = (u8)(b * 255);
  339. return Color(out_r, out_g, out_b);
  340. }
  341. constexpr Color suggested_foreground_color() const
  342. {
  343. return luminosity() < 128 ? Color::White : Color::Black;
  344. }
  345. private:
  346. constexpr explicit Color(RGBA32 rgba)
  347. : m_value(rgba)
  348. {
  349. }
  350. RGBA32 m_value { 0 };
  351. };
  352. constexpr Color::Color(NamedColor named)
  353. {
  354. if (named == Transparent) {
  355. m_value = 0;
  356. return;
  357. }
  358. struct {
  359. u8 r;
  360. u8 g;
  361. u8 b;
  362. } rgb;
  363. switch (named) {
  364. case Black:
  365. rgb = { 0, 0, 0 };
  366. break;
  367. case White:
  368. rgb = { 255, 255, 255 };
  369. break;
  370. case Red:
  371. rgb = { 255, 0, 0 };
  372. break;
  373. case Green:
  374. rgb = { 0, 255, 0 };
  375. break;
  376. case Cyan:
  377. rgb = { 0, 255, 255 };
  378. break;
  379. case DarkCyan:
  380. rgb = { 0, 127, 127 };
  381. break;
  382. case MidCyan:
  383. rgb = { 0, 192, 192 };
  384. break;
  385. case Blue:
  386. rgb = { 0, 0, 255 };
  387. break;
  388. case Yellow:
  389. rgb = { 255, 255, 0 };
  390. break;
  391. case Magenta:
  392. rgb = { 255, 0, 255 };
  393. break;
  394. case DarkGray:
  395. rgb = { 64, 64, 64 };
  396. break;
  397. case MidGray:
  398. rgb = { 127, 127, 127 };
  399. break;
  400. case LightGray:
  401. rgb = { 192, 192, 192 };
  402. break;
  403. case MidGreen:
  404. rgb = { 0, 192, 0 };
  405. break;
  406. case MidBlue:
  407. rgb = { 0, 0, 192 };
  408. break;
  409. case MidRed:
  410. rgb = { 192, 0, 0 };
  411. break;
  412. case MidMagenta:
  413. rgb = { 192, 0, 192 };
  414. break;
  415. case DarkGreen:
  416. rgb = { 0, 128, 0 };
  417. break;
  418. case DarkBlue:
  419. rgb = { 0, 0, 128 };
  420. break;
  421. case DarkRed:
  422. rgb = { 128, 0, 0 };
  423. break;
  424. case WarmGray:
  425. rgb = { 212, 208, 200 };
  426. break;
  427. default:
  428. VERIFY_NOT_REACHED();
  429. break;
  430. }
  431. m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
  432. }
  433. }
  434. using Gfx::Color;
  435. namespace AK {
  436. template<>
  437. struct Formatter<Gfx::Color> : public Formatter<StringView> {
  438. void format(FormatBuilder& builder, const Gfx::Color& value);
  439. };
  440. }
  441. namespace IPC {
  442. bool encode(Encoder&, const Gfx::Color&);
  443. bool decode(Decoder&, Gfx::Color&);
  444. }