CSSHSL.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSHSL.h"
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. namespace Web::CSS {
  10. Color CSSHSL::to_color(Optional<Layout::NodeWithStyle const&>) const
  11. {
  12. auto const h_val = resolve_hue(m_properties.h).value_or(0);
  13. auto const s_val = resolve_with_reference_value(m_properties.s, 100.0).value_or(0);
  14. auto const l_val = resolve_with_reference_value(m_properties.l, 100.0).value_or(0);
  15. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  16. return Color::from_hsla(h_val, s_val / 100.0f, l_val / 100.0f, alpha_val);
  17. }
  18. bool CSSHSL::equals(CSSStyleValue const& other) const
  19. {
  20. if (type() != other.type())
  21. return false;
  22. auto const& other_color = other.as_color();
  23. if (color_type() != other_color.color_type())
  24. return false;
  25. auto const& other_hsl = verify_cast<CSSHSL>(other_color);
  26. return m_properties == other_hsl.m_properties;
  27. }
  28. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  29. String CSSHSL::to_string() const
  30. {
  31. // FIXME: Do this properly, taking unresolved calculated values into account.
  32. return serialize_a_srgb_value(to_color({}));
  33. }
  34. }