Skip to content

Commit

Permalink
Cleaning color.cc to chromium style.
Browse files Browse the repository at this point in the history
Moving static variables and functions to anonymous namespace.

Bug: None

Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I91789dd354da254dd13410425792321a2608f19c
Reviewed-on: https://chromium-review.googlesource.com/1049587
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Prashant Nevase <prashant.n@samsung.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569238}
  • Loading branch information
yuvanesh-srib authored and Commit Bot committed Jun 21, 2018
1 parent 7dcf98f commit 7d72512
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 83 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ Yupei Wang <perryuwang@tencent.com>
Yura Yaroshevich <yura.yaroshevich@gmail.com>
Yuri Gorobets <yuri.gorobets@gmail.com>
Yuriy Taraday <yorik.sar@gmail.com>
Yuvanesh Natarajan <yuvanesh.n1@samsung.com>
Zeno Albisser <zeno.albisser@digia.com>
Zeqin Chen <talonchen@tencent.com>
Zhaoze Zhou <zhaoze.zhou@partner.samsung.com>
Expand Down
170 changes: 87 additions & 83 deletions third_party/blink/renderer/platform/graphics/color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,24 @@ const RGBA32 Color::kLightGray;
const RGBA32 Color::kTransparent;
#endif

static const RGBA32 kLightenedBlack = 0xFF545454;
static const RGBA32 kDarkenedWhite = 0xFFABABAB;
namespace {

RGBA32 MakeRGB(int r, int g, int b) {
return 0xFF000000 | clampTo(r, 0, 255) << 16 | clampTo(g, 0, 255) << 8 |
clampTo(b, 0, 255);
}

RGBA32 MakeRGBA(int r, int g, int b, int a) {
return clampTo(a, 0, 255) << 24 | clampTo(r, 0, 255) << 16 |
clampTo(g, 0, 255) << 8 | clampTo(b, 0, 255);
}
const RGBA32 kLightenedBlack = 0xFF545454;
const RGBA32 kDarkenedWhite = 0xFFABABAB;

static int ColorFloatToRGBAByte(float f) {
return clampTo(static_cast<int>(lroundf(255.0f * f)), 0, 255);
}
const int kCStartAlpha = 153; // 60%
const int kCEndAlpha = 204; // 80%;
const int kCAlphaIncrement = 17; // Increments in between.

RGBA32 MakeRGBA32FromFloats(float r, float g, float b, float a) {
return ColorFloatToRGBAByte(a) << 24 | ColorFloatToRGBAByte(r) << 16 |
ColorFloatToRGBAByte(g) << 8 | ColorFloatToRGBAByte(b);
int BlendComponent(int c, int a) {
// We use white.
float alpha = a / 255.0f;
int white_blend = 255 - a;
c -= white_blend;
return static_cast<int>(c / alpha);
}

static double CalcHue(double temp1, double temp2, double hue_val) {
double CalcHue(double temp1, double temp2, double hue_val) {
if (hue_val < 0.0)
hue_val += 6.0;
else if (hue_val >= 6.0)
Expand All @@ -85,48 +80,15 @@ static double CalcHue(double temp1, double temp2, double hue_val) {
return temp1;
}

// Explanation of this algorithm can be found in the CSS Color 4 Module
// specification at https://drafts.csswg.org/css-color-4/#hsl-to-rgb with
// further explanation available at http://en.wikipedia.org/wiki/HSL_color_space

// Hue is in the range of 0 to 6.0, the remainder are in the range 0 to 1.0
RGBA32 MakeRGBAFromHSLA(double hue,
double saturation,
double lightness,
double alpha) {
const double scale_factor = 255.0;

if (!saturation) {
int grey_value = static_cast<int>(round(lightness * scale_factor));
return MakeRGBA(grey_value, grey_value, grey_value,
static_cast<int>(round(alpha * scale_factor)));
}

double temp2 = lightness <= 0.5
? lightness * (1.0 + saturation)
: lightness + saturation - lightness * saturation;
double temp1 = 2.0 * lightness - temp2;

return MakeRGBA(
static_cast<int>(round(CalcHue(temp1, temp2, hue + 2.0) * scale_factor)),
static_cast<int>(round(CalcHue(temp1, temp2, hue) * scale_factor)),
static_cast<int>(round(CalcHue(temp1, temp2, hue - 2.0) * scale_factor)),
static_cast<int>(round(alpha * scale_factor)));
}

RGBA32 MakeRGBAFromCMYKA(float c, float m, float y, float k, float a) {
double colors = 1 - k;
int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));
int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));
int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));
return MakeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));
int ColorFloatToRGBAByte(float f) {
return clampTo(static_cast<int>(lroundf(255.0f * f)), 0, 255);
}

// originally moved here from the CSS parser
template <typename CharacterType>
static inline bool ParseHexColorInternal(const CharacterType* name,
unsigned length,
RGBA32& rgb) {
inline bool ParseHexColorInternal(const CharacterType* name,
unsigned length,
RGBA32& rgb) {
if (length != 3 && length != 4 && length != 6 && length != 8)
return false;
if ((length == 8 || length == 4) &&
Expand Down Expand Up @@ -163,6 +125,75 @@ static inline bool ParseHexColorInternal(const CharacterType* name,
return true;
}

inline const NamedColor* FindNamedColor(const String& name) {
char buffer[64]; // easily big enough for the longest color name
unsigned length = name.length();
if (length > sizeof(buffer) - 1)
return nullptr;
for (unsigned i = 0; i < length; ++i) {
UChar c = name[i];
if (!c || c > 0x7F)
return nullptr;
buffer[i] = ToASCIILower(static_cast<char>(c));
}
buffer[length] = '\0';
return FindColor(buffer, length);
}

} // namespace

RGBA32 MakeRGB(int r, int g, int b) {
return 0xFF000000 | clampTo(r, 0, 255) << 16 | clampTo(g, 0, 255) << 8 |
clampTo(b, 0, 255);
}

RGBA32 MakeRGBA(int r, int g, int b, int a) {
return clampTo(a, 0, 255) << 24 | clampTo(r, 0, 255) << 16 |
clampTo(g, 0, 255) << 8 | clampTo(b, 0, 255);
}

RGBA32 MakeRGBA32FromFloats(float r, float g, float b, float a) {
return ColorFloatToRGBAByte(a) << 24 | ColorFloatToRGBAByte(r) << 16 |
ColorFloatToRGBAByte(g) << 8 | ColorFloatToRGBAByte(b);
}

// Explanation of this algorithm can be found in the CSS Color 4 Module
// specification at https://drafts.csswg.org/css-color-4/#hsl-to-rgb with
// further explanation available at http://en.wikipedia.org/wiki/HSL_color_space

// Hue is in the range of 0 to 6.0, the remainder are in the range 0 to 1.0
RGBA32 MakeRGBAFromHSLA(double hue,
double saturation,
double lightness,
double alpha) {
const double scale_factor = 255.0;

if (!saturation) {
int grey_value = static_cast<int>(round(lightness * scale_factor));
return MakeRGBA(grey_value, grey_value, grey_value,
static_cast<int>(round(alpha * scale_factor)));
}

double temp2 = lightness <= 0.5
? lightness * (1.0 + saturation)
: lightness + saturation - lightness * saturation;
double temp1 = 2.0 * lightness - temp2;

return MakeRGBA(
static_cast<int>(round(CalcHue(temp1, temp2, hue + 2.0) * scale_factor)),
static_cast<int>(round(CalcHue(temp1, temp2, hue) * scale_factor)),
static_cast<int>(round(CalcHue(temp1, temp2, hue - 2.0) * scale_factor)),
static_cast<int>(round(alpha * scale_factor)));
}

RGBA32 MakeRGBAFromCMYKA(float c, float m, float y, float k, float a) {
double colors = 1 - k;
int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));
int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));
int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));
return MakeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));
}

bool Color::ParseHexColor(const LChar* name, unsigned length, RGBA32& rgb) {
return ParseHexColorInternal(name, length, rgb);
}
Expand Down Expand Up @@ -266,21 +297,6 @@ String Color::NameForLayoutTreeAsText() const {
return String::Format("#%02X%02X%02X", Red(), Green(), Blue());
}

static inline const NamedColor* FindNamedColor(const String& name) {
char buffer[64]; // easily big enough for the longest color name
unsigned length = name.length();
if (length > sizeof(buffer) - 1)
return nullptr;
for (unsigned i = 0; i < length; ++i) {
UChar c = name[i];
if (!c || c > 0x7F)
return nullptr;
buffer[i] = ToASCIILower(static_cast<char>(c));
}
buffer[length] = '\0';
return FindColor(buffer, length);
}

bool Color::SetNamedColor(const String& name) {
const NamedColor* found_color = FindNamedColor(name);
color_ = found_color ? found_color->argb_value : 0;
Expand Down Expand Up @@ -334,18 +350,6 @@ Color Color::CombineWithAlpha(float other_alpha) const {
return rgb_only | ColorFloatToRGBAByte(override_alpha) << 24;
}

static int BlendComponent(int c, int a) {
// We use white.
float alpha = a / 255.0f;
int white_blend = 255 - a;
c -= white_blend;
return static_cast<int>(c / alpha);
}

const int kCStartAlpha = 153; // 60%
const int kCEndAlpha = 204; // 80%;
const int kCAlphaIncrement = 17; // Increments in between.

Color Color::Blend(const Color& source) const {
if (!Alpha() || !source.HasAlpha())
return source;
Expand Down

0 comments on commit 7d72512

Please sign in to comment.