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 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 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 <unicode/uchar.h>
30#include <wtf/FastMalloc.h>
31#include <wtf/Forward.h>
32#include <wtf/text/LChar.h>
33
34#if USE(CG)
35#include "ColorSpace.h"
36typedef struct CGColor* CGColorRef;
37#if PLATFORM(IOS)
38typedef struct CGColorSpace* CGColorSpaceRef;
39#endif // PLATFORM(IOS)
40#endif
41
42#if PLATFORM(GTK)
43typedef struct _GdkColor GdkColor;
44#ifndef GTK_API_VERSION_2
45typedef struct _GdkRGBA GdkRGBA;
46#endif
47#endif
48
49namespace WebCore {
50
51class Color;
52
53typedef unsigned RGBA32;        // RGBA quadruplet
54
55RGBA32 makeRGB(int r, int g, int b);
56RGBA32 makeRGBA(int r, int g, int b, int a);
57
58RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
59RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
60RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
61RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
62
63int differenceSquared(const Color&, const Color&);
64
65inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; }
66inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; }
67inline int blueChannel(RGBA32 color) { return color & 0xFF; }
68inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; }
69
70class Color {
71    WTF_MAKE_FAST_ALLOCATED;
72public:
73    Color() : m_color(0), m_valid(false) { }
74    Color(RGBA32 color, bool valid = true) : m_color(color), m_valid(valid) { ASSERT(!m_color || m_valid); }
75    Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
76    Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
77    // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
78    Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
79    // Creates a new color from the specific CMYK and alpha values.
80    Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
81    explicit Color(const String&);
82    explicit Color(const char*);
83
84    static Color createUnchecked(int r, int g, int b)
85    {
86        RGBA32 color = 0xFF000000 | r << 16 | g << 8 | b;
87        return Color(color);
88    }
89    static Color createUnchecked(int r, int g, int b, int a)
90    {
91        RGBA32 color = a << 24 | r << 16 | g << 8 | b;
92        return Color(color);
93    }
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
105    bool isValid() const { return m_valid; }
106
107    bool hasAlpha() const { return alpha() < 255; }
108
109    int red() const { return redChannel(m_color); }
110    int green() const { return greenChannel(m_color); }
111    int blue() const { return blueChannel(m_color); }
112    int alpha() const { return alphaChannel(m_color); }
113
114    RGBA32 rgb() const { return m_color; } // Preserve the alpha.
115    void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
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    bool isDark() const;
125
126    // This is an implementation of Porter-Duff's "source-over" equation
127    Color blend(const Color&) const;
128    Color blendWithWhite() const;
129
130#if PLATFORM(GTK)
131    Color(const GdkColor&);
132    // We can't sensibly go back to GdkColor without losing the alpha value
133#ifndef GTK_API_VERSION_2
134    Color(const GdkRGBA&);
135    operator GdkRGBA() const;
136#endif
137#endif
138
139#if USE(CG)
140    Color(CGColorRef);
141#endif
142
143    static bool parseHexColor(const String&, RGBA32&);
144    static bool parseHexColor(const LChar*, unsigned, RGBA32&);
145    static bool parseHexColor(const UChar*, unsigned, RGBA32&);
146
147    static const RGBA32 black = 0xFF000000;
148    static const RGBA32 white = 0xFFFFFFFF;
149    static const RGBA32 darkGray = 0xFF808080;
150    static const RGBA32 gray = 0xFFA0A0A0;
151    static const RGBA32 lightGray = 0xFFC0C0C0;
152    static const RGBA32 transparent = 0x00000000;
153    static const RGBA32 cyan = 0xFF00FFFF;
154
155#if PLATFORM(IOS)
156    static const RGBA32 tap = 0x4D1A1A1A;
157
158    // FIXME: This color shouldn't be iOS-specific. Once we fix up its usage in InlineTextBox::paintCompositionBackground()
159    // we should move it outside the PLATFORM(IOS)-guard. See <https://bugs.webkit.org/show_bug.cgi?id=126296>.
160    static const RGBA32 compositionFill = 0x3CAFC0E3;
161#endif
162
163private:
164    RGBA32 m_color;
165    bool m_valid;
166};
167
168inline bool operator==(const Color& a, const Color& b)
169{
170    return a.rgb() == b.rgb() && a.isValid() == b.isValid();
171}
172
173inline bool operator!=(const Color& a, const Color& b)
174{
175    return !(a == b);
176}
177
178Color colorFromPremultipliedARGB(RGBA32);
179RGBA32 premultipliedARGBFromColor(const Color&);
180Color blend(const Color& from, const Color& to, double progress, bool blendPremultiplied = true);
181
182inline uint16_t fastDivideBy255(uint16_t value)
183{
184    // This is an approximate algorithm for division by 255, but it gives accurate results for 16bit values.
185    uint16_t approximation = value >> 8;
186    uint16_t remainder = value - (approximation * 255) + 1;
187    return approximation + (remainder >> 8);
188}
189
190#if USE(CG)
191CGColorRef cachedCGColor(const Color&, ColorSpace);
192#if PLATFORM(IOS)
193CGColorRef createCGColorWithDeviceWhite(CGFloat white, CGFloat alpha);
194#endif // PLATFORM(IOS)
195#endif
196
197} // namespace WebCore
198
199#endif // Color_h
200