Commit a3c458e0 authored by Anders Hartvoll Ruud's avatar Anders Hartvoll Ruud Committed by Commit Bot

Move Color::SerializedAsCSSComponentValue to CSSColorValue.

This special serialization behavior belongs in the css directory. Also, in
a subsequent CL (for css-properties-values-api), it will be necessary to
expose the alpha rounding part of the serialization, and I don't want to
add this function to platform/color.h.

Change-Id: I4512e695016b6495c6b9197b521cda6431642962
Reviewed-on: https://chromium-review.googlesource.com/c/1293614Reviewed-by: default avatarChris Harrelson <chrishtr@chromium.org>
Commit-Queue: Anders Ruud <andruud@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602404}
parent e2569db7
......@@ -5,6 +5,7 @@
#include "third_party/blink/renderer/core/css/css_color_value.h"
#include "third_party/blink/renderer/core/css/css_value_pool.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
namespace cssvalue {
......@@ -26,5 +27,39 @@ CSSColorValue* CSSColorValue::Create(RGBA32 color) {
return entry.stored_value->value;
}
String CSSColorValue::SerializeAsCSSComponentValue(Color color) {
StringBuilder result;
result.ReserveCapacity(32);
bool color_has_alpha = color.HasAlpha();
if (color_has_alpha)
result.Append("rgba(");
else
result.Append("rgb(");
result.AppendNumber(static_cast<unsigned char>(color.Red()));
result.Append(", ");
result.AppendNumber(static_cast<unsigned char>(color.Green()));
result.Append(", ");
result.AppendNumber(static_cast<unsigned char>(color.Blue()));
if (color_has_alpha) {
result.Append(", ");
// See <alphavalue> section in
// https://drafts.csswg.org/cssom/#serializing-css-values
int alphavalue = color.Alpha();
float rounded = round(alphavalue * 100 / 255.0f) / 100;
if (round(rounded * 255) == alphavalue) {
result.AppendNumber(rounded, 2);
} else {
rounded = round(alphavalue * 1000 / 255.0f) / 1000;
result.AppendNumber(rounded, 3);
}
}
result.Append(')');
return result.ToString();
}
} // namespace cssvalue
} // namespace blink
......@@ -21,9 +21,7 @@ class CSSColorValue : public CSSValue {
// TODO(sashab): Make this create() method take a Color instead.
static CSSColorValue* Create(RGBA32 color);
String CustomCSSText() const {
return color_.SerializedAsCSSComponentValue();
}
String CustomCSSText() const { return SerializeAsCSSComponentValue(color_); }
Color Value() const { return color_; }
......@@ -35,6 +33,10 @@ class CSSColorValue : public CSSValue {
CSSValue::TraceAfterDispatch(visitor);
}
// Returns the color serialized according to CSSOM:
// https://drafts.csswg.org/cssom/#serialize-a-css-component-value
static String SerializeAsCSSComponentValue(Color color);
private:
friend class ::blink::CSSValuePool;
......
......@@ -2356,7 +2356,7 @@ Response InspectorCSSAgent::getBackgroundColors(
*background_colors = protocol::Array<String>::create();
for (auto color : colors) {
background_colors->fromJust()->addItem(
color.SerializedAsCSSComponentValue());
cssvalue::CSSColorValue::SerializeAsCSSComponentValue(color));
}
CSSComputedStyleDeclaration* computed_style_info =
......
......@@ -19,6 +19,7 @@
#include "third_party/blink/renderer/core/svg/svg_animated_color.h"
#include "third_party/blink/renderer/core/css/css_color_value.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/svg/color_distance.h"
......@@ -36,7 +37,8 @@ SVGColorProperty::SVGColorProperty(const String& color_string)
String SVGColorProperty::ValueAsString() const {
return style_color_.IsCurrentColor()
? "currentColor"
: style_color_.GetColor().SerializedAsCSSComponentValue();
: cssvalue::CSSColorValue::SerializeAsCSSComponentValue(
style_color_.GetColor());
}
SVGPropertyBase* SVGColorProperty::CloneForAnimation(const String&) const {
......
......@@ -225,40 +225,6 @@ bool Color::SetFromString(const String& name) {
return ParseHexColor(name.Characters16() + 1, name.length() - 1, color_);
}
String Color::SerializedAsCSSComponentValue() const {
StringBuilder result;
result.ReserveCapacity(32);
bool color_has_alpha = HasAlpha();
if (color_has_alpha)
result.Append("rgba(");
else
result.Append("rgb(");
result.AppendNumber(static_cast<unsigned char>(Red()));
result.Append(", ");
result.AppendNumber(static_cast<unsigned char>(Green()));
result.Append(", ");
result.AppendNumber(static_cast<unsigned char>(Blue()));
if (color_has_alpha) {
result.Append(", ");
// See <alphavalue> section in
// https://drafts.csswg.org/cssom/#serializing-css-values
int alphavalue = Alpha();
float rounded = round(alphavalue * 100 / 255.0f) / 100;
if (round(rounded * 255) == alphavalue) {
result.AppendNumber(rounded, 2);
} else {
rounded = round(alphavalue * 1000 / 255.0f) / 1000;
result.AppendNumber(rounded, 3);
}
}
result.Append(')');
return result.ToString();
}
String Color::Serialized() const {
if (!HasAlpha()) {
StringBuilder builder;
......
......@@ -97,10 +97,6 @@ class PLATFORM_EXPORT Color {
// http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
String Serialized() const;
// Returns the color serialized according to CSSOM:
// https://drafts.csswg.org/cssom/#serialize-a-css-component-value
String SerializedAsCSSComponentValue() const;
// Returns the color serialized as either #RRGGBB or #RRGGBBAA. The latter
// format is not a valid CSS color, and should only be seen in DRT dumps.
String NameForLayoutTreeAsText() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment