mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-25 19:02:07 -05:00
e43b27a3fa
This parses hex colors in either #RRGGBBAA or #RRGGBB format. No other formats are supported at the moment.
121 lines
2.7 KiB
C++
121 lines
2.7 KiB
C++
#include <AK/Assertions.h>
|
|
#include <LibDraw/Color.h>
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
|
|
Color::Color(NamedColor named)
|
|
{
|
|
struct {
|
|
u8 r;
|
|
u8 g;
|
|
u8 b;
|
|
} rgb;
|
|
|
|
switch (named) {
|
|
case Black:
|
|
rgb = { 0, 0, 0 };
|
|
break;
|
|
case White:
|
|
rgb = { 255, 255, 255 };
|
|
break;
|
|
case Red:
|
|
rgb = { 255, 0, 0 };
|
|
break;
|
|
case Green:
|
|
rgb = { 0, 255, 0 };
|
|
break;
|
|
case Cyan:
|
|
rgb = { 0, 255, 255 };
|
|
break;
|
|
case Blue:
|
|
rgb = { 0, 0, 255 };
|
|
break;
|
|
case Yellow:
|
|
rgb = { 255, 255, 0 };
|
|
break;
|
|
case Magenta:
|
|
rgb = { 255, 0, 255 };
|
|
break;
|
|
case DarkGray:
|
|
rgb = { 64, 64, 64 };
|
|
break;
|
|
case MidGray:
|
|
rgb = { 127, 127, 127 };
|
|
break;
|
|
case LightGray:
|
|
rgb = { 192, 192, 192 };
|
|
break;
|
|
case MidGreen:
|
|
rgb = { 0, 192, 0 };
|
|
break;
|
|
case MidBlue:
|
|
rgb = { 0, 0, 192 };
|
|
break;
|
|
case MidRed:
|
|
rgb = { 192, 0, 0 };
|
|
break;
|
|
case MidMagenta:
|
|
rgb = { 192, 0, 192 };
|
|
break;
|
|
case DarkGreen:
|
|
rgb = { 0, 128, 0 };
|
|
break;
|
|
case DarkBlue:
|
|
rgb = { 0, 0, 128 };
|
|
break;
|
|
case DarkRed:
|
|
rgb = { 128, 0, 0 };
|
|
break;
|
|
case WarmGray:
|
|
rgb = { 212, 208, 200 };
|
|
break;
|
|
default:
|
|
ASSERT_NOT_REACHED();
|
|
break;
|
|
}
|
|
|
|
m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
|
|
}
|
|
|
|
String Color::to_string() const
|
|
{
|
|
return String::format("#%b%b%b%b", red(), green(), blue(), alpha());
|
|
}
|
|
|
|
Optional<Color> Color::from_string(const StringView& string)
|
|
{
|
|
if (string.is_empty())
|
|
return {};
|
|
|
|
if (string[0] != '#')
|
|
return {};
|
|
|
|
if (string.length() != 7 && string.length() != 9)
|
|
return {};
|
|
|
|
auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
|
|
if (!isxdigit(nibble))
|
|
return {};
|
|
if (nibble >= '0' && nibble <= '9')
|
|
return nibble - '0';
|
|
return 10 + (tolower(nibble) - 'a');
|
|
};
|
|
|
|
auto to_hex = [&](char c1, char c2) -> Optional<u8> {
|
|
auto nib1 = hex_nibble_to_u8(c1);
|
|
auto nib2 = hex_nibble_to_u8(c2);
|
|
if (!nib1.has_value() || !nib2.has_value())
|
|
return {};
|
|
return nib1.value() << 4 | nib2.value();
|
|
};
|
|
|
|
Optional<u8> r = to_hex(string[1], string[2]);
|
|
Optional<u8> g = to_hex(string[3], string[4]);
|
|
Optional<u8> b = to_hex(string[5], string[6]);
|
|
Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
|
|
|
|
if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
|
|
return {};
|
|
|
|
return Color(r.value(), g.value(), b.value(), a.value());
|
|
}
|