mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
LibGfx: Accept decimal numbers when parsing rgb and rgba strings
This will round all numbers and clamp them within the limits of a u8. This also standardises on returning u8 instead of uint/int.
This commit is contained in:
parent
da3cfd5bbc
commit
2ecc1d64d8
1 changed files with 10 additions and 10 deletions
|
@ -49,14 +49,14 @@ static Optional<Color> parse_rgb_color(StringView string)
|
|||
if (parts.size() != 3)
|
||||
return {};
|
||||
|
||||
auto r = parts[0].to_uint().value_or(256);
|
||||
auto g = parts[1].to_uint().value_or(256);
|
||||
auto b = parts[2].to_uint().value_or(256);
|
||||
auto r = parts[0].to_double().map(AK::clamp_to<u8, double>);
|
||||
auto g = parts[1].to_double().map(AK::clamp_to<u8, double>);
|
||||
auto b = parts[2].to_double().map(AK::clamp_to<u8, double>);
|
||||
|
||||
if (r > 255 || g > 255 || b > 255)
|
||||
if (!r.has_value() || !g.has_value() || !b.has_value())
|
||||
return {};
|
||||
|
||||
return Color(r, g, b);
|
||||
return Color(*r, *g, *b);
|
||||
}
|
||||
|
||||
static Optional<Color> parse_rgba_color(StringView string)
|
||||
|
@ -70,9 +70,9 @@ static Optional<Color> parse_rgba_color(StringView string)
|
|||
if (parts.size() != 4)
|
||||
return {};
|
||||
|
||||
auto r = parts[0].to_int().value_or(256);
|
||||
auto g = parts[1].to_int().value_or(256);
|
||||
auto b = parts[2].to_int().value_or(256);
|
||||
auto r = parts[0].to_double().map(AK::clamp_to<u8, double>);
|
||||
auto g = parts[1].to_double().map(AK::clamp_to<u8, double>);
|
||||
auto b = parts[2].to_double().map(AK::clamp_to<u8, double>);
|
||||
|
||||
double alpha = 0;
|
||||
auto alpha_str = parts[3].trim_whitespace();
|
||||
|
@ -83,10 +83,10 @@ static Optional<Color> parse_rgba_color(StringView string)
|
|||
|
||||
unsigned a = alpha * 255;
|
||||
|
||||
if (r > 255 || g > 255 || b > 255 || a > 255)
|
||||
if (!r.has_value() || !g.has_value() || !b.has_value() || a > 255)
|
||||
return {};
|
||||
|
||||
return Color(r, g, b, a);
|
||||
return Color(*r, *g, *b, a);
|
||||
}
|
||||
|
||||
Optional<Color> Color::from_named_css_color_string(StringView string)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue