blob: c97a8ec4d156994f4aef1822a4a90fe27964b970 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkColor_DEFINED
18#define SkColor_DEFINED
19
20#include "SkScalar.h"
21
22/** \file SkColor.h
23
24 Types and macros for colors
25*/
26
27/** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
28*/
29typedef uint8_t SkAlpha;
30/** 32 bit ARGB color value, not premultiplied. The color components are always in
31 a known order. This is different from SkPMColor, which has its bytes in a configuration
32 dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
33 specify colors in SkPaint and in gradients.
34*/
35typedef uint32_t SkColor;
36
37/** Return a SkColor value from 8 bit component values
38*/
39static inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
40{
41 SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
42
43 return (a << 24) | (r << 16) | (g << 8) | (b << 0);
44}
45
46/** Return a SkColor value from 8 bit component values, with an implied value
47 of 0xFF for alpha (fully opaque)
48*/
49#define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
50
51/** return the alpha byte from a SkColor value */
52#define SkColorGetA(color) (((color) >> 24) & 0xFF)
53/** return the red byte from a SkColor value */
54#define SkColorGetR(color) (((color) >> 16) & 0xFF)
55/** return the green byte from a SkColor value */
56#define SkColorGetG(color) (((color) >> 8) & 0xFF)
57/** return the blue byte from a SkColor value */
58#define SkColorGetB(color) (((color) >> 0) & 0xFF)
59
60static inline SkColor SkColorSetA(SkColor c, U8CPU a) {
61 return (c & 0x00FFFFFF) | (a << 24);
62}
63
64// common colors
65
66#define SK_ColorBLACK 0xFF000000 //!< black SkColor value
67#define SK_ColorDKGRAY 0xFF444444 //!< dark gray SkColor value
68#define SK_ColorGRAY 0xFF888888 //!< gray SkColor value
69#define SK_ColorLTGRAY 0xFFCCCCCC //!< light gray SkColor value
70#define SK_ColorWHITE 0xFFFFFFFF //!< white SkColor value
71
72#define SK_ColorRED 0xFFFF0000 //!< red SkColor value
73#define SK_ColorGREEN 0xFF00FF00 //!< green SkColor value
74#define SK_ColorBLUE 0xFF0000FF //!< blue SkColor value
75#define SK_ColorYELLOW 0xFFFFFF00 //!< yellow SkColor value
76#define SK_ColorCYAN 0xFF00FFFF //!< cyan SkColor value
77#define SK_ColorMAGENTA 0xFFFF00FF //!< magenta SkColor value
78
79////////////////////////////////////////////////////////////////////////
80
81/** Convert RGB components to HSV.
82 hsv[0] is Hue [0 .. 360)
83 hsv[1] is Saturation [0...1]
84 hsv[2] is Value [0...1]
85 @param red red component value [0..255]
86 @param green green component value [0..255]
87 @param blue blue component value [0..255]
88 @param hsv 3 element array which holds the resulting HSV components.
89*/
90void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
91
92/** Convert the argb color to its HSV components.
93 hsv[0] is Hue [0 .. 360)
94 hsv[1] is Saturation [0...1]
95 hsv[2] is Value [0...1]
96 @param color the argb color to convert. Note: the alpha component is ignored.
97 @param hsv 3 element array which holds the resulting HSV components.
98*/
99static inline void SkColorToHSV(SkColor color, SkScalar hsv[3])
100{
101 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
102}
103
104/** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
105 hsv[0] is Hue [0 .. 360)
106 hsv[1] is Saturation [0...1]
107 hsv[2] is Value [0...1]
108 If hsv values are out of range, they are pinned.
109 @param alpha the alpha component of the returned argb color.
110 @param hsv 3 element array which holds the input HSV components.
111 @return the resulting argb color
112*/
113SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
114
115/** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
116 hsv[0] is Hue [0 .. 360)
117 hsv[1] is Saturation [0...1]
118 hsv[2] is Value [0...1]
119 If hsv values are out of range, they are pinned.
120 @param hsv 3 element array which holds the input HSV components.
121 @return the resulting argb color
122*/
123static inline SkColor SkHSVToColor(const SkScalar hsv[3])
124{
125 return SkHSVToColor(0xFF, hsv);
126}
127
128////////////////////////////////////////////////////////////////////////
129
130/** 32 bit ARGB color value, premultiplied. The byte order for this value is
131 configuration dependent, matching the format of kARGB32 bitmaps. This is different
132 from SkColor, which is nonpremultiplied, and is always in the same byte order.
133*/
134typedef uint32_t SkPMColor;
135
136/** Return a SkPMColor value from unpremultiplied 8 bit component values
137*/
138SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
139/** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
140 components by the color's alpha, and by arranging the bytes in a configuration
141 dependent order, to match the format of kARGB32 bitmaps.
142*/
143SkPMColor SkPreMultiplyColor(SkColor c);
144
145/** Define a function pointer type for combining two premultiplied colors
146*/
147typedef SkPMColor (*SkXfermodeProc)(SkPMColor src, SkPMColor dst);
148
149/** Define a function pointer type for combining a premultiplied src color
150 and a 16bit device color.
151*/
152typedef uint16_t (*SkXfermodeProc16)(SkPMColor src, uint16_t dst);
153
154#endif
155