1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkColor_DEFINED
9 #define SkColor_DEFINED
10
11 #include "SkScalar.h"
12 #include "SkPoint3.h"
13 #include "SkTypes.h"
14
15 /** \file SkColor.h
16
17 Types and macros for colors
18 */
19
20 /** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
21 */
22 typedef uint8_t SkAlpha;
23 /** 32 bit ARGB color value, not premultiplied. The color components are always in
24 a known order. This is different from SkPMColor, which has its bytes in a configuration
25 dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
26 specify colors in SkPaint and in gradients.
27 */
28 typedef uint32_t SkColor;
29
30 /** Return a SkColor value from 8 bit component values
31 */
SkColorSetARGBInline(U8CPU a,U8CPU r,U8CPU g,U8CPU b)32 static inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
33 {
34 SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
35
36 return (a << 24) | (r << 16) | (g << 8) | (b << 0);
37 }
38
39 #define SkColorSetARGBMacro(a, r, g, b) \
40 static_cast<SkColor>( \
41 (static_cast<U8CPU>(a) << 24) | \
42 (static_cast<U8CPU>(r) << 16) | \
43 (static_cast<U8CPU>(g) << 8) | \
44 (static_cast<U8CPU>(b) << 0))
45
46 /** gcc will generate static initializers for code of this form:
47 * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03)
48 * if SkColorSetARGB() is a static inline, but not if it's a macro.
49 */
50 #if defined(NDEBUG)
51 #define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b)
52 #else
53 #define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b)
54 #endif
55
56 /** Return a SkColor value from 8 bit component values, with an implied value
57 of 0xFF for alpha (fully opaque)
58 */
59 #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
60
61 /** return the alpha byte from a SkColor value */
62 #define SkColorGetA(color) (((color) >> 24) & 0xFF)
63 /** return the red byte from a SkColor value */
64 #define SkColorGetR(color) (((color) >> 16) & 0xFF)
65 /** return the green byte from a SkColor value */
66 #define SkColorGetG(color) (((color) >> 8) & 0xFF)
67 /** return the blue byte from a SkColor value */
68 #define SkColorGetB(color) (((color) >> 0) & 0xFF)
69
SkColorSetA(SkColor c,U8CPU a)70 static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) {
71 return (c & 0x00FFFFFF) | (a << 24);
72 }
73
74 // common colors
75
76 /** transparent SkAlpha value */
77 #define SK_AlphaTRANSPARENT static_cast<SkAlpha>(0x00)
78 /** opaque SkAlpha value */
79 #define SK_AlphaOPAQUE static_cast<SkAlpha>(0xFF)
80
81 /** transparent SkColor value */
82 #define SK_ColorTRANSPARENT static_cast<SkColor>(0x00000000)
83
84 /** black SkColor value */
85 #define SK_ColorBLACK static_cast<SkColor>(0xFF000000)
86 /** dark gray SkColor value */
87 #define SK_ColorDKGRAY static_cast<SkColor>(0xFF444444)
88 /** gray SkColor value */
89 #define SK_ColorGRAY static_cast<SkColor>(0xFF888888)
90 /** light gray SkColor value */
91 #define SK_ColorLTGRAY static_cast<SkColor>(0xFFCCCCCC)
92 /** white SkColor value */
93 #define SK_ColorWHITE static_cast<SkColor>(0xFFFFFFFF)
94
95 /** red SkColor value */
96 #define SK_ColorRED static_cast<SkColor>(0xFFFF0000)
97 /** green SkColor value */
98 #define SK_ColorGREEN static_cast<SkColor>(0xFF00FF00)
99 /** blue SkColor value */
100 #define SK_ColorBLUE static_cast<SkColor>(0xFF0000FF)
101 /** yellow SkColor value */
102 #define SK_ColorYELLOW static_cast<SkColor>(0xFFFFFF00)
103 /** cyan SkColor value */
104 #define SK_ColorCYAN static_cast<SkColor>(0xFF00FFFF)
105 /** magenta SkColor value */
106 #define SK_ColorMAGENTA static_cast<SkColor>(0xFFFF00FF)
107
108 ////////////////////////////////////////////////////////////////////////
109
110 /** Convert RGB components to HSV.
111 hsv[0] is Hue [0 .. 360)
112 hsv[1] is Saturation [0...1]
113 hsv[2] is Value [0...1]
114 @param red red component value [0..255]
115 @param green green component value [0..255]
116 @param blue blue component value [0..255]
117 @param hsv 3 element array which holds the resulting HSV components.
118 */
119 SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
120
121 /** Convert the argb color to its HSV components.
122 hsv[0] is Hue [0 .. 360)
123 hsv[1] is Saturation [0...1]
124 hsv[2] is Value [0...1]
125 @param color the argb color to convert. Note: the alpha component is ignored.
126 @param hsv 3 element array which holds the resulting HSV components.
127 */
SkColorToHSV(SkColor color,SkScalar hsv[3])128 static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
129 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
130 }
131
132 /** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
133 hsv[0] is Hue [0 .. 360)
134 hsv[1] is Saturation [0...1]
135 hsv[2] is Value [0...1]
136 If hsv values are out of range, they are pinned.
137 @param alpha the alpha component of the returned argb color.
138 @param hsv 3 element array which holds the input HSV components.
139 @return the resulting argb color
140 */
141 SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
142
143 /** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
144 hsv[0] is Hue [0 .. 360)
145 hsv[1] is Saturation [0...1]
146 hsv[2] is Value [0...1]
147 If hsv values are out of range, they are pinned.
148 @param hsv 3 element array which holds the input HSV components.
149 @return the resulting argb color
150 */
SkHSVToColor(const SkScalar hsv[3])151 static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
152 return SkHSVToColor(0xFF, hsv);
153 }
154
155 ////////////////////////////////////////////////////////////////////////
156
157 /** 32 bit ARGB color value, premultiplied. The byte order for this value is
158 configuration dependent, matching the format of kARGB32 bitmaps. This is different
159 from SkColor, which is nonpremultiplied, and is always in the same byte order.
160 */
161 typedef uint32_t SkPMColor;
162
163 /** Return a SkPMColor value from unpremultiplied 8 bit component values
164 */
165 SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
166 /** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
167 components by the color's alpha, and by arranging the bytes in a configuration
168 dependent order, to match the format of kARGB32 bitmaps.
169 */
170 SK_API SkPMColor SkPreMultiplyColor(SkColor c);
171
172 ///////////////////////////////////////////////////////////////////////////////////////////////////
173
174 struct SkPM4f;
175
176 /*
177 * The float values are 0...1 unpremultiplied
178 */
179 struct SkColor4f {
180 float fR;
181 float fG;
182 float fB;
183 float fA;
184
185 bool operator==(const SkColor4f& other) const {
186 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
187 }
188 bool operator!=(const SkColor4f& other) const {
189 return !(*this == other);
190 }
191
vecSkColor4f192 const float* vec() const { return &fR; }
vecSkColor4f193 float* vec() { return &fR; }
194
195 static SkColor4f Pin(float r, float g, float b, float a);
196 /** Convert to SkColor4f, assuming SkColor is sRGB */
197 static SkColor4f FromColor(SkColor);
198 static SkColor4f FromColor3f(SkColor3f, float a);
199
200 SkColor toSkColor() const;
201
pinSkColor4f202 SkColor4f pin() const {
203 return Pin(fR, fG, fB, fA);
204 }
205
206 SkPM4f premul() const;
207 };
208
209 #endif
210