CSSLabLike.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSLabLike.h"
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. #include <LibWeb/CSS/StyleValues/CSSMathValue.h>
  10. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  12. namespace Web::CSS {
  13. bool CSSLabLike::equals(CSSStyleValue const& other) const
  14. {
  15. if (type() != other.type())
  16. return false;
  17. auto const& other_color = other.as_color();
  18. if (color_type() != other_color.color_type())
  19. return false;
  20. auto const& other_lab_like = verify_cast<CSSLabLike>(other_color);
  21. return m_properties == other_lab_like.m_properties;
  22. }
  23. Color CSSOKLab::to_color(Optional<Layout::NodeWithStyle const&>) const
  24. {
  25. auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1);
  26. auto const a_val = resolve_with_reference_value(m_properties.a, 0.4).value_or(0);
  27. auto const b_val = resolve_with_reference_value(m_properties.b, 0.4).value_or(0);
  28. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  29. return Color::from_oklab(l_val, a_val, b_val, alpha_val);
  30. }
  31. // https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch
  32. String CSSOKLab::to_string() const
  33. {
  34. // FIXME: Do this properly, taking unresolved calculated values into account.
  35. return serialize_a_srgb_value(to_color({}));
  36. }
  37. Color CSSLab::to_color(Optional<Layout::NodeWithStyle const&>) const
  38. {
  39. auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 100).value_or(0), 0, 100);
  40. auto const a_val = resolve_with_reference_value(m_properties.a, 125).value_or(0);
  41. auto const b_val = resolve_with_reference_value(m_properties.b, 125).value_or(0);
  42. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  43. return Color::from_lab(l_val, a_val, b_val, alpha_val);
  44. }
  45. // https://www.w3.org/TR/css-color-4/#serializing-lab-lch
  46. String CSSLab::to_string() const
  47. {
  48. // FIXME: Do this properly, taking unresolved calculated values into account.
  49. return serialize_a_srgb_value(to_color({}));
  50. }
  51. }