~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/css_parser/src/webutil/html/htmlcolor.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Copyright 2010 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Copyright (C) 2000 and onwards Google, Inc.
18
 
//
19
 
//
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.
25
 
 
26
 
#ifndef WEBUTIL_HTML_HTMLCOLOR_H_
27
 
#define WEBUTIL_HTML_HTMLCOLOR_H_
28
 
 
29
 
#include <stdlib.h>
30
 
#include <string>
31
 
#include "string_using.h"
32
 
#include "strings/stringpiece.h"
33
 
 
34
 
class HtmlColor {
35
 
 private:
36
 
  unsigned char r_;
37
 
  unsigned char g_;
38
 
  unsigned char b_;
39
 
 
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;
51
 
 
52
 
  void SetBadNameValue() {
53
 
    r_ = g_ = b_ = 0x00;
54
 
    is_bad_value_ = kBadColorName;
55
 
  }
56
 
  void SetBadHexValue() {
57
 
    r_ = g_ = b_ = 0x00;
58
 
    is_bad_value_ = kBadColorHex;
59
 
  }
60
 
  void SetDefaultValue() {
61
 
    r_ = g_ = b_ = 0x00;
62
 
    is_bad_value_ = kGoodColorValue;
63
 
  }
64
 
 
65
 
 public:
66
 
  enum TolerateLevel {
67
 
    EXACTLY_SAME = 0,
68
 
    HIGHLY_SIMILAR = 5,
69
 
    SIMILAR = 10
70
 
  };
71
 
 
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);
77
 
 
78
 
  bool IsDefined() const {
79
 
    return is_bad_value_ == 0;
80
 
  }
81
 
 
82
 
  bool IsSimilar(const HtmlColor &color, int level) const {
83
 
    if (!IsDefined() || !color.IsDefined())
84
 
      return false;
85
 
 
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)
89
 
       )
90
 
      return true;
91
 
    return false;
92
 
  }
93
 
 
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;
102
 
 
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;
106
 
 
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);
112
 
 
113
 
  // Desaturate the color (0.0 = no change, 1.0 = equivalent shade of gray)
114
 
  void Desaturate(float factor);
115
 
 
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);
120
 
 
121
 
  string ToString() const;
122
 
 
123
 
  // hexstr is in form of "xxxxxx"
124
 
  void SetValueFromHexStr(StringPiece hexstr);
125
 
 
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);
130
 
 
131
 
  // Set the html color object from rgb values
132
 
  void SetValueFromRGB(unsigned char r, unsigned char g,
133
 
                       unsigned char b);
134
 
 
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
137
 
  // can be found at:
138
 
  //   http://www.w3.org/TR/css3-color/#svg-color
139
 
  void SetValueFromName(StringPiece str);
140
 
 
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;
145
 
 
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); }
150
 
};
151
 
 
152
 
class HtmlColorUtils {
153
 
 public:
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);
162
 
};
163
 
 
164
 
#endif  // WEBUTIL_HTML_HTMLCOLOR_H_