1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:58:12 +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(')');
}
// 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();
}
}