CSSColor.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. StringView string_view_from_color_type(CSSColorValue::ColorType color_type)
  33. {
  34. if (color_type == CSSColorValue::ColorType::A98RGB)
  35. return "a98-rgb"sv;
  36. if (color_type == CSSColorValue::ColorType::DisplayP3)
  37. return "display-p3"sv;
  38. if (color_type == CSSColorValue::ColorType::sRGB)
  39. return "srgb"sv;
  40. if (color_type == CSSColorValue::ColorType::sRGBLinear)
  41. return "srgb-linear"sv;
  42. if (color_type == CSSColorValue::ColorType::ProPhotoRGB)
  43. return "prophoto-rgb"sv;
  44. if (color_type == CSSColorValue::ColorType::Rec2020)
  45. return "rec2020"sv;
  46. if (color_type == CSSColorValue::ColorType::XYZD50)
  47. return "xyz-d50"sv;
  48. if (color_type == CSSColorValue::ColorType::XYZD65)
  49. return "xyz"sv;
  50. VERIFY_NOT_REACHED();
  51. }
  52. }
  53. ValueComparingNonnullRefPtr<CSSColor> CSSColor::create(StringView color_space, ValueComparingNonnullRefPtr<CSSStyleValue> c1, ValueComparingNonnullRefPtr<CSSStyleValue> c2, ValueComparingNonnullRefPtr<CSSStyleValue> c3, ValueComparingRefPtr<CSSStyleValue> alpha)
  54. {
  55. VERIFY(any_of(s_supported_color_space, [=](auto supported) { return color_space == supported; }));
  56. if (!alpha)
  57. alpha = NumberStyleValue::create(1);
  58. return adopt_ref(*new (nothrow) CSSColor(color_type_from_string_view(color_space), move(c1), move(c2), move(c3), alpha.release_nonnull()));
  59. VERIFY_NOT_REACHED();
  60. }
  61. bool CSSColor::equals(CSSStyleValue const& other) const
  62. {
  63. if (type() != other.type())
  64. return false;
  65. auto const& other_color = other.as_color();
  66. if (color_type() != other_color.color_type())
  67. return false;
  68. auto const& other_lab_like = verify_cast<CSSColor>(other_color);
  69. return m_properties == other_lab_like.m_properties;
  70. }
  71. CSSColor::Resolved CSSColor::resolve_properties() const
  72. {
  73. float const c1 = resolve_with_reference_value(m_properties.channels[0], 1).value_or(0);
  74. float const c2 = resolve_with_reference_value(m_properties.channels[1], 1).value_or(0);
  75. float const c3 = resolve_with_reference_value(m_properties.channels[2], 1).value_or(0);
  76. float const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
  77. return { .channels = { c1, c2, c3 }, .alpha = alpha_val };
  78. }
  79. // https://www.w3.org/TR/css-color-4/#serializing-color-function-values
  80. String CSSColor::to_string(SerializationMode) const
  81. {
  82. // FIXME: Do this properly, taking unresolved calculated values into account.
  83. auto resolved = resolve_properties();
  84. if (resolved.alpha == 1) {
  85. return MUST(String::formatted("color({} {} {} {})",
  86. string_view_from_color_type(m_color_type),
  87. resolved.channels[0],
  88. resolved.channels[1],
  89. resolved.channels[2]));
  90. }
  91. return MUST(String::formatted("color({} {} {} {} / {})",
  92. string_view_from_color_type(m_color_type),
  93. resolved.channels[0],
  94. resolved.channels[1],
  95. resolved.channels[2],
  96. resolved.alpha));
  97. }
  98. Color CSSColor::to_color(Optional<Layout::NodeWithStyle const&>) const
  99. {
  100. auto [channels, alpha_val] = resolve_properties();
  101. auto c1 = channels[0];
  102. auto c2 = channels[1];
  103. auto c3 = channels[2];
  104. if (color_type() == ColorType::A98RGB)
  105. return Color::from_a98rgb(c1, c2, c3, alpha_val);
  106. if (color_type() == ColorType::DisplayP3)
  107. return Color::from_display_p3(c1, c2, c3, alpha_val);
  108. if (color_type() == ColorType::sRGB) {
  109. auto const to_u8 = [](float c) -> u8 { return round_to<u8>(clamp(255 * c, 0, 255)); };
  110. return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val));
  111. }
  112. if (color_type() == ColorType::sRGBLinear)
  113. return Color::from_linear_srgb(c1, c2, c3, alpha_val);
  114. if (color_type() == ColorType::ProPhotoRGB)
  115. return Color::from_pro_photo_rgb(c1, c2, c3, alpha_val);
  116. if (color_type() == ColorType::Rec2020)
  117. return Color::from_rec2020(c1, c2, c3, alpha_val);
  118. if (color_type() == ColorType::XYZD50)
  119. return Color::from_xyz50(c1, c2, c3, alpha_val);
  120. if (color_type() == ColorType::XYZD65)
  121. return Color::from_xyz65(c1, c2, c3, alpha_val);
  122. VERIFY_NOT_REACHED();
  123. }
  124. } // Web::CSS