• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003-6 Apple Computer, 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/Platform.h>
30 
31 #if PLATFORM(CG)
32 typedef struct CGColor* CGColorRef;
33 #endif
34 
35 #if PLATFORM(QT)
36 #include <qglobal.h>
37 QT_BEGIN_NAMESPACE
38 class QColor;
39 QT_END_NAMESPACE
40 #endif
41 
42 #if PLATFORM(GTK)
43 typedef struct _GdkColor GdkColor;
44 #endif
45 
46 #if PLATFORM(WX)
47 class wxColour;
48 #endif
49 
50 namespace WebCore {
51 
52 class String;
53 class Color;
54 
55 typedef unsigned RGBA32;        // RGBA quadruplet
56 
57 RGBA32 makeRGB(int r, int g, int b);
58 RGBA32 makeRGBA(int r, int g, int b, int a);
59 
60 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
61 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
62 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
63 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
64 
65 int differenceSquared(const Color&, const Color&);
66 
67 class Color {
68 public:
Color()69     Color() : m_color(0), m_valid(false) { }
Color(RGBA32 col)70     Color(RGBA32 col) : m_color(col), m_valid(true) { }
Color(int r,int g,int b)71     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)72     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
73     // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
Color(float r,float g,float b,float a)74     Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
75     // Creates a new color from the specific CMYK and alpha values.
Color(float c,float m,float y,float k,float a)76     Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
77     explicit Color(const String&);
78     explicit Color(const char*);
79 
80     String name() const;
81     void setNamedColor(const String&);
82 
isValid()83     bool isValid() const { return m_valid; }
84 
hasAlpha()85     bool hasAlpha() const { return alpha() < 255; }
86 
red()87     int red() const { return (m_color >> 16) & 0xFF; }
green()88     int green() const { return (m_color >> 8) & 0xFF; }
blue()89     int blue() const { return m_color & 0xFF; }
alpha()90     int alpha() const { return (m_color >> 24) & 0xFF; }
91 
rgb()92     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
setRGB(int r,int g,int b)93     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
setRGB(RGBA32 rgb)94     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
95     void getRGBA(float& r, float& g, float& b, float& a) const;
96     void getRGBA(double& r, double& g, double& b, double& a) const;
97     void getHSL(double& h, double& s, double& l) const;
98 
99     Color light() const;
100     Color dark() const;
101 
102     Color blend(const Color&) const;
103     Color blendWithWhite() const;
104 
105 #if PLATFORM(QT)
106     Color(const QColor&);
107     operator QColor() const;
108 #endif
109 
110 #if PLATFORM(GTK)
111     Color(const GdkColor&);
112     // We can't sensibly go back to GdkColor without losing the alpha value
113 #endif
114 
115 #if PLATFORM(WX)
116     Color(const wxColour&);
117     operator wxColour() const;
118 #endif
119 
120 #if PLATFORM(CG)
121     Color(CGColorRef);
122 #endif
123 
124     static bool parseHexColor(const String& name, RGBA32& rgb);
125 
126     static const RGBA32 black = 0xFF000000;
127     static const RGBA32 white = 0xFFFFFFFF;
128     static const RGBA32 darkGray = 0xFF808080;
129     static const RGBA32 gray = 0xFFA0A0A0;
130     static const RGBA32 lightGray = 0xFFC0C0C0;
131     static const RGBA32 transparent = 0x00000000;
132 
133 #ifdef ANDROID_CSS_TAP_HIGHLIGHT_COLOR
134     static const RGBA32 tap = 0x4D1A1A1A;
135 #endif
136 
137 private:
138     RGBA32 m_color;
139     bool m_valid;
140 };
141 
142 inline bool operator==(const Color& a, const Color& b)
143 {
144     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
145 }
146 
147 inline bool operator!=(const Color& a, const Color& b)
148 {
149     return !(a == b);
150 }
151 
152 Color colorFromPremultipliedARGB(unsigned);
153 unsigned premultipliedARGBFromColor(const Color&);
154 
155 #if PLATFORM(CG)
156 CGColorRef createCGColor(const Color&);
157 #endif
158 
159 } // namespace WebCore
160 
161 #endif // Color_h
162