CSSColor.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2024, Lucas Chollet <lucas.chollet@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSColor.h"
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  10. namespace Web::CSS {
  11. namespace {
  12. CSSColorValue::ColorType color_type_from_string_view(StringView color_space)
  13. {
  14. if (color_space == "srgb"sv)
  15. return CSSColorValue::ColorType::sRGB;
  16. if (color_space == "xyz-d50"sv)
  17. return CSSColorValue::ColorType::XYZD50;
  18. if (color_space == "xyz"sv || color_space == "xyz-d65")
  19. return CSSColorValue::ColorType::XYZD65;
  20. VERIFY_NOT_REACHED();
  21. }
  22. }
  23. ValueComparingNonnullRefPtr<CSSColor> CSSColor::create(StringView color_space, ValueComparingNonnullRefPtr<CSSStyleValue> c1, ValueComparingNonnullRefPtr<CSSStyleValue> c2, ValueComparingNonnullRefPtr<CSSStyleValue> c3, ValueComparingRefPtr<CSSStyleValue> alpha)
  24. {
  25. VERIFY(any_of(s_supported_color_space, [=](auto supported) { return color_space == supported; }));
  26. if (!alpha)
  27. alpha = NumberStyleValue::create(1);
  28. return adopt_ref(*new (nothrow) CSSColor(color_type_from_string_view(color_space), move(c1), move(c2), move(c3), alpha.release_nonnull()));
  29. VERIFY_NOT_REACHED();
  30. }
  31. bool CSSColor::equals(CSSStyleValue const& other) const
  32. {
  33. if (type() != other.type())
  34. return false;
  35. auto const& other_color = other.as_color();
  36. if (color_type() != other_color.color_type())
  37. return false;
  38. auto const& other_lab_like = verify_cast<CSSColor>(other_color);
  39. return m_properties == other_lab_like.m_properties;
  40. }
  41. // https://www.w3.org/TR/css-color-4/#serializing-color-function-values
  42. String CSSColor::to_string() const
  43. {
  44. // FIXME: Do this properly, taking unresolved calculated values into account.
  45. return serialize_a_srgb_value(to_color({}));
  46. }
  47. Color CSSColor::to_color(Optional<Layout::NodeWithStyle const&>) const
  48. {
  49. auto const c1 = resolve_with_reference_value(m_properties.channels[0], 100).value_or(0);
  50. auto const c2 = resolve_with_reference_value(m_properties.channels[1], 100).value_or(0);
  51. auto const c3 = resolve_with_reference_value(m_properties.channels[2], 100).value_or(0);
  52. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  53. if (color_type() == ColorType::sRGB) {
  54. auto const to_u8 = [](float c) -> u8 { return round_to<u8>(clamp(255 * c, 0, 255)); };
  55. return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val));
  56. }
  57. if (color_type() == ColorType::XYZD50)
  58. return Color::from_xyz50(c1, c2, c3, alpha_val);
  59. if (color_type() == ColorType::XYZD65)
  60. return Color::from_xyz65(c1, c2, c3, alpha_val);
  61. VERIFY_NOT_REACHED();
  62. }
  63. } // Web::CSS