CSSColor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 == "display-p3"sv)
  17. return CSSColorValue::ColorType::DisplayP3;
  18. if (color_space == "srgb"sv)
  19. return CSSColorValue::ColorType::sRGB;
  20. if (color_space == "srgb-linear"sv)
  21. return CSSColorValue::ColorType::sRGBLinear;
  22. if (color_space == "prophoto-rgb"sv)
  23. return CSSColorValue::ColorType::ProPhotoRGB;
  24. if (color_space == "rec2020"sv)
  25. return CSSColorValue::ColorType::Rec2020;
  26. if (color_space == "xyz-d50"sv)
  27. return CSSColorValue::ColorType::XYZD50;
  28. if (color_space == "xyz"sv || color_space == "xyz-d65")
  29. return CSSColorValue::ColorType::XYZD65;
  30. VERIFY_NOT_REACHED();
  31. }
  32. }
  33. ValueComparingNonnullRefPtr<CSSColor> CSSColor::create(StringView color_space, ValueComparingNonnullRefPtr<CSSStyleValue> c1, ValueComparingNonnullRefPtr<CSSStyleValue> c2, ValueComparingNonnullRefPtr<CSSStyleValue> c3, ValueComparingRefPtr<CSSStyleValue> alpha)
  34. {
  35. VERIFY(any_of(s_supported_color_space, [=](auto supported) { return color_space == supported; }));
  36. if (!alpha)
  37. alpha = NumberStyleValue::create(1);
  38. return adopt_ref(*new (nothrow) CSSColor(color_type_from_string_view(color_space), move(c1), move(c2), move(c3), alpha.release_nonnull()));
  39. VERIFY_NOT_REACHED();
  40. }
  41. bool CSSColor::equals(CSSStyleValue const& other) const
  42. {
  43. if (type() != other.type())
  44. return false;
  45. auto const& other_color = other.as_color();
  46. if (color_type() != other_color.color_type())
  47. return false;
  48. auto const& other_lab_like = verify_cast<CSSColor>(other_color);
  49. return m_properties == other_lab_like.m_properties;
  50. }
  51. // https://www.w3.org/TR/css-color-4/#serializing-color-function-values
  52. String CSSColor::to_string(SerializationMode) const
  53. {
  54. // FIXME: Do this properly, taking unresolved calculated values into account.
  55. return serialize_a_srgb_value(to_color({}));
  56. }
  57. Color CSSColor::to_color(Optional<Layout::NodeWithStyle const&>) const
  58. {
  59. auto const c1 = resolve_with_reference_value(m_properties.channels[0], 1).value_or(0);
  60. auto const c2 = resolve_with_reference_value(m_properties.channels[1], 1).value_or(0);
  61. auto const c3 = resolve_with_reference_value(m_properties.channels[2], 1).value_or(0);
  62. auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  63. if (color_type() == ColorType::A98RGB)
  64. return Color::from_a98rgb(c1, c2, c3, alpha_val);
  65. if (color_type() == ColorType::DisplayP3)
  66. return Color::from_display_p3(c1, c2, c3, alpha_val);
  67. if (color_type() == ColorType::sRGB) {
  68. auto const to_u8 = [](float c) -> u8 { return round_to<u8>(clamp(255 * c, 0, 255)); };
  69. return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val));
  70. }
  71. if (color_type() == ColorType::sRGBLinear)
  72. return Color::from_linear_srgb(c1, c2, c3, alpha_val);
  73. if (color_type() == ColorType::ProPhotoRGB)
  74. return Color::from_pro_photo_rgb(c1, c2, c3, alpha_val);
  75. if (color_type() == ColorType::Rec2020)
  76. return Color::from_rec2020(c1, c2, c3, alpha_val);
  77. if (color_type() == ColorType::XYZD50)
  78. return Color::from_xyz50(c1, c2, c3, alpha_val);
  79. if (color_type() == ColorType::XYZD65)
  80. return Color::from_xyz65(c1, c2, c3, alpha_val);
  81. VERIFY_NOT_REACHED();
  82. }
  83. } // Web::CSS