CSSRGB.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CSSRGB.h"
  7. #include <AK/TypeCasts.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. #include <LibWeb/CSS/StyleValues/CSSMathValue.h>
  10. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  12. namespace Web::CSS {
  13. Color CSSRGB::to_color(Optional<Layout::NodeWithStyle const&>) const
  14. {
  15. auto resolve_rgb_to_u8 = [](CSSStyleValue const& style_value) -> Optional<u8> {
  16. // <number> | <percentage> | none
  17. auto normalized = [](double number) {
  18. if (isnan(number))
  19. number = 0;
  20. return llround(clamp(number, 0.0, 255.0));
  21. };
  22. if (style_value.is_number())
  23. return normalized(style_value.as_number().number());
  24. if (style_value.is_percentage())
  25. return normalized(style_value.as_percentage().value() * 255 / 100);
  26. if (style_value.is_math()) {
  27. auto const& calculated = style_value.as_math();
  28. if (calculated.resolves_to_number())
  29. return normalized(calculated.resolve_number().value());
  30. if (calculated.resolves_to_percentage())
  31. return normalized(calculated.resolve_percentage().value().value() * 255 / 100);
  32. }
  33. if (style_value.is_keyword() && style_value.to_keyword() == Keyword::None)
  34. return 0;
  35. return {};
  36. };
  37. auto resolve_alpha_to_u8 = [](CSSStyleValue const& style_value) -> Optional<u8> {
  38. auto alpha_0_1 = resolve_alpha(style_value);
  39. if (alpha_0_1.has_value())
  40. return llround(clamp(alpha_0_1.value() * 255.0f, 0.0f, 255.0f));
  41. return {};
  42. };
  43. u8 const r_val = resolve_rgb_to_u8(m_properties.r).value_or(0);
  44. u8 const g_val = resolve_rgb_to_u8(m_properties.g).value_or(0);
  45. u8 const b_val = resolve_rgb_to_u8(m_properties.b).value_or(0);
  46. u8 const alpha_val = resolve_alpha_to_u8(m_properties.alpha).value_or(255);
  47. return Color(r_val, g_val, b_val, alpha_val);
  48. }
  49. bool CSSRGB::equals(CSSStyleValue const& other) const
  50. {
  51. if (type() != other.type())
  52. return false;
  53. auto const& other_color = other.as_color();
  54. if (color_type() != other_color.color_type())
  55. return false;
  56. auto const& other_rgb = verify_cast<CSSRGB>(other_color);
  57. return m_properties == other_rgb.m_properties;
  58. }
  59. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  60. String CSSRGB::to_string(SerializationMode mode) const
  61. {
  62. // FIXME: Do this properly, taking unresolved calculated values into account.
  63. if (mode != SerializationMode::ResolvedValue && m_properties.name.has_value())
  64. return m_properties.name.value().to_string().to_ascii_lowercase();
  65. return serialize_a_srgb_value(to_color({}));
  66. }
  67. }