CSSOKLab.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSOKLab.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. Color CSSOKLab::to_color(Optional<Layout::NodeWithStyle const&>) const
  14. {
  15. auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1);
  16. auto const a_val = resolve_with_reference_value(m_properties.a, 0.4).value_or(0);
  17. auto const b_val = resolve_with_reference_value(m_properties.b, 0.4).value_or(0);
  18. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  19. return Color::from_oklab(l_val, a_val, b_val, alpha_val);
  20. }
  21. bool CSSOKLab::equals(CSSStyleValue const& other) const
  22. {
  23. if (type() != other.type())
  24. return false;
  25. auto const& other_color = other.as_color();
  26. if (color_type() != other_color.color_type())
  27. return false;
  28. auto const& other_oklab = verify_cast<CSSOKLab>(other_color);
  29. return m_properties == other_oklab.m_properties;
  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. }