Color.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include <AK/Optional.h>
  3. #include <AK/String.h>
  4. #include <AK/Types.h>
  5. typedef u32 RGBA32;
  6. inline constexpr u32 make_rgb(u8 r, u8 g, u8 b)
  7. {
  8. return ((r << 16) | (g << 8) | b);
  9. }
  10. enum class SystemColor {
  11. DesktopBackground,
  12. ActiveWindowBorder1,
  13. ActiveWindowBorder2,
  14. ActiveWindowTitle,
  15. InactiveWindowBorder1,
  16. InactiveWindowBorder2,
  17. InactiveWindowTitle,
  18. MovingWindowBorder1,
  19. MovingWindowBorder2,
  20. MovingWindowTitle,
  21. HighlightWindowBorder1,
  22. HighlightWindowBorder2,
  23. HighlightWindowTitle,
  24. MenuStripe,
  25. MenuBase,
  26. MenuSelection,
  27. Window,
  28. Text,
  29. Base,
  30. ThreedHighlight,
  31. ThreedShadow1,
  32. ThreedShadow2,
  33. HoverHighlight,
  34. };
  35. class Color {
  36. public:
  37. enum NamedColor {
  38. Black,
  39. White,
  40. Red,
  41. Green,
  42. Cyan,
  43. Blue,
  44. Yellow,
  45. Magenta,
  46. DarkGray,
  47. MidGray,
  48. LightGray,
  49. WarmGray,
  50. DarkCyan,
  51. DarkGreen,
  52. DarkBlue,
  53. DarkRed,
  54. MidCyan,
  55. MidGreen,
  56. MidRed,
  57. MidBlue,
  58. MidMagenta,
  59. };
  60. Color() {}
  61. Color(NamedColor);
  62. Color(SystemColor);
  63. Color(u8 r, u8 g, u8 b)
  64. : m_value(0xff000000 | (r << 16) | (g << 8) | b)
  65. {
  66. }
  67. Color(u8 r, u8 g, u8 b, u8 a)
  68. : m_value((a << 24) | (r << 16) | (g << 8) | b)
  69. {
  70. }
  71. static Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
  72. static Color from_rgba(unsigned rgba) { return Color(rgba); }
  73. u8 red() const { return (m_value >> 16) & 0xff; }
  74. u8 green() const { return (m_value >> 8) & 0xff; }
  75. u8 blue() const { return m_value & 0xff; }
  76. u8 alpha() const { return (m_value >> 24) & 0xff; }
  77. void set_alpha(u8 value)
  78. {
  79. m_value &= 0x00ffffff;
  80. m_value |= value << 24;
  81. }
  82. void set_red(u8 value)
  83. {
  84. m_value &= 0xff00ffff;
  85. m_value |= value << 16;
  86. }
  87. void set_green(u8 value)
  88. {
  89. m_value &= 0xffff00ff;
  90. m_value |= value << 8;
  91. }
  92. void set_blue(u8 value)
  93. {
  94. m_value &= 0xffffff00;
  95. m_value |= value;
  96. }
  97. Color with_alpha(u8 alpha)
  98. {
  99. return Color((m_value & 0x00ffffff) | alpha << 24);
  100. }
  101. Color blend(Color source) const
  102. {
  103. if (!alpha() || source.alpha() == 255)
  104. return source;
  105. if (!source.alpha())
  106. return *this;
  107. int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
  108. u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
  109. u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
  110. u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
  111. u8 a = d / 255;
  112. return Color(r, g, b, a);
  113. }
  114. Color to_grayscale() const
  115. {
  116. int gray = (red() + green() + blue()) / 3;
  117. return Color(gray, gray, gray, alpha());
  118. }
  119. Color darkened(float amount = 0.5) const
  120. {
  121. return Color(red() * amount, green() * amount, blue() * amount, alpha());
  122. }
  123. Color lightened() const
  124. {
  125. return Color(min(255.0, red() * 1.2), min(255.0, green() * 1.2), min(255.0, blue() * 1.2), alpha());
  126. }
  127. Color inverted() const
  128. {
  129. return Color(~red(), ~green(), ~blue());
  130. }
  131. RGBA32 value() const { return m_value; }
  132. bool operator==(const Color& other) const
  133. {
  134. return m_value == other.m_value;
  135. }
  136. bool operator!=(const Color& other) const
  137. {
  138. return m_value != other.m_value;
  139. }
  140. String to_string() const;
  141. static Optional<Color> from_string(const StringView&);
  142. private:
  143. explicit Color(RGBA32 rgba)
  144. : m_value(rgba)
  145. {
  146. }
  147. RGBA32 m_value { 0 };
  148. };
  149. inline const LogStream& operator<<(const LogStream& stream, Color value)
  150. {
  151. return stream << value.to_string();
  152. }