2
* Copyright 2010 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Copyright (C) 2000 and onwards Google, Inc.
20
// .h for the HtmlColor class
21
// HtmlColor provides 'IsSimilar' for comparing HTML colors
22
// of different representations ( "#xxxxxx" or color names such as "white").
23
// Before doing comparison, check 'IsDefined' first, it's because
24
// that not all given HTML color strings are valid.
26
#ifndef WEBUTIL_HTML_HTMLCOLOR_H_
27
#define WEBUTIL_HTML_HTMLCOLOR_H_
31
#include "string_using.h"
32
#include "strings/stringpiece.h"
40
// a colorstr is well defined if it is "#xxxxxx" ('x' is a hexdigit)
41
// or it's a known color name such as "black".
42
// 0: the RGB value is good!
43
// 1: bad (name) value, caused by bad color name
44
// 2: bad (hex) value, caused by bad hex string value
45
// -- the browser (Netscape Communicator 4.75, linux-2.2.14,
46
// shows that the color displayed is sometimes 'black' under case '2'.
47
unsigned char is_bad_value_;
48
static const unsigned char kGoodColorValue = 0x00;
49
static const unsigned char kBadColorName = 0x01;
50
static const unsigned char kBadColorHex = 0x02;
52
void SetBadNameValue() {
54
is_bad_value_ = kBadColorName;
56
void SetBadHexValue() {
58
is_bad_value_ = kBadColorHex;
60
void SetDefaultValue() {
62
is_bad_value_ = kGoodColorValue;
72
// These methods also accept a CSS shorthand string "#xyz" for convenience.
73
// "#xyz" is expanded to "#xxyyzz" before processing.
74
explicit HtmlColor(StringPiece colorstr);
75
HtmlColor(const char* colorstr, int colorstrlen);
76
HtmlColor(unsigned char r, unsigned char g, unsigned char b);
78
bool IsDefined() const {
79
return is_bad_value_ == 0;
82
bool IsSimilar(const HtmlColor &color, int level) const {
83
if (!IsDefined() || !color.IsDefined())
86
if ( (abs(static_cast<int>(r_) - static_cast<int>(color.r_)) <= level) &&
87
(abs(static_cast<int>(g_) - static_cast<int>(color.g_)) <= level) &&
88
(abs(static_cast<int>(b_) - static_cast<int>(color.b_)) <= level)
94
// Compares color similarity in HSL (Hue, Saturation, Lightness) space.
95
// This is assumed to be more accurate based on human perception.
96
// Note the difference level is a float number and it may vary from 0.0 to
97
// 1.0, inclusive. A suggested value for level is 0.02.
98
// WARNING: this is more expensive than IsSimilar() as it involves float
99
// arithmetic and cosine operations.
100
// TODO(yian): may need to disintegrate it into a separate HSL class.
101
bool IsSimilarInHSL(const HtmlColor &color, double level) const;
103
// return the luminance (0-255) of the color.
104
// this corresponds to a human's perception of the color's brightness
105
int Luminance() const;
107
// Lighten or darken the color by a given factor (between 0 and 1)
108
// Lightening with factor 1.0 => white
109
// Darkening with factor 1.0 => black
110
void Lighten(float factor);
111
void Darken(float factor);
113
// Desaturate the color (0.0 = no change, 1.0 = equivalent shade of gray)
114
void Desaturate(float factor);
116
// Blend the color with a second color by a certain factor between 0 and 1
117
// 1.0 => original color
118
// 0.0 => other color
119
void BlendWithColor(float factor, const HtmlColor& c);
121
string ToString() const;
123
// hexstr is in form of "xxxxxx"
124
void SetValueFromHexStr(StringPiece hexstr);
126
// either a color name or a hex string "#xxxxxx"
127
// This method also accepts a CSS shorthand string "#xyz" for convenience.
128
// "#xyz" is expanded to "#xxyyzz" before processing.
129
void SetValueFromStr(StringPiece str);
131
// Set the html color object from rgb values
132
void SetValueFromRGB(unsigned char r, unsigned char g,
135
// must be a color name. It can be one of 147 colors defined in CSS3 color
136
// module or SVG 1.0, which is supported by all major browsers. A reference
138
// http://www.w3.org/TR/css3-color/#svg-color
139
void SetValueFromName(StringPiece str);
141
// Two IsDefined() colors are equal if their rgb()s are equal.
142
// An IsDefined() color is not equal to a !IsDefined() color.
143
// Two !IsDefined() colors are equal regardless of their rgb()s.
144
bool Equals(const HtmlColor& color) const;
146
int r() const { return static_cast<int>(r_); }
147
int g() const { return static_cast<int>(g_); }
148
int b() const { return static_cast<int>(b_); }
149
int rgb() const { return b() + (g() << 8) + (r() << 16); }
152
class HtmlColorUtils {
154
// Converts a color into its shortest possible CSS representation.
155
// For 9 colors, that is their color name. Example: "#008000" returns "green".
156
// For colors in the form #rrggbb, where r=r, g=g, and b=b, that is #rgb.
157
// Example: "#aabbcc" returns "#abc".
158
// For all other colors, the six hex-digit representation is shortest.
159
// Example: "lightgoldenrodyellow" returns "#FAFAD2"
160
static string MaybeConvertToCssShorthand(const HtmlColor& color);
161
static string MaybeConvertToCssShorthand(StringPiece orig);
164
#endif // WEBUTIL_HTML_HTMLCOLOR_H_