CSSColor.cpp 3.5 KB

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