CSSColor.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 == "a98-rgb"sv)
  15. return CSSColorValue::ColorType::A98RGB;
  16. if (color_space == "srgb"sv)
  17. return CSSColorValue::ColorType::sRGB;
  18. if (color_space == "srgb-linear"sv)
  19. return CSSColorValue::ColorType::sRGBLinear;
  20. if (color_space == "xyz-d50"sv)
  21. return CSSColorValue::ColorType::XYZD50;
  22. if (color_space == "xyz"sv || color_space == "xyz-d65")
  23. return CSSColorValue::ColorType::XYZD65;
  24. VERIFY_NOT_REACHED();
  25. }
  26. }
  27. ValueComparingNonnullRefPtr<CSSColor> CSSColor::create(StringView color_space, ValueComparingNonnullRefPtr<CSSStyleValue> c1, ValueComparingNonnullRefPtr<CSSStyleValue> c2, ValueComparingNonnullRefPtr<CSSStyleValue> c3, ValueComparingRefPtr<CSSStyleValue> alpha)
  28. {
  29. VERIFY(any_of(s_supported_color_space, [=](auto supported) { return color_space == supported; }));
  30. if (!alpha)
  31. alpha = NumberStyleValue::create(1);
  32. return adopt_ref(*new (nothrow) CSSColor(color_type_from_string_view(color_space), move(c1), move(c2), move(c3), alpha.release_nonnull()));
  33. VERIFY_NOT_REACHED();
  34. }
  35. bool CSSColor::equals(CSSStyleValue const& other) const
  36. {
  37. if (type() != other.type())
  38. return false;
  39. auto const& other_color = other.as_color();
  40. if (color_type() != other_color.color_type())
  41. return false;
  42. auto const& other_lab_like = verify_cast<CSSColor>(other_color);
  43. return m_properties == other_lab_like.m_properties;
  44. }
  45. // https://www.w3.org/TR/css-color-4/#serializing-color-function-values
  46. String CSSColor::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. Color CSSColor::to_color(Optional<Layout::NodeWithStyle const&>) const
  52. {
  53. auto const c1 = resolve_with_reference_value(m_properties.channels[0], 1).value_or(0);
  54. auto const c2 = resolve_with_reference_value(m_properties.channels[1], 1).value_or(0);
  55. auto const c3 = resolve_with_reference_value(m_properties.channels[2], 1).value_or(0);
  56. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  57. if (color_type() == ColorType::A98RGB)
  58. return Color::from_a98rgb(c1, c2, c3, alpha_val);
  59. if (color_type() == ColorType::sRGB) {
  60. auto const to_u8 = [](float c) -> u8 { return round_to<u8>(clamp(255 * c, 0, 255)); };
  61. return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val));
  62. }
  63. if (color_type() == ColorType::sRGBLinear)
  64. return Color::from_linear_srgb(c1, c2, c3, alpha_val);
  65. if (color_type() == ColorType::XYZD50)
  66. return Color::from_xyz50(c1, c2, c3, alpha_val);
  67. if (color_type() == ColorType::XYZD65)
  68. return Color::from_xyz65(c1, c2, c3, alpha_val);
  69. VERIFY_NOT_REACHED();
  70. }
  71. } // Web::CSS