2018-10-10 14:49:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
typedef dword RGBA32;
|
|
|
|
|
2019-01-25 01:09:29 +00:00
|
|
|
inline constexpr dword make_rgb(byte r, byte g, byte b)
|
|
|
|
{
|
|
|
|
return ((r << 16) | (g << 8) | b);
|
|
|
|
}
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
class Color {
|
|
|
|
public:
|
2018-10-12 18:05:11 +00:00
|
|
|
enum NamedColor {
|
|
|
|
Black,
|
|
|
|
White,
|
|
|
|
Red,
|
|
|
|
Green,
|
|
|
|
Blue,
|
2019-01-12 03:02:36 +00:00
|
|
|
Yellow,
|
|
|
|
Magenta,
|
2018-10-12 21:02:23 +00:00
|
|
|
DarkGray,
|
|
|
|
MidGray,
|
|
|
|
LightGray,
|
2018-10-12 18:05:11 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
Color() { }
|
2018-10-12 18:05:11 +00:00
|
|
|
Color(NamedColor);
|
2019-02-19 00:42:53 +00:00
|
|
|
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); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-02-07 22:13:47 +00:00
|
|
|
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; }
|
|
|
|
|
2019-02-19 00:42:53 +00:00
|
|
|
void set_alpha(byte value)
|
2019-02-07 22:13:47 +00:00
|
|
|
{
|
2019-02-19 00:42:53 +00:00
|
|
|
m_value &= 0x00ffffff;
|
|
|
|
m_value |= value << 24;
|
2019-02-07 22:13:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 00:42:53 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-01-25 04:01:27 +00:00
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32 value() const { return m_value; }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
|
|
|
private:
|
2019-02-19 00:42:53 +00:00
|
|
|
explicit Color(RGBA32 rgba) : m_value(rgba) { }
|
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32 m_value { 0 };
|
2018-10-10 14:49:36 +00:00
|
|
|
};
|