mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-24 19:28:48 +00:00
LibGfx: Reimplement from_hsla
- Hue now wraps properly when negative or larger than 360 - The hsl to rgb conversion now closely mirrors the code example from the spec. This fixes a number of WPT tests in /css/css-color/parsing/color-computed-hsl.html
This commit is contained in:
parent
bc846d933d
commit
201648485f
Notes:
github-actions[bot]
2024-11-22 11:11:26 +00:00
Author: https://github.com/glupi-borna 🔰
Commit: 201648485f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2495
Reviewed-by: https://github.com/LucasChollet ✅
Reviewed-by: https://github.com/gmta
2 changed files with 28 additions and 37 deletions
|
@ -131,49 +131,25 @@ public:
|
|||
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
|
||||
float h = clamp(h_degrees / 360.0f, 0.0f, 1.0f);
|
||||
|
||||
float h = fmodf(h_degrees, 360.0f);
|
||||
if (h < 0.0)
|
||||
h += 360.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 = [](float m1, float m2, float h) -> float {
|
||||
// IF h<0: PUT h+1 IN h
|
||||
if (h < 0.0f)
|
||||
h = h + 1.0f;
|
||||
// IF h>1: PUT h-1 IN h
|
||||
if (h > 1.0f)
|
||||
h = h - 1.0f;
|
||||
// IF h*6<1: RETURN m1+(m2-m1)*h*6
|
||||
if (h * 6.0f < 1.0f)
|
||||
return m1 + (m2 - m1) * h * 6.0f;
|
||||
// IF h*2<1: RETURN m2
|
||||
if (h * 2.0f < 1.0f)
|
||||
return m2;
|
||||
// IF h*3<2: RETURN m1+(m2-m1)*(2/3-h)*6
|
||||
if (h * 3.0f < 2.0f)
|
||||
return m1 + (m2 - m1) * (2.0f / 3.0f - h) * 6.0f;
|
||||
// RETURN m1
|
||||
return m1;
|
||||
auto to_rgb = [](float h, float s, float l, float offset) {
|
||||
float k = fmodf(offset + h / 30.0f, 12.0f);
|
||||
float a = s * min(l, 1.0f - l);
|
||||
return l - a * max(-1.0f, min(min(k - 3.0f, 9.0f - k), 1.0f));
|
||||
};
|
||||
|
||||
// SELECT:
|
||||
// l<=0.5: PUT l*(s+1) IN m2
|
||||
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
|
||||
float m1 = l * 2.0f - m2;
|
||||
// PUT hue.to.rgb(m1, m2, h+1/3) IN r
|
||||
float r = hue_to_rgb(m1, m2, h + 1.0f / 3.0f);
|
||||
// PUT hue.to.rgb(m1, m2, h ) IN g
|
||||
float g = hue_to_rgb(m1, m2, h);
|
||||
// PUT hue.to.rgb(m1, m2, h-1/3) IN b
|
||||
float b = hue_to_rgb(m1, m2, h - 1.0f / 3.0f);
|
||||
// RETURN (r, g, b)
|
||||
float r = to_rgb(h, s, l, 0.0f);
|
||||
float g = to_rgb(h, s, l, 8.0f);
|
||||
float b = to_rgb(h, s, l, 4.0f);
|
||||
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue