CSSOKLCH.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSOKLCH.h"
  7. #include <AK/Math.h>
  8. #include <AK/TypeCasts.h>
  9. #include <LibWeb/CSS/Serialize.h>
  10. #include <LibWeb/CSS/StyleValues/CSSMathValue.h>
  11. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  13. namespace Web::CSS {
  14. Color CSSOKLCH::to_color(Optional<Layout::NodeWithStyle const&>) const
  15. {
  16. auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1);
  17. auto const c_val = max(resolve_with_reference_value(m_properties.c, 0.4).value_or(0), 0);
  18. auto const h_val = AK::to_radians(resolve_hue(m_properties.h).value_or(0));
  19. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  20. return Color::from_oklab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val);
  21. }
  22. bool CSSOKLCH::equals(CSSStyleValue const& other) const
  23. {
  24. if (type() != other.type())
  25. return false;
  26. auto const& other_color = other.as_color();
  27. if (color_type() != other_color.color_type())
  28. return false;
  29. auto const& other_oklch = verify_cast<CSSOKLCH>(other_color);
  30. return m_properties == other_oklch.m_properties;
  31. }
  32. // https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch
  33. String CSSOKLCH::to_string() const
  34. {
  35. // FIXME: Do this properly, taking unresolved calculated values into account.
  36. return serialize_a_srgb_value(to_color({}));
  37. }
  38. }