blob: b1571c700922b3d358607525482c962f66587e9a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkColor_DEFINED
9#define SkColor_DEFINED
10
11#include "SkScalar.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
14/** \file SkColor.h
15
16 Types and macros for colors
17*/
18
19/** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
20*/
21typedef uint8_t SkAlpha;
22/** 32 bit ARGB color value, not premultiplied. The color components are always in
23 a known order. This is different from SkPMColor, which has its bytes in a configuration
24 dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
25 specify colors in SkPaint and in gradients.
26*/
27typedef uint32_t SkColor;
28
29/** Return a SkColor value from 8 bit component values
30*/
agl@chromium.orgc9c9ebb2010-07-28 17:10:30 +000031static inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
reed@android.com8a1c16f2008-12-17 15:59:43 +000032{
33 SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
34
35 return (a << 24) | (r << 16) | (g << 8) | (b << 0);
36}
37
agl@chromium.orgc9c9ebb2010-07-28 17:10:30 +000038#define SkColorSetARGBMacro(a, r, g, b) \
39 static_cast<SkColor>( \
40 (static_cast<U8CPU>(a) << 24) | \
41 (static_cast<U8CPU>(r) << 16) | \
42 (static_cast<U8CPU>(g) << 8) | \
43 (static_cast<U8CPU>(b) << 0))
44
45/** gcc will generate static initializers for code of this form:
46 * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03)
47 * if SkColorSetARGB() is a static inline, but not if it's a macro.
48 */
49#if defined(NDEBUG)
50#define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b)
51#else
52#define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b)
53#endif
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055/** Return a SkColor value from 8 bit component values, with an implied value
56 of 0xFF for alpha (fully opaque)
57*/
58#define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
59
60/** return the alpha byte from a SkColor value */
61#define SkColorGetA(color) (((color) >> 24) & 0xFF)
62/** return the red byte from a SkColor value */
63#define SkColorGetR(color) (((color) >> 16) & 0xFF)
64/** return the green byte from a SkColor value */
65#define SkColorGetG(color) (((color) >> 8) & 0xFF)
66/** return the blue byte from a SkColor value */
67#define SkColorGetB(color) (((color) >> 0) & 0xFF)
68
69static inline SkColor SkColorSetA(SkColor c, U8CPU a) {
70 return (c & 0x00FFFFFF) | (a << 24);
71}
72
73// common colors
74
commit-bot@chromium.org9fdb7052013-07-19 17:43:27 +000075#define SK_AlphaTRANSPARENT 0x00 //!< transparent SkAlpha value
76#define SK_AlphaOPAQUE 0xFF //!< opaque SkAlpha value
77
junov@google.comdbfac8a2012-12-06 21:47:40 +000078#define SK_ColorTRANSPARENT 0x00000000 //!< transparent SkColor value
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
junov@google.comdbfac8a2012-12-06 21:47:40 +000080#define SK_ColorBLACK 0xFF000000 //!< black SkColor value
81#define SK_ColorDKGRAY 0xFF444444 //!< dark gray SkColor value
82#define SK_ColorGRAY 0xFF888888 //!< gray SkColor value
83#define SK_ColorLTGRAY 0xFFCCCCCC //!< light gray SkColor value
84#define SK_ColorWHITE 0xFFFFFFFF //!< white SkColor value
85
86#define SK_ColorRED 0xFFFF0000 //!< red SkColor value
87#define SK_ColorGREEN 0xFF00FF00 //!< green SkColor value
88#define SK_ColorBLUE 0xFF0000FF //!< blue SkColor value
89#define SK_ColorYELLOW 0xFFFFFF00 //!< yellow SkColor value
90#define SK_ColorCYAN 0xFF00FFFF //!< cyan SkColor value
91#define SK_ColorMAGENTA 0xFFFF00FF //!< magenta SkColor value
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93////////////////////////////////////////////////////////////////////////
94
95/** Convert RGB components to HSV.
96 hsv[0] is Hue [0 .. 360)
97 hsv[1] is Saturation [0...1]
98 hsv[2] is Value [0...1]
99 @param red red component value [0..255]
100 @param green green component value [0..255]
101 @param blue blue component value [0..255]
102 @param hsv 3 element array which holds the resulting HSV components.
103*/
reed@google.comf3166342011-04-26 20:06:08 +0000104SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
106/** Convert the argb color to its HSV components.
107 hsv[0] is Hue [0 .. 360)
108 hsv[1] is Saturation [0...1]
109 hsv[2] is Value [0...1]
110 @param color the argb color to convert. Note: the alpha component is ignored.
111 @param hsv 3 element array which holds the resulting HSV components.
112*/
reedfbc1e292016-01-29 05:22:59 -0800113static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
115}
116
117/** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
118 hsv[0] is Hue [0 .. 360)
119 hsv[1] is Saturation [0...1]
120 hsv[2] is Value [0...1]
121 If hsv values are out of range, they are pinned.
122 @param alpha the alpha component of the returned argb color.
123 @param hsv 3 element array which holds the input HSV components.
124 @return the resulting argb color
125*/
reed@google.comf3166342011-04-26 20:06:08 +0000126SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127
128/** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
129 hsv[0] is Hue [0 .. 360)
130 hsv[1] is Saturation [0...1]
131 hsv[2] is Value [0...1]
132 If hsv values are out of range, they are pinned.
133 @param hsv 3 element array which holds the input HSV components.
134 @return the resulting argb color
135*/
reedfbc1e292016-01-29 05:22:59 -0800136static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 return SkHSVToColor(0xFF, hsv);
138}
139
140////////////////////////////////////////////////////////////////////////
141
142/** 32 bit ARGB color value, premultiplied. The byte order for this value is
143 configuration dependent, matching the format of kARGB32 bitmaps. This is different
144 from SkColor, which is nonpremultiplied, and is always in the same byte order.
145*/
146typedef uint32_t SkPMColor;
147
148/** Return a SkPMColor value from unpremultiplied 8 bit component values
149*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000150SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151/** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
152 components by the color's alpha, and by arranging the bytes in a configuration
153 dependent order, to match the format of kARGB32 bitmaps.
154*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000155SK_API SkPMColor SkPreMultiplyColor(SkColor c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
157/** Define a function pointer type for combining two premultiplied colors
158*/
159typedef SkPMColor (*SkXfermodeProc)(SkPMColor src, SkPMColor dst);
160
reed6d3cef92016-01-22 01:04:29 -0800161///////////////////////////////////////////////////////////////////////////////////////////////////
162
reedf7cdb062016-02-04 11:35:27 -0800163struct SkColor4f;
164
reed6d3cef92016-01-22 01:04:29 -0800165/*
166 * The float values are 0...1 premultiplied
167 */
mtklein24c27202016-01-22 09:50:20 -0800168struct SkPM4f {
reedfbc1e292016-01-29 05:22:59 -0800169 enum {
170 A = SK_A32_SHIFT/8,
171 R = SK_R32_SHIFT/8,
172 G = SK_G32_SHIFT/8,
173 B = SK_B32_SHIFT/8,
174 };
reed6d3cef92016-01-22 01:04:29 -0800175 float fVec[4];
176
reed395eabe2016-01-30 18:52:31 -0800177 float a() const { return fVec[A]; }
reed7f225cf2016-01-23 19:37:56 -0800178
reedf7cdb062016-02-04 11:35:27 -0800179 SkColor4f unpremul() const;
180
reed7f225cf2016-01-23 19:37:56 -0800181 static SkPM4f FromPMColor(SkPMColor);
reedfbc1e292016-01-29 05:22:59 -0800182
reed3601f282016-02-05 11:18:39 -0800183 // half-float routines
184 void toF16(uint16_t[4]) const;
185 uint64_t toF16() const; // 4 float16 values packed into uint64_t
186 static SkPM4f FromF16(const uint16_t[4]);
187
reed395eabe2016-01-30 18:52:31 -0800188#ifdef SK_DEBUG
189 void assertIsUnit() const;
190#else
191 void assertIsUnit() const {}
192#endif
reed6d3cef92016-01-22 01:04:29 -0800193};
194
195/*
196 * The float values are 0...1 unpremultiplied
197 */
198struct SkColor4f {
199 float fA;
200 float fR;
201 float fG;
202 float fB;
203
204 bool operator==(const SkColor4f& other) const {
205 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
206 }
207 bool operator!=(const SkColor4f& other) const {
208 return !(*this == other);
209 }
210
211 const float* vec() const { return &fA; }
212 float* vec() { return &fA; }
213
214 static SkColor4f Pin(float a, float r, float g, float b);
215 static SkColor4f FromColor(SkColor);
216
217 SkColor4f pin() const {
218 return Pin(fA, fR, fG, fB);
219 }
220
221 SkPM4f premul() const;
222};
223
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224#endif