diff options
author | MacDue <macdue@dueutil.tech> | 2022-07-12 15:30:13 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-07-17 20:11:38 +0100 |
commit | 8603541e5614384b23bfdb174a4096a6dac43dcf (patch) | |
tree | c0213412cf30b15df746cc25e8602649a50db904 /Userland | |
parent | aa9f7cc597d7c06cf2ae4c202612e63dd2bb5d4e (diff) | |
download | serenity-8603541e5614384b23bfdb174a4096a6dac43dcf.zip |
LibWeb: Add serialize_a_srgb_value()
This moves the logic from ColorStyleValue::to_string() to a standalone
function.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Serialize.cpp | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Serialize.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 8 |
3 files changed, 23 insertions, 7 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Serialize.cpp b/Userland/Libraries/LibWeb/CSS/Serialize.cpp index dfb1e34843..29a2c614a9 100644 --- a/Userland/Libraries/LibWeb/CSS/Serialize.cpp +++ b/Userland/Libraries/LibWeb/CSS/Serialize.cpp @@ -117,6 +117,18 @@ void serialize_a_url(StringBuilder& builder, StringView url) builder.append(')'); } +// https://www.w3.org/TR/css-color-4/#serializing-sRGB-values +void serialize_a_srgb_value(StringBuilder& builder, Color color) +{ + // The serialized form is derived from the computed value and thus, uses either the rgb() or rgba() form + // (depending on whether the alpha is exactly 1, or not), with lowercase letters for the function name. + // NOTE: Since we use Gfx::Color, having an "alpha of 1" means its value is 255. + if (color.alpha() == 255) + builder.appendff("rgb({}, {}, {})"sv, color.red(), color.green(), color.blue()); + else + builder.appendff("rgba({}, {}, {}, {})"sv, color.red(), color.green(), color.blue(), (float)(color.alpha()) / 255.0f); +} + String escape_a_character(u32 character) { StringBuilder builder; @@ -152,4 +164,11 @@ String serialize_a_url(StringView url) return builder.to_string(); } +String serialize_a_srgb_value(Color color) +{ + StringBuilder builder; + serialize_a_srgb_value(builder, color); + return builder.to_string(); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/Serialize.h b/Userland/Libraries/LibWeb/CSS/Serialize.h index 15ca58151b..3988c2ba61 100644 --- a/Userland/Libraries/LibWeb/CSS/Serialize.h +++ b/Userland/Libraries/LibWeb/CSS/Serialize.h @@ -9,6 +9,7 @@ #include <AK/String.h> #include <AK/StringBuilder.h> #include <AK/StringView.h> +#include <LibGfx/Color.h> namespace Web::CSS { @@ -17,11 +18,13 @@ void escape_a_character_as_code_point(StringBuilder&, u32 character); void serialize_an_identifier(StringBuilder&, StringView ident); void serialize_a_string(StringBuilder&, StringView string); void serialize_a_url(StringBuilder&, StringView url); +void serialize_a_srgb_value(StringBuilder&, Color color); String escape_a_character(u32 character); String escape_a_character_as_code_point(u32 character); String serialize_an_identifier(StringView ident); String serialize_a_string(StringView string); String serialize_a_url(StringView url); +String serialize_a_srgb_value(Color color); } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index d5f030dd34..63221f75d4 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -1071,15 +1071,9 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartW return value->resolve(layout_node, percentage_basis); } -// https://www.w3.org/TR/css-color-4/#serializing-sRGB-values String ColorStyleValue::to_string() const { - // The serialized form is derived from the computed value and thus, uses either the rgb() or rgba() form - // (depending on whether the alpha is exactly 1, or not), with lowercase letters for the function name. - // NOTE: Since we use Gfx::Color, having an "alpha of 1" means its value is 255. - if (m_color.alpha() == 255) - return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue()); - return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f); + return serialize_a_srgb_value(m_color); } bool ColorStyleValue::equals(StyleValue const& other) const |