Color.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Assertions.h>
  28. #include <AK/Forward.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <LibIPC/Forward.h>
  31. namespace Gfx {
  32. enum class ColorRole;
  33. typedef u32 RGBA32;
  34. inline constexpr u32 make_rgb(u8 r, u8 g, u8 b)
  35. {
  36. return ((r << 16) | (g << 8) | b);
  37. }
  38. struct HSV {
  39. double hue { 0 };
  40. double saturation { 0 };
  41. double value { 0 };
  42. };
  43. class Color {
  44. public:
  45. enum NamedColor {
  46. Transparent,
  47. Black,
  48. White,
  49. Red,
  50. Green,
  51. Cyan,
  52. Blue,
  53. Yellow,
  54. Magenta,
  55. DarkGray,
  56. MidGray,
  57. LightGray,
  58. WarmGray,
  59. DarkCyan,
  60. DarkGreen,
  61. DarkBlue,
  62. DarkRed,
  63. MidCyan,
  64. MidGreen,
  65. MidRed,
  66. MidBlue,
  67. MidMagenta,
  68. };
  69. constexpr Color() { }
  70. Color(NamedColor);
  71. constexpr Color(u8 r, u8 g, u8 b)
  72. : m_value(0xff000000 | (r << 16) | (g << 8) | b)
  73. {
  74. }
  75. constexpr Color(u8 r, u8 g, u8 b, u8 a)
  76. : m_value((a << 24) | (r << 16) | (g << 8) | b)
  77. {
  78. }
  79. static constexpr Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
  80. static constexpr Color from_rgba(unsigned rgba) { return Color(rgba); }
  81. u8 red() const { return (m_value >> 16) & 0xff; }
  82. u8 green() const { return (m_value >> 8) & 0xff; }
  83. u8 blue() const { return m_value & 0xff; }
  84. u8 alpha() const { return (m_value >> 24) & 0xff; }
  85. void set_alpha(u8 value)
  86. {
  87. m_value &= 0x00ffffff;
  88. m_value |= value << 24;
  89. }
  90. void set_red(u8 value)
  91. {
  92. m_value &= 0xff00ffff;
  93. m_value |= value << 16;
  94. }
  95. void set_green(u8 value)
  96. {
  97. m_value &= 0xffff00ff;
  98. m_value |= value << 8;
  99. }
  100. void set_blue(u8 value)
  101. {
  102. m_value &= 0xffffff00;
  103. m_value |= value;
  104. }
  105. Color with_alpha(u8 alpha)
  106. {
  107. return Color((m_value & 0x00ffffff) | alpha << 24);
  108. }
  109. Color blend(Color source) const
  110. {
  111. if (!alpha() || source.alpha() == 255)
  112. return source;
  113. if (!source.alpha())
  114. return *this;
  115. int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
  116. u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
  117. u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
  118. u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
  119. u8 a = d / 255;
  120. return Color(r, g, b, a);
  121. }
  122. Color to_grayscale() const
  123. {
  124. int gray = (red() + green() + blue()) / 3;
  125. return Color(gray, gray, gray, alpha());
  126. }
  127. Color darkened(float amount = 0.5f) const
  128. {
  129. return Color(red() * amount, green() * amount, blue() * amount, alpha());
  130. }
  131. Color lightened(float amount = 1.2f) const
  132. {
  133. return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
  134. }
  135. Color inverted() const
  136. {
  137. return Color(~red(), ~green(), ~blue());
  138. }
  139. RGBA32 value() const { return m_value; }
  140. bool operator==(const Color& other) const
  141. {
  142. return m_value == other.m_value;
  143. }
  144. bool operator!=(const Color& other) const
  145. {
  146. return m_value != other.m_value;
  147. }
  148. String to_string() const;
  149. String to_string_without_alpha() const;
  150. static Optional<Color> from_string(const StringView&);
  151. HSV to_hsv() const
  152. {
  153. HSV hsv;
  154. double r = static_cast<double>(red()) / 255.0;
  155. double g = static_cast<double>(green()) / 255.0;
  156. double b = static_cast<double>(blue()) / 255.0;
  157. double max = AK::max(AK::max(r, g), b);
  158. double min = AK::min(AK::min(r, g), b);
  159. double chroma = max - min;
  160. if (!chroma)
  161. hsv.hue = 0.0;
  162. else if (max == r)
  163. hsv.hue = (60.0 * ((g - b) / chroma)) + 360.0;
  164. else if (max == g)
  165. hsv.hue = (60.0 * ((b - r) / chroma)) + 120.0;
  166. else
  167. hsv.hue = (60.0 * ((r - g) / chroma)) + 240.0;
  168. if (hsv.hue >= 360.0)
  169. hsv.hue -= 360.0;
  170. if (!max)
  171. hsv.saturation = 0;
  172. else
  173. hsv.saturation = chroma / max;
  174. hsv.value = max;
  175. ASSERT(hsv.hue >= 0.0 && hsv.hue < 360.0);
  176. ASSERT(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  177. ASSERT(hsv.value >= 0.0 && hsv.value <= 1.0);
  178. return hsv;
  179. }
  180. static Color from_hsv(double hue, double saturation, double value)
  181. {
  182. return from_hsv({ hue, saturation, value });
  183. }
  184. static Color from_hsv(const HSV& hsv)
  185. {
  186. ASSERT(hsv.hue >= 0.0 && hsv.hue < 360.0);
  187. ASSERT(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
  188. ASSERT(hsv.value >= 0.0 && hsv.value <= 1.0);
  189. double hue = hsv.hue;
  190. double saturation = hsv.saturation;
  191. double value = hsv.value;
  192. int high = static_cast<int>(hue / 60.0) % 6;
  193. double f = (hue / 60.0) - high;
  194. double c1 = value * (1.0 - saturation);
  195. double c2 = value * (1.0 - saturation * f);
  196. double c3 = value * (1.0 - saturation * (1.0 - f));
  197. double r = 0;
  198. double g = 0;
  199. double b = 0;
  200. switch (high) {
  201. case 0:
  202. r = value;
  203. g = c3;
  204. b = c1;
  205. break;
  206. case 1:
  207. r = c2;
  208. g = value;
  209. b = c1;
  210. break;
  211. case 2:
  212. r = c1;
  213. g = value;
  214. b = c3;
  215. break;
  216. case 3:
  217. r = c1;
  218. g = c2;
  219. b = value;
  220. break;
  221. case 4:
  222. r = c3;
  223. g = c1;
  224. b = value;
  225. break;
  226. case 5:
  227. r = value;
  228. g = c1;
  229. b = c2;
  230. break;
  231. }
  232. u8 out_r = (u8)(r * 255);
  233. u8 out_g = (u8)(g * 255);
  234. u8 out_b = (u8)(b * 255);
  235. return Color(out_r, out_g, out_b);
  236. }
  237. private:
  238. constexpr explicit Color(RGBA32 rgba)
  239. : m_value(rgba)
  240. {
  241. }
  242. RGBA32 m_value { 0 };
  243. };
  244. const LogStream& operator<<(const LogStream&, Color);
  245. }
  246. using Gfx::Color;
  247. namespace IPC {
  248. bool encode(Encoder&, const Gfx::Color&);
  249. bool decode(Decoder&, Gfx::Color&);
  250. }