LibGfx+LibWeb: Use floats not doubles to create HSL colors

There's really no reason to use doubles here, except at the time I
wanted to use doubles everywhere in CSS. I now realize that is
excessive, so everything can be floats instead.
This commit is contained in:
Sam Atkins 2022-03-21 17:13:52 +00:00 committed by Andreas Kling
parent 75ec960495
commit 404a7cb63a
Notes: sideshowbarker 2024-07-17 16:55:33 +09:00
2 changed files with 34 additions and 34 deletions

View File

@ -75,57 +75,57 @@ public:
return Color(r, g, b);
}
static constexpr Color from_hsl(double h_degrees, double s, double l) { return from_hsla(h_degrees, s, l, 1.0); }
static constexpr Color from_hsla(double h_degrees, double s, double l, double a)
static constexpr Color from_hsl(float h_degrees, float s, float l) { return from_hsla(h_degrees, s, l, 1.0); }
static constexpr Color from_hsla(float h_degrees, float s, float l, float a)
{
// Algorithm from https://www.w3.org/TR/css-color-3/#hsl-color
double h = clamp(h_degrees / 360.0, 0.0, 1.0);
s = clamp(s, 0.0, 1.0);
l = clamp(l, 0.0, 1.0);
a = clamp(a, 0.0, 1.0);
float h = clamp(h_degrees / 360.0f, 0.0f, 1.0f);
s = clamp(s, 0.0f, 1.0f);
l = clamp(l, 0.0f, 1.0f);
a = clamp(a, 0.0f, 1.0f);
// HOW TO RETURN hue.to.rgb(m1, m2, h):
auto hue_to_rgb = [](double m1, double m2, double h) -> double {
auto hue_to_rgb = [](float m1, float m2, float h) -> float {
// IF h<0: PUT h+1 IN h
if (h < 0.0)
h = h + 1.0;
if (h < 0.0f)
h = h + 1.0f;
// IF h>1: PUT h-1 IN h
if (h > 1.0)
h = h - 1.0;
if (h > 1.0f)
h = h - 1.0f;
// IF h*6<1: RETURN m1+(m2-m1)*h*6
if (h * 6.0 < 1.0)
return m1 + (m2 - m1) * h * 6.0;
if (h * 6.0f < 1.0f)
return m1 + (m2 - m1) * h * 6.0f;
// IF h*2<1: RETURN m2
if (h * 2.0 < 1.0)
if (h * 2.0f < 1.0f)
return m2;
// IF h*3<2: RETURN m1+(m2-m1)*(2/3-h)*6
if (h * 3.0 < 2.0)
return m1 + (m2 - m1) * (2.0 / 3.0 - h) * 6.0;
if (h * 3.0f < 2.0f)
return m1 + (m2 - m1) * (2.0f / 3.0f - h) * 6.0f;
// RETURN m1
return m1;
};
// SELECT:
// l<=0.5: PUT l*(s+1) IN m2
double m2;
if (l <= 0.5)
m2 = l * (s + 1.0);
float m2;
if (l <= 0.5f)
m2 = l * (s + 1.0f);
// ELSE: PUT l+s-l*s IN m2
else
m2 = l + s - l * s;
// PUT l*2-m2 IN m1
double m1 = l * 2.0 - m2;
float m1 = l * 2.0f - m2;
// PUT hue.to.rgb(m1, m2, h+1/3) IN r
double r = hue_to_rgb(m1, m2, h + 1.0 / 3.0);
float r = hue_to_rgb(m1, m2, h + 1.0f / 3.0f);
// PUT hue.to.rgb(m1, m2, h ) IN g
double g = hue_to_rgb(m1, m2, h);
float g = hue_to_rgb(m1, m2, h);
// PUT hue.to.rgb(m1, m2, h-1/3) IN b
double b = hue_to_rgb(m1, m2, h - 1.0 / 3.0);
float b = hue_to_rgb(m1, m2, h - 1.0f / 3.0f);
// RETURN (r, g, b)
u8 r_u8 = clamp(lroundf(r * 255.0), 0, 255);
u8 g_u8 = clamp(lroundf(g * 255.0), 0, 255);
u8 b_u8 = clamp(lroundf(b * 255.0), 0, 255);
u8 a_u8 = clamp(lroundf(a * 255.0), 0, 255);
u8 r_u8 = clamp(lroundf(r * 255.0f), 0, 255);
u8 g_u8 = clamp(lroundf(g * 255.0f), 0, 255);
u8 b_u8 = clamp(lroundf(b * 255.0f), 0, 255);
u8 a_u8 = clamp(lroundf(a * 255.0f), 0, 255);
return Color(r_u8, g_u8, b_u8, a_u8);
}

View File

@ -2530,9 +2530,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
&& s_val.is(Token::Type::Percentage)
&& l_val.is(Token::Type::Percentage)) {
auto h = h_val.number_value();
auto s = s_val.percentage() / 100.0;
auto l = l_val.percentage() / 100.0;
auto h = static_cast<float>(h_val.number_value());
auto s = static_cast<float>(s_val.percentage() / 100.0f);
auto l = static_cast<float>(l_val.percentage() / 100.0f);
return Color::from_hsl(h, s, l);
}
} else if (function.name().equals_ignoring_case("hsla")) {
@ -2549,10 +2549,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
&& l_val.is(Token::Type::Percentage)
&& a_val.is(Token::Type::Number)) {
auto h = h_val.number_value();
auto s = s_val.percentage() / 100.0;
auto l = l_val.percentage() / 100.0;
auto a = a_val.number_value();
auto h = static_cast<float>(h_val.number_value());
auto s = static_cast<float>(s_val.percentage() / 100.0f);
auto l = static_cast<float>(l_val.percentage() / 100.0f);
auto a = static_cast<float>(a_val.number_value());
return Color::from_hsla(h, s, l, a);
}
}