mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
LibWeb: Don't resolve colors with unresolved components
`CSSColorValue`s which have unresolved `calc` components should be able to be resolved. Previously we would always resolve them but with incorrect values. This is useful as we will now be able to now whether we should serialize colors in their normalized form or not. Slight regression in that we now serialize (RGB, HSL and HWB) colors with components that rely on compute-time information as an empty string, but that will be fixed in the next commit.
This commit is contained in:
parent
e66332c07a
commit
6a9c8d7767
Notes:
github-actions[bot]
2025-07-16 12:06:47 +00:00
Author: https://github.com/Calme1709
Commit: 6a9c8d7767
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5305
Reviewed-by: https://github.com/AtkinsSJ ✅
30 changed files with 278 additions and 129 deletions
|
@ -27,14 +27,20 @@ bool CSSLCHLike::equals(CSSStyleValue const& other) const
|
|||
return m_properties == other_oklch_like.m_properties;
|
||||
}
|
||||
|
||||
Color CSSLCH::to_color(Optional<Layout::NodeWithStyle const&>, CalculationResolutionContext const& resolution_context) const
|
||||
Optional<Color> CSSLCH::to_color(Optional<Layout::NodeWithStyle const&>, CalculationResolutionContext const& resolution_context) const
|
||||
{
|
||||
auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 100, resolution_context).value_or(0), 0, 100);
|
||||
auto const c_val = resolve_with_reference_value(m_properties.c, 150, resolution_context).value_or(0);
|
||||
auto const h_val = AK::to_radians(resolve_hue(m_properties.h, resolution_context).value_or(0));
|
||||
auto const alpha_val = resolve_alpha(m_properties.alpha, resolution_context).value_or(1);
|
||||
auto raw_l_val = resolve_with_reference_value(m_properties.l, 100, resolution_context);
|
||||
auto c_val = resolve_with_reference_value(m_properties.c, 150, resolution_context);
|
||||
auto raw_h_val = resolve_hue(m_properties.h, resolution_context);
|
||||
auto alpha_val = resolve_alpha(m_properties.alpha, resolution_context);
|
||||
|
||||
return Color::from_lab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val);
|
||||
if (!raw_l_val.has_value() || !c_val.has_value() || !raw_h_val.has_value() || !alpha_val.has_value())
|
||||
return {};
|
||||
|
||||
auto l_val = clamp(raw_l_val.value(), 0, 100);
|
||||
auto h_val = AK::to_radians(raw_h_val.value());
|
||||
|
||||
return Color::from_lab(l_val, c_val.value() * cos(h_val), c_val.value() * sin(h_val), alpha_val.value());
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/css-color-4/#serializing-lab-lch
|
||||
|
@ -57,14 +63,21 @@ String CSSLCH::to_string(SerializationMode mode) const
|
|||
return MUST(builder.to_string());
|
||||
}
|
||||
|
||||
Color CSSOKLCH::to_color(Optional<Layout::NodeWithStyle const&>, CalculationResolutionContext const& resolution_context) const
|
||||
Optional<Color> CSSOKLCH::to_color(Optional<Layout::NodeWithStyle const&>, CalculationResolutionContext const& resolution_context) const
|
||||
{
|
||||
auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0, resolution_context).value_or(0), 0, 1);
|
||||
auto const c_val = max(resolve_with_reference_value(m_properties.c, 0.4, resolution_context).value_or(0), 0);
|
||||
auto const h_val = AK::to_radians(resolve_hue(m_properties.h, resolution_context).value_or(0));
|
||||
auto const alpha_val = resolve_alpha(m_properties.alpha, resolution_context).value_or(1);
|
||||
auto raw_l_val = resolve_with_reference_value(m_properties.l, 1.0, resolution_context);
|
||||
auto raw_c_val = resolve_with_reference_value(m_properties.c, 0.4, resolution_context);
|
||||
auto raw_h_val = resolve_hue(m_properties.h, resolution_context);
|
||||
auto alpha_val = resolve_alpha(m_properties.alpha, resolution_context);
|
||||
|
||||
return Color::from_oklab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val);
|
||||
if (!raw_l_val.has_value() || !raw_c_val.has_value() || !raw_h_val.has_value() || !alpha_val.has_value())
|
||||
return {};
|
||||
|
||||
auto l_val = clamp(raw_l_val.value(), 0, 1);
|
||||
auto c_val = max(raw_c_val.value(), 0);
|
||||
auto h_val = AK::to_radians(raw_h_val.value());
|
||||
|
||||
return Color::from_oklab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val.value());
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue