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