Color.h 13 KB

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