Color.h 3.4 KB

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