1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibWeb: Add serialize_a_srgb_value()

This moves the logic from ColorStyleValue::to_string() to a standalone
function.
This commit is contained in:
MacDue 2022-07-12 15:30:13 +01:00 committed by Sam Atkins
parent aa9f7cc597
commit 8603541e56
3 changed files with 23 additions and 7 deletions

View file

@ -117,6 +117,18 @@ void serialize_a_url(StringBuilder& builder, StringView url)
builder.append(')'); 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) String escape_a_character(u32 character)
{ {
StringBuilder builder; StringBuilder builder;
@ -152,4 +164,11 @@ String serialize_a_url(StringView url)
return builder.to_string(); return builder.to_string();
} }
String serialize_a_srgb_value(Color color)
{
StringBuilder builder;
serialize_a_srgb_value(builder, color);
return builder.to_string();
}
} }

View file

@ -9,6 +9,7 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibGfx/Color.h>
namespace Web::CSS { 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_an_identifier(StringBuilder&, StringView ident);
void serialize_a_string(StringBuilder&, StringView string); void serialize_a_string(StringBuilder&, StringView string);
void serialize_a_url(StringBuilder&, StringView url); 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(u32 character);
String escape_a_character_as_code_point(u32 character); String escape_a_character_as_code_point(u32 character);
String serialize_an_identifier(StringView ident); String serialize_an_identifier(StringView ident);
String serialize_a_string(StringView string); String serialize_a_string(StringView string);
String serialize_a_url(StringView url); String serialize_a_url(StringView url);
String serialize_a_srgb_value(Color color);
} }

View file

@ -1071,15 +1071,9 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartW
return value->resolve(layout_node, percentage_basis); return value->resolve(layout_node, percentage_basis);
} }
// https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
String ColorStyleValue::to_string() const String ColorStyleValue::to_string() const
{ {
// The serialized form is derived from the computed value and thus, uses either the rgb() or rgba() form return serialize_a_srgb_value(m_color);
// (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);
} }
bool ColorStyleValue::equals(StyleValue const& other) const bool ColorStyleValue::equals(StyleValue const& other) const