tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / Color.h
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 "AnimationUtilities.h"
30 #include <wtf/FastAllocBase.h>
31 #include <wtf/Forward.h>
32 #include <wtf/unicode/Unicode.h>
33
34 #if USE(CG)
35 #include "ColorSpace.h"
36 typedef struct CGColor* CGColorRef;
37 #endif
38
39 #if PLATFORM(QT)
40 #include <qglobal.h>
41 QT_BEGIN_NAMESPACE
42 class QColor;
43 QT_END_NAMESPACE
44 #endif
45
46 #if PLATFORM(GTK)
47 typedef struct _GdkColor GdkColor;
48 #ifndef GTK_API_VERSION_2
49 typedef struct _GdkRGBA GdkRGBA;
50 #endif
51 #endif
52
53 #if PLATFORM(WX)
54 class wxColour;
55 #endif
56
57 namespace WebCore {
58
59 class Color;
60
61 typedef unsigned RGBA32;        // RGBA quadruplet
62
63 RGBA32 makeRGB(int r, int g, int b);
64 RGBA32 makeRGBA(int r, int g, int b, int a);
65
66 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
67 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
68 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
69 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
70
71 int differenceSquared(const Color&, const Color&);
72
73 inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; }
74 inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; }
75 inline int blueChannel(RGBA32 color) { return color & 0xFF; }
76 inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; }
77
78 class Color {
79     WTF_MAKE_FAST_ALLOCATED;
80 public:
81     Color() : m_color(0), m_valid(false) { }
82     Color(RGBA32 color, bool valid = true) : m_color(color), m_valid(valid) { ASSERT(!m_color || m_valid); }
83     Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
84     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
85     // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
86     Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
87     // Creates a new color from the specific CMYK and alpha values.
88     Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
89     explicit Color(const String&);
90     explicit Color(const char*);
91
92     static Color createUnCheked(int r, int g, int b) 
93     {
94         RGBA32 color = 0xFF000000 | r << 16 | g << 8 | b;
95         return Color(color);
96     }
97     static Color createUnCheked(int r, int g, int b, int a) 
98     {
99         RGBA32 color = a << 24 | r << 16 | g << 8 | b;
100         return Color(color);
101     }
102
103     // Returns the color serialized according to HTML5
104     // - http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
105     String serialized() const;
106
107     // Returns the color serialized as either #RRGGBB or #RRGGBBAA
108     // The latter format is not a valid CSS color, and should only be seen in DRT dumps.
109     String nameForRenderTreeAsText() const;
110
111     void setNamedColor(const String&);
112
113     bool isValid() const { return m_valid; }
114
115     bool hasAlpha() const { return alpha() < 255; }
116
117     int red() const { return redChannel(m_color); }
118     int green() const { return greenChannel(m_color); }
119     int blue() const { return blueChannel(m_color); }
120     int alpha() const { return alphaChannel(m_color); }
121     
122     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
123     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
124     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
125     void getRGBA(float& r, float& g, float& b, float& a) const;
126     void getRGBA(double& r, double& g, double& b, double& a) const;
127     void getHSL(double& h, double& s, double& l) const;
128
129     Color light() const;
130     Color dark() const;
131
132     // This is an implementation of Porter-Duff's "source-over" equation
133     Color blend(const Color&) const;
134     Color blendWithWhite() const;
135
136 #if PLATFORM(QT)
137     Color(const QColor&);
138     operator QColor() const;
139 #endif
140
141 #if PLATFORM(GTK)
142     Color(const GdkColor&);
143     // We can't sensibly go back to GdkColor without losing the alpha value
144 #ifndef GTK_API_VERSION_2
145     Color(const GdkRGBA&);
146     operator GdkRGBA() const;
147 #endif
148 #endif
149
150 #if PLATFORM(WX)
151     Color(const wxColour&);
152     operator wxColour() const;
153 #endif
154
155 #if USE(CG)
156     Color(CGColorRef);
157 #endif
158
159     static bool parseHexColor(const String& name, RGBA32& rgb);
160     static bool parseHexColor(const UChar* name, unsigned length, RGBA32& rgb);
161
162     static const RGBA32 black = 0xFF000000;
163     static const RGBA32 white = 0xFFFFFFFF;
164     static const RGBA32 darkGray = 0xFF808080;
165     static const RGBA32 gray = 0xFFA0A0A0;
166     static const RGBA32 lightGray = 0xFFC0C0C0;
167     static const RGBA32 transparent = 0x00000000;
168
169 private:
170     RGBA32 m_color;
171     bool m_valid;
172 };
173
174 inline bool operator==(const Color& a, const Color& b)
175 {
176     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
177 }
178
179 inline bool operator!=(const Color& a, const Color& b)
180 {
181     return !(a == b);
182 }
183
184 Color colorFromPremultipliedARGB(unsigned);
185 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING)
186 Color colorFromPremultipliedABGR(unsigned);
187 unsigned premultipliedABGRFromColor(const Color&);
188 #endif
189 unsigned premultipliedARGBFromColor(const Color&);
190
191 inline Color blend(const Color& from, const Color& to, double progress, bool blendPremultiplied = true)
192 {
193     // We need to preserve the state of the valid flag at the end of the animation
194     if (progress == 1 && !to.isValid())
195         return Color();
196     
197     if (blendPremultiplied) {
198         // Contrary to the name, RGBA32 actually stores ARGB, so we can initialize Color directly from premultipliedARGBFromColor().
199         // Also, premultipliedARGBFromColor() bails on zero alpha, so special-case that.
200         Color premultFrom = from.alpha() ? premultipliedARGBFromColor(from) : 0;
201         Color premultTo = to.alpha() ? premultipliedARGBFromColor(to) : 0;
202
203         Color premultBlended(blend(premultFrom.red(), premultTo.red(), progress),
204                      blend(premultFrom.green(), premultTo.green(), progress),
205                      blend(premultFrom.blue(), premultTo.blue(), progress),
206                      blend(premultFrom.alpha(), premultTo.alpha(), progress));
207
208         return Color(colorFromPremultipliedARGB(premultBlended.rgb()));
209     }
210
211     return Color(blend(from.red(), to.red(), progress),
212                  blend(from.green(), to.green(), progress),
213                  blend(from.blue(), to.blue(), progress),
214                  blend(from.alpha(), to.alpha(), progress));
215 }
216
217 #if USE(CG)
218 CGColorRef cachedCGColor(const Color&, ColorSpace);
219 #endif
220
221 } // namespace WebCore
222
223 #endif // Color_h