• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef Color_h
27 #define Color_h
28 
29 #include <wtf/FastAllocBase.h>
30 #include <wtf/Forward.h>
31 #include <wtf/unicode/Unicode.h>
32 
33 #if USE(CG)
34 #include "ColorSpace.h"
35 typedef struct CGColor* CGColorRef;
36 #endif
37 
38 #if PLATFORM(QT)
39 #include <qglobal.h>
40 QT_BEGIN_NAMESPACE
41 class QColor;
42 QT_END_NAMESPACE
43 #endif
44 
45 #if PLATFORM(GTK)
46 typedef struct _GdkColor GdkColor;
47 #ifndef GTK_API_VERSION_2
48 typedef struct _GdkRGBA GdkRGBA;
49 #endif
50 #endif
51 
52 #if PLATFORM(WX)
53 class wxColour;
54 #endif
55 
56 #if PLATFORM(HAIKU)
57 struct rgb_color;
58 #endif
59 
60 namespace WebCore {
61 
62 class Color;
63 
64 typedef unsigned RGBA32;        // RGBA quadruplet
65 
66 RGBA32 makeRGB(int r, int g, int b);
67 RGBA32 makeRGBA(int r, int g, int b, int a);
68 
69 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
70 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
71 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
72 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
73 
74 int differenceSquared(const Color&, const Color&);
75 
redChannel(RGBA32 color)76 inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; }
greenChannel(RGBA32 color)77 inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; }
blueChannel(RGBA32 color)78 inline int blueChannel(RGBA32 color) { return color & 0xFF; }
alphaChannel(RGBA32 color)79 inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; }
80 
81 class Color {
82     WTF_MAKE_FAST_ALLOCATED;
83 public:
Color()84     Color() : m_color(0), m_valid(false) { }
Color(RGBA32 col)85     Color(RGBA32 col) : m_color(col), m_valid(true) { }
Color(int r,int g,int b)86     Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
Color(int r,int g,int b,int a)87     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
88     // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
Color(float r,float g,float b,float a)89     Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
90     // Creates a new color from the specific CMYK and alpha values.
Color(float c,float m,float y,float k,float a)91     Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
92     explicit Color(const String&);
93     explicit Color(const char*);
94 
95     // Returns the color serialized according to HTML5
96     // - http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
97     String serialized() const;
98 
99     // Returns the color serialized as either #RRGGBB or #RRGGBBAA
100     // The latter format is not a valid CSS color, and should only be seen in DRT dumps.
101     String nameForRenderTreeAsText() const;
102 
103     void setNamedColor(const String&);
104 
isValid()105     bool isValid() const { return m_valid; }
106 
hasAlpha()107     bool hasAlpha() const { return alpha() < 255; }
108 
red()109     int red() const { return redChannel(m_color); }
green()110     int green() const { return greenChannel(m_color); }
blue()111     int blue() const { return blueChannel(m_color); }
alpha()112     int alpha() const { return alphaChannel(m_color); }
113 
rgb()114     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
setRGB(int r,int g,int b)115     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
setRGB(RGBA32 rgb)116     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
117     void getRGBA(float& r, float& g, float& b, float& a) const;
118     void getRGBA(double& r, double& g, double& b, double& a) const;
119     void getHSL(double& h, double& s, double& l) const;
120 
121     Color light() const;
122     Color dark() const;
123 
124     Color blend(const Color&) const;
125     Color blendWithWhite() const;
126 
127 #if PLATFORM(QT)
128     Color(const QColor&);
129     operator QColor() const;
130 #endif
131 
132 #if PLATFORM(GTK)
133     Color(const GdkColor&);
134     // We can't sensibly go back to GdkColor without losing the alpha value
135 #ifndef GTK_API_VERSION_2
136     Color(const GdkRGBA&);
137     operator GdkRGBA() const;
138 #endif
139 #endif
140 
141 #if PLATFORM(WX)
142     Color(const wxColour&);
143     operator wxColour() const;
144 #endif
145 
146 #if USE(CG)
147     Color(CGColorRef);
148 #endif
149 
150 #if PLATFORM(HAIKU)
151     Color(const rgb_color&);
152     operator rgb_color() const;
153 #endif
154 
155     static bool parseHexColor(const String& name, RGBA32& rgb);
156     static bool parseHexColor(const UChar* name, unsigned length, RGBA32& rgb);
157 
158     static const RGBA32 black = 0xFF000000;
159     static const RGBA32 white = 0xFFFFFFFF;
160     static const RGBA32 darkGray = 0xFF808080;
161     static const RGBA32 gray = 0xFFA0A0A0;
162     static const RGBA32 lightGray = 0xFFC0C0C0;
163     static const RGBA32 transparent = 0x00000000;
164 
165 #ifdef ANDROID_CSS_RING
166     static const RGBA32 ringFill = 0x666699FF;
167     static const RGBA32 ringPressedInner = 0x006699FF;
168     static const RGBA32 ringPressedOuter = 0x336699FF;
169     static const RGBA32 ringSelectedInner = 0xAA6699FF;
170     static const RGBA32 ringSelectedOuter = 0x336699FF;
171 #endif
172 #ifdef ANDROID_CSS_TAP_HIGHLIGHT_COLOR
173     static const RGBA32 tap = 0x4D1A1A1A;
174 #endif
175 
176 private:
177     RGBA32 m_color;
178     bool m_valid;
179 };
180 
181 inline bool operator==(const Color& a, const Color& b)
182 {
183     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
184 }
185 
186 inline bool operator!=(const Color& a, const Color& b)
187 {
188     return !(a == b);
189 }
190 
191 Color colorFromPremultipliedARGB(unsigned);
192 unsigned premultipliedARGBFromColor(const Color&);
193 
194 #if USE(CG)
195 CGColorRef cachedCGColor(const Color&, ColorSpace);
196 #endif
197 
198 } // namespace WebCore
199 
200 #endif // Color_h
201