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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COLOR_H_
27 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COLOR_H_
28
29 #include "third_party/blink/renderer/platform/platform_export.h"
30 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
31 #include "third_party/blink/renderer/platform/wtf/forward.h"
32 #include "third_party/blink/renderer/platform/wtf/text/unicode.h"
33
34 namespace blink {
35
36 class Color;
37
38 typedef unsigned RGBA32; // RGBA quadruplet
39
40 PLATFORM_EXPORT RGBA32 MakeRGB(int r, int g, int b);
41 PLATFORM_EXPORT RGBA32 MakeRGBA(int r, int g, int b, int a);
42
43 PLATFORM_EXPORT RGBA32 MakeRGBA32FromFloats(float r, float g, float b, float a);
44 PLATFORM_EXPORT RGBA32 MakeRGBAFromHSLA(double h, double s, double l, double a);
45 PLATFORM_EXPORT RGBA32
46 MakeRGBAFromCMYKA(float c, float m, float y, float k, float a);
47
48 PLATFORM_EXPORT int DifferenceSquared(const Color&, const Color&);
49
RedChannel(RGBA32 color)50 inline int RedChannel(RGBA32 color) {
51 return (color >> 16) & 0xFF;
52 }
GreenChannel(RGBA32 color)53 inline int GreenChannel(RGBA32 color) {
54 return (color >> 8) & 0xFF;
55 }
BlueChannel(RGBA32 color)56 inline int BlueChannel(RGBA32 color) {
57 return color & 0xFF;
58 }
AlphaChannel(RGBA32 color)59 inline int AlphaChannel(RGBA32 color) {
60 return (color >> 24) & 0xFF;
61 }
62
63 struct NamedColor {
64 DISALLOW_NEW();
65 const char* name;
66 unsigned argb_value;
67 };
68
69 PLATFORM_EXPORT const NamedColor* FindColor(const char* str, unsigned len);
70
71 class PLATFORM_EXPORT Color {
72 DISALLOW_NEW();
73
74 public:
Color()75 constexpr Color() : color_(Color::kTransparent) {}
Color(RGBA32 color)76 constexpr Color(RGBA32 color) : color_(color) {}
Color(int r,int g,int b)77 Color(int r, int g, int b) : color_(MakeRGB(r, g, b)) {}
Color(int r,int g,int b,int a)78 Color(int r, int g, int b, int a) : color_(MakeRGBA(r, g, b, a)) {}
79 // Color is currently limited to 32bit RGBA. Perhaps some day we'll support
80 // better colors.
Color(float r,float g,float b,float a)81 Color(float r, float g, float b, float a)
82 : color_(MakeRGBA32FromFloats(r, g, b, a)) {}
83 // Creates a new color from the specific CMYK and alpha values.
Color(float c,float m,float y,float k,float a)84 Color(float c, float m, float y, float k, float a)
85 : color_(MakeRGBAFromCMYKA(c, m, y, k, a)) {}
86
CreateUnchecked(int r,int g,int b)87 static constexpr Color CreateUnchecked(int r, int g, int b) {
88 RGBA32 color = 0xFF000000 | r << 16 | g << 8 | b;
89 return Color(color);
90 }
CreateUnchecked(int r,int g,int b,int a)91 static constexpr Color CreateUnchecked(int r, int g, int b, int a) {
92 RGBA32 color = a << 24 | r << 16 | g << 8 | b;
93 return Color(color);
94 }
95
96 // Returns the color serialized according to HTML5:
97 // http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
98 String Serialized() const;
99
100 // Returns the color serialized as either #RRGGBB or #RRGGBBAA. The latter
101 // format is not a valid CSS color, and should only be seen in DRT dumps.
102 String NameForLayoutTreeAsText() const;
103
104 // Returns whether parsing succeeded. The resulting Color is arbitrary
105 // if parsing fails.
106 bool SetFromString(const String&);
107 bool SetNamedColor(const String&);
108
HasAlpha()109 bool HasAlpha() const { return Alpha() < 255; }
110
Red()111 int Red() const { return RedChannel(color_); }
Green()112 int Green() const { return GreenChannel(color_); }
Blue()113 int Blue() const { return BlueChannel(color_); }
Alpha()114 int Alpha() const { return AlphaChannel(color_); }
115
Rgb()116 RGBA32 Rgb() const { return color_; } // Preserve the alpha.
SetRGB(int r,int g,int b)117 void SetRGB(int r, int g, int b) { color_ = MakeRGB(r, g, b); }
SetRGB(RGBA32 rgb)118 void SetRGB(RGBA32 rgb) { color_ = rgb; }
119 void GetRGBA(float& r, float& g, float& b, float& a) const;
120 void GetRGBA(double& r, double& g, double& b, double& a) const;
121 void GetHSL(double& h, double& s, double& l) const;
122
123 Color Light() const;
124 Color Dark() const;
125
126 Color CombineWithAlpha(float other_alpha) const;
127
128 // This is an implementation of Porter-Duff's "source-over" equation
129 Color Blend(const Color&) const;
130 Color BlendWithWhite() const;
131
132 static bool ParseHexColor(const StringView&, RGBA32&);
133 static bool ParseHexColor(const LChar*, unsigned, RGBA32&);
134 static bool ParseHexColor(const UChar*, unsigned, RGBA32&);
135
136 static const RGBA32 kBlack = 0xFF000000;
137 static const RGBA32 kWhite = 0xFFFFFFFF;
138 static const RGBA32 kDarkGray = 0xFF808080;
139 static const RGBA32 kGray = 0xFFA0A0A0;
140 static const RGBA32 kLightGray = 0xFFC0C0C0;
141 static const RGBA32 kTransparent = 0x00000000;
142
143 private:
144 RGBA32 color_;
145 };
146
147 inline bool operator==(const Color& a, const Color& b) {
148 return a.Rgb() == b.Rgb();
149 }
150
151 inline bool operator!=(const Color& a, const Color& b) {
152 return !(a == b);
153 }
154
155 PLATFORM_EXPORT Color ColorFromPremultipliedARGB(RGBA32);
156 PLATFORM_EXPORT RGBA32 PremultipliedARGBFromColor(const Color&);
157
158 } // namespace blink
159
160 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COLOR_H_
161