blob: 67a47a297e46bf752eb75b63ac50c1d288f4a1e0 [file] [log] [blame]
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001#Topic Color
2#Alias Color_Reference
3
4#Subtopic Overview
Cary Clark682c58d2018-05-16 07:07:07 -04005#Populate
Cary Clark2d4bf5f2018-04-16 08:37:38 -04006##
7
8#Subtopic Define
9#Populate
10##
11
Cary Clark682c58d2018-05-16 07:07:07 -040012Color constants can be helpful to write code, documenting the meaning of values
13the represent transparency and color values. The use of Color constants is not
14required.
15
16#Subtopic Constant
17#Populate
18##
19
Cary Clark2d4bf5f2018-04-16 08:37:38 -040020#Subtopic Function
21#Populate
22##
23
24#Subtopic Typedef
25#Populate
26##
27
Cary Clark2d4bf5f2018-04-16 08:37:38 -040028# ------------------------------------------------------------------------------
29
Cary Clark682c58d2018-05-16 07:07:07 -040030#Subtopic Alpha
Cary Clark2d4bf5f2018-04-16 08:37:38 -040031
Cary Clark682c58d2018-05-16 07:07:07 -040032Alpha represents the transparency of Color. Color with Alpha of zero is fully
33transparent. Color with Alpha of 255 is fully opaque. Some, but not all pixel
34formats contain Alpha. Pixels with Alpha may store it as unsigned integers or
35floating point values. Unsigned integer Alpha ranges from zero, fully
36transparent, to all bits set, fully opaque. Floating point Alpha ranges from
37zero, fully transparent, to one, fully opaque.
38
39#Alias Alpha
40
41#Typedef uint8_t SkAlpha
42#Line # defines Alpha as eight bits ##
43
Cary Clarkffb3d682018-05-17 12:17:28 -040044#Code
45typedef uint8_t SkAlpha;
46##
47
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400488-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
49
50#Typedef ##
51
Cary Clark682c58d2018-05-16 07:07:07 -040052#Subtopic ##
53
Cary Clark2d4bf5f2018-04-16 08:37:38 -040054# ------------------------------------------------------------------------------
55
56#Typedef uint32_t SkColor
Cary Clark682c58d2018-05-16 07:07:07 -040057#Line # defines Color as 32 bits ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040058
Cary Clarkffb3d682018-05-17 12:17:28 -040059#Code
60typedef uint32_t SkColor;
61##
62
Cary Clark682c58d2018-05-16 07:07:07 -04006332-bit ARGB Color value, Unpremultiplied. Color components are always in
Cary Clark2d4bf5f2018-04-16 08:37:38 -040064a known order. This is different from SkPMColor, which has its bytes in a configuration
65dependent order, to match the format of kBGRA_8888_SkColorType bitmaps. SkColor
66is the type used to specify colors in SkPaint and in gradients.
67
Cary Clark682c58d2018-05-16 07:07:07 -040068Color that is Premultiplied has the same component values as Color
69that is Unpremultiplied if Alpha is 255, fully opaque, although may have the
70component values in a different order.
71
72#SeeAlso SkPMColor
73
Cary Clark2d4bf5f2018-04-16 08:37:38 -040074#Typedef ##
75
76# ------------------------------------------------------------------------------
77
78#Method static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
79#In Function
Cary Clarkffb3d682018-05-17 12:17:28 -040080#Line # returns Color_Alpha and RGB combined ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040081
Cary Clark682c58d2018-05-16 07:07:07 -040082Returns Color value from 8-bit component values. Asserts if SK_DEBUG is defined
83if a, r, g, or b exceed 255. Since Color is Unpremultiplied, a may be smaller
84than the largest of r, g, and b.
Cary Clark2d4bf5f2018-04-16 08:37:38 -040085
Cary Clark682c58d2018-05-16 07:07:07 -040086#Param a amount of Alpha, from fully transparent (0) to fully opaque (255) ##
Cary Clarkffb3d682018-05-17 12:17:28 -040087#Param r amount of red, from no red (0) to full red (255) ##
88#Param g amount of green, from no green (0) to full green (255) ##
89#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040090
Cary Clark682c58d2018-05-16 07:07:07 -040091#Return color and alpha, Unpremultiplied ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040092
93#Example
Cary Clark682c58d2018-05-16 07:07:07 -040094 canvas->drawColor(SK_ColorRED);
95 canvas->clipRect(SkRect::MakeWH(150, 150));
96 canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0xFF, 0x00));
97 canvas->clipRect(SkRect::MakeWH(75, 75));
98 canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0x00, 0xFF));
Cary Clark2d4bf5f2018-04-16 08:37:38 -040099##
100
Cary Clarkffb3d682018-05-17 12:17:28 -0400101#SeeAlso SkColorSetRGB SkPaint::setARGB SkPaint::setColor SkColorSetA
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400102
103#Method ##
104
105# ------------------------------------------------------------------------------
106
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400107#Define SkColorSetRGB
Cary Clark682c58d2018-05-16 07:07:07 -0400108#Line # returns opaque Color ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400109
110#Code
111###$
112 #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
113$$$#
114##
115
Cary Clark682c58d2018-05-16 07:07:07 -0400116Returns Color value from 8-bit component values, with Alpha set
117fully opaque to 255.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400118
Cary Clarkffb3d682018-05-17 12:17:28 -0400119#Param r amount of red, from no red (0) to full red (255) ##
120#Param g amount of green, from no green (0) to full green (255) ##
121#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clark682c58d2018-05-16 07:07:07 -0400122
123#Return color with opaque alpha ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400124
125#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400126 canvas->drawColor(SK_ColorRED);
127 canvas->clipRect(SkRect::MakeWH(150, 150));
128 canvas->drawColor(SkColorSetRGB(0x00, 0xFF, 0x00));
129 canvas->clipRect(SkRect::MakeWH(75, 75));
130 canvas->drawColor(SkColorSetRGB(0x00, 0x00, 0xFF));
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400131##
132
Cary Clarkffb3d682018-05-17 12:17:28 -0400133#SeeAlso SkColorSetARGB
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400134
135#Define ##
136
137# ------------------------------------------------------------------------------
138
139#Define SkColorGetA
Cary Clark682c58d2018-05-16 07:07:07 -0400140#Line # returns Alpha component ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400141
142#Code
143###$
144 #define SkColorGetA(color) (((color) >> 24) & 0xFF)
145$$$#
146##
147
Cary Clark682c58d2018-05-16 07:07:07 -0400148Returns Alpha byte from Color value.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400149
Cary Clark682c58d2018-05-16 07:07:07 -0400150#Param color SkColor, a 32-bit unsigned int, in 0xAARRGGBB format ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400151
152#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400153 SkPaint paint;
154 paint.setAntiAlias(true);
155 paint.setColor(SK_ColorRED);
156 for (int alpha = 255; alpha >= 0; alpha -= 17) {
157 paint.setAlpha(alpha);
158 canvas->drawRect({5, 5, 100, 20}, paint);
159 SkAlpha alphaInPaint = SkColorGetA(paint.getColor());
160 canvas->drawString(std::to_string(alphaInPaint).c_str(), 110, 18, paint);
161 canvas->translate(0, 15);
162 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400163##
164
Cary Clarkffb3d682018-05-17 12:17:28 -0400165#SeeAlso SkPaint::getAlpha
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400166
167#Define ##
168
169# ------------------------------------------------------------------------------
170
171#Define SkColorGetR
Cary Clarkffb3d682018-05-17 12:17:28 -0400172#Line # returns red component ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400173
174#Code
175###$
176 #define SkColorGetR(color) (((color) >> 16) & 0xFF)
177$$$#
178##
179
Cary Clark682c58d2018-05-16 07:07:07 -0400180Returns red component of Color, from zero to 255.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400181
Cary Clark682c58d2018-05-16 07:07:07 -0400182#Param color SkColor, a 32-bit unsigned int, in 0xAARRGGBB format ##
183#Return red byte ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400184
185#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400186#Image 3
187 canvas->drawBitmap(source, 0, 0);
188 SkPaint bgPaint;
189 bgPaint.setColor(0xafffffff);
190 canvas->drawRect({20, 50, 80, 70}, bgPaint);
191 uint8_t red = SkColorGetR(source.getColor(226, 128));
192 canvas->drawString(std::to_string(red).c_str(), 40, 65, SkPaint());
193 canvas->drawLine(80, 70, 226, 128, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400194##
195
Cary Clarkffb3d682018-05-17 12:17:28 -0400196#SeeAlso SkColorGetG SkColorGetB
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400197
198#Define ##
199
200# ------------------------------------------------------------------------------
201
202#Define SkColorGetG
Cary Clarkffb3d682018-05-17 12:17:28 -0400203#Line # returns green component ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400204
205#Code
206###$
207 #define SkColorGetG(color) (((color) >> 8) & 0xFF)
208$$$#
209##
210
Cary Clark682c58d2018-05-16 07:07:07 -0400211Returns green component of Color, from zero to 255.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400212
Cary Clark682c58d2018-05-16 07:07:07 -0400213#Param color SkColor, a 32-bit unsigned int, in 0xAARRGGBB format ##
214#Return green byte ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400215
216#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400217#Image 3
218 canvas->drawBitmap(source, 0, 0);
219 SkPaint bgPaint;
220 bgPaint.setColor(0xafffffff);
221 canvas->drawRect({20, 50, 80, 70}, bgPaint);
222 uint8_t green = SkColorGetG(source.getColor(57, 192));
223 canvas->drawString(std::to_string(green).c_str(), 40, 65, SkPaint());
224 canvas->drawLine(80, 70, 57, 192, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400225##
226
Cary Clarkffb3d682018-05-17 12:17:28 -0400227#SeeAlso SkColorGetR SkColorGetB
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400228
229#Define ##
230
231# ------------------------------------------------------------------------------
232
233#Define SkColorGetB
Cary Clarkffb3d682018-05-17 12:17:28 -0400234#Line # returns blue component ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400235
236#Code
237###$
238 #define SkColorGetB(color) (((color) >> 0) & 0xFF)
239$$$#
240##
241
Cary Clark682c58d2018-05-16 07:07:07 -0400242Returns blue component of Color, from zero to 255.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400243
Cary Clark682c58d2018-05-16 07:07:07 -0400244#Param color SkColor, a 32-bit unsigned int, in 0xAARRGGBB format ##
245#Return blue byte ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400246
247#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400248#Image 3
249 canvas->drawBitmap(source, 0, 0);
250 SkPaint bgPaint;
251 bgPaint.setColor(0xafffffff);
252 canvas->drawRect({20, 50, 80, 70}, bgPaint);
253 uint8_t blue = SkColorGetB(source.getColor(168, 170));
254 canvas->drawString(std::to_string(blue).c_str(), 40, 65, SkPaint());
255 canvas->drawLine(80, 70, 168, 170, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400256##
257
Cary Clarkffb3d682018-05-17 12:17:28 -0400258#SeeAlso SkColorGetR SkColorGetG
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400259
260#Define ##
261
262# ------------------------------------------------------------------------------
263
264#Method static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a)
265#In Function
Cary Clarkffb3d682018-05-17 12:17:28 -0400266#Line # returns Color with transparency ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400267
Cary Clarkffb3d682018-05-17 12:17:28 -0400268Returns Unpremultiplied Color with red, blue, and green set from c; and alpha set
269from a. Alpha component of c is ignored and is replaced by a in result.
Cary Clark682c58d2018-05-16 07:07:07 -0400270
Cary Clarkffb3d682018-05-17 12:17:28 -0400271#Param c packed RGB, eight bits per component ##
272#Param a Alpha: transparent at zero, fully opaque at 255 ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400273
Cary Clarkffb3d682018-05-17 12:17:28 -0400274#Return Color with transparency ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400275
276#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400277#Image 3
278 canvas->drawBitmap(source, 0, 0);
279 for (int y = 0; y < 256; y += 16) {
280 for (int x = 0; x < 256; x += 16) {
281 SkColor color = source.getColor(x + 8, y + 8);
282 SkPaint paint;
283 paint.setColor(SkColorSetA(color, x + y));
284 canvas->drawRect(SkRect::MakeXYWH(x, y, 16, 16), paint);
285 }
286 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400287##
288
Cary Clarkffb3d682018-05-17 12:17:28 -0400289#SeeAlso SkColorSetARGB
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400290
291#Method ##
292
293# ------------------------------------------------------------------------------
294
Cary Clark682c58d2018-05-16 07:07:07 -0400295#Subtopic Alpha_Constants
296#In Constant
297#Line # constants for Alpha ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400298
299#Code
Cary Clark682c58d2018-05-16 07:07:07 -0400300 constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
301 constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400302##
303
Cary Clark682c58d2018-05-16 07:07:07 -0400304Alpha constants are conveniences to represent fully transparent and fully
305opaque colors and masks. Their use is not required.
306
307#Const SK_AlphaTRANSPARENT 0x00
308#Line # fully transparent SkAlpha ##
309#Details Transparent
310Represents fully transparent SkAlpha value. SkAlpha ranges from zero,
311fully transparent; to 255, fully opaque.
312##
313#Const SK_AlphaOPAQUE 0xFF
314#Line # fully opaque SkAlpha ##
315#Details Opaque
316Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
317fully transparent; to 255, fully opaque.
318##
319
320#Subtopic Transparent
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400321
322#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400323#Image 1
324#Height 128
325#Description
326Color the parts of the bitmap red if they mostly contain transparent pixels.
327##
328 std::vector<int32_t> srcPixels;
329 srcPixels.resize(source.height() * source.rowBytes());
330 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width(), source.height()),
331 &srcPixels.front(), source.rowBytes());
332 source.readPixels(pixmap, 0, 0);
333 for (int y = 0; y < 16; ++y) {
334 for (int x = 0; x < 16; ++x) {
335 int32_t* blockStart = &srcPixels.front() + y * source.width() * 16 + x * 16;
336 size_t transparentCount = 0;
337 for (int fillY = 0; fillY < source.height() / 16; ++fillY) {
338 for (int fillX = 0; fillX < source.width() / 16; ++fillX) {
339 const SkColor color = SkUnPreMultiply::PMColorToColor(blockStart[fillX]);
340 transparentCount += SkColorGetA(color) == SK_AlphaTRANSPARENT;
341 }
342 blockStart += source.width();
343 }
344 if (transparentCount > 200) {
345 blockStart = &srcPixels.front() + y * source.width() * 16 + x * 16;
346 for (int fillY = 0; fillY < source.height() / 16; ++fillY) {
347 for (int fillX = 0; fillX < source.width() / 16; ++fillX) {
348 blockStart[fillX] = SK_ColorRED;
349 }
350 blockStart += source.width();
351 }
352 }
353 }
354 }
355 SkBitmap bitmap;
356 bitmap.installPixels(pixmap);
357 canvas->drawBitmap(bitmap, 0, 0);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400358##
359
Cary Clark682c58d2018-05-16 07:07:07 -0400360#SeeAlso SkAlpha SK_ColorTRANSPARENT SK_AlphaOPAQUE
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400361
Cary Clark682c58d2018-05-16 07:07:07 -0400362#Subtopic Transparent ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400363
364# ------------------------------------------------------------------------------
365
Cary Clark682c58d2018-05-16 07:07:07 -0400366#Subtopic Opaque
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400367
368#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400369#Image 1
370#Height 128
371 std::vector<int32_t> srcPixels;
372 srcPixels.resize(source.height() * source.rowBytes());
373 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width(), source.height()),
374 &srcPixels.front(), source.rowBytes());
375 source.readPixels(pixmap, 0, 0);
376 for (int y = 0; y < source.height(); ++y) {
377 for (int x = 0; x < source.width(); ++x) {
378 SkPMColor pixel = srcPixels[y * source.width() + x];
379 const SkColor color = SkUnPreMultiply::PMColorToColor(pixel);
380 if (SkColorGetA(color) == SK_AlphaOPAQUE) {
381 srcPixels[y * source.width() + x] = SK_ColorGREEN;
382 }
383 }
384 }
385 SkBitmap bitmap;
386 bitmap.installPixels(pixmap);
387 canvas->drawBitmap(bitmap, 0, 0);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400388##
389
Cary Clark682c58d2018-05-16 07:07:07 -0400390#SeeAlso SkAlpha SK_AlphaTRANSPARENT
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400391
Cary Clark682c58d2018-05-16 07:07:07 -0400392#Subtopic Opaque ##
393#Subtopic Alpha_Constants ##
394
395#Subtopic Color_Constants
396#In Constant
397#Line # constants for Color ##
398
399# ------------------------------------------------------------------------------
400#Code
401 constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
402 constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
403 constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
404 constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
405 constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
406 constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
407 constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
408 constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
409 constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
410 constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
411 constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
412 constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
413##
414
415Color names are provided as conveniences, but are not otherwise special.
416The values chosen for names may not be the same as values used by
417SVG, HTML, CSS, or colors named by a platform.
418
419#Example
420###$
421$Function
422#define SKIA_COLOR_PAIR(name) "SK_Color" #name, SK_Color##name
423$$
424void draw(SkCanvas* canvas) {
425 struct ColorCompare {
426 const char* fSVGName;
427 SkColor fSVGColor;
428 const char* fSkiaName;
429 SkColor fSkiaColor;
430 } colorCompare[] = { // see https://www.w3.org/TR/SVG/types.html#ColorKeywords
431 {"black", SkColorSetRGB( 0, 0, 0), SKIA_COLOR_PAIR(BLACK) },
432 {"darkgray", SkColorSetRGB(169, 169, 169), SKIA_COLOR_PAIR(DKGRAY) },
433 {"gray", SkColorSetRGB(128, 128, 128), SKIA_COLOR_PAIR(GRAY) },
434 {"lightgray", SkColorSetRGB(211, 211, 211), SKIA_COLOR_PAIR(LTGRAY) },
435 {"white", SkColorSetRGB(255, 255, 255), SKIA_COLOR_PAIR(WHITE) },
436 {"red", SkColorSetRGB(255, 0, 0), SKIA_COLOR_PAIR(RED) },
437 {"green", SkColorSetRGB( 0, 128, 0), SKIA_COLOR_PAIR(GREEN) },
438 {"blue", SkColorSetRGB( 0, 0, 255), SKIA_COLOR_PAIR(BLUE) },
439 {"yellow", SkColorSetRGB(255, 255, 0), SKIA_COLOR_PAIR(YELLOW) },
440 {"aqua", SkColorSetRGB( 0, 255, 255), SKIA_COLOR_PAIR(CYAN) },
441 {"fuchsia", SkColorSetRGB(255, 0, 255), SKIA_COLOR_PAIR(MAGENTA) },
442 };
443 SkPaint paint;
444 paint.setAntiAlias(true);
445 paint.setTextSize(14);
446 for (auto compare : colorCompare) {
447 paint.setStyle(SkPaint::kFill_Style);
448 paint.setColor(compare.fSVGColor);
449 canvas->drawRect({5, 5, 15, 15}, paint);
450 paint.setColor(SK_ColorBLACK);
451 canvas->drawString(compare.fSVGName, 20, 16, paint);
452 paint.setColor(compare.fSkiaColor);
453 canvas->drawRect({105, 5, 115, 15}, paint);
454 paint.setColor(SK_ColorBLACK);
455 canvas->drawString(compare.fSkiaName, 120, 16, paint);
456 paint.setStyle(SkPaint::kStroke_Style);
457 canvas->drawRect({5, 5, 15, 15}, paint);
458 canvas->drawRect({105, 5, 115, 15}, paint);
459 canvas->translate(0, 20);
460 }
461}
462$$$#
463##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400464
465# ------------------------------------------------------------------------------
466
Cary Clark682c58d2018-05-16 07:07:07 -0400467#Const SK_ColorTRANSPARENT 0x00000000
468#Line # transparent Color ##
469#Details Transparent
470 Represents fully transparent SkColor. May be used to initialize a destination
471 containing a mask or a non-rectangular image.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400472##
Cary Clark682c58d2018-05-16 07:07:07 -0400473#Const SK_ColorBLACK 0xFF000000
474#Line # black Color ##
475#Details Black
476 Represents fully opaque black.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400477##
Cary Clark682c58d2018-05-16 07:07:07 -0400478#Const SK_ColorDKGRAY 0xFF444444
479#Line # dark gray Color ##
480 Represents fully opaque dark gray.
481 Note that SVG_darkgray is equivalent to 0xFFA9A9A9.
482##
483#Const SK_ColorGRAY 0xFF888888
484#Line # gray Color ##
485 Represents fully opaque gray.
486 Note that HTML_Gray is equivalent to 0xFF808080.
487##
488#Const SK_ColorLTGRAY 0xFFCCCCCC
489#Line # light gray Color ##
490 Represents fully opaque light gray. HTML_Silver is equivalent to 0xFFC0C0C0.
491 Note that SVG_lightgray is equivalent to 0xFFD3D3D3.
492##
493#Const SK_ColorWHITE 0xFFFFFFFF
494#Line # white Color ##
495 Represents fully opaque white.
496##
497#Const SK_ColorRED 0xFFFF0000
498#Line # red Color ##
499 Represents fully opaque red.
500##
501#Const SK_ColorGREEN 0xFF00FF00
502#Line # green Color ##
503 Represents fully opaque green. HTML_Lime is equivalent.
504 Note that HTML_Green is equivalent to 0xFF008000.
505##
506#Const SK_ColorBLUE 0xFF0000FF
507#Line # blue Color ##
508 Represents fully opaque blue.
509##
510#Const SK_ColorYELLOW 0xFFFFFF00
511#Line # yellow Color ##
512 Represents fully opaque yellow.
513##
514#Const SK_ColorCYAN 0xFF00FFFF
515#Line # cyan Color ##
516 Represents fully opaque cyan. HTML_Aqua is equivalent.
517##
518#Const SK_ColorMAGENTA 0xFFFF00FF
519#Line # magenta Color ##
520 Represents fully opaque magenta. HTML_Fuchsia is equivalent.
521##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400522
523# ------------------------------------------------------------------------------
524
Cary Clark682c58d2018-05-16 07:07:07 -0400525#Subtopic Transparent
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400526
527#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400528#Image 3
Cary Clark682c58d2018-05-16 07:07:07 -0400529 std::vector<uint32_t> srcPixels;
530 constexpr int width = 256;
531 constexpr int height = 256;
532 srcPixels.resize(width * height);
533 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(width, height);
534 SkPixmap pixmap(imageInfo, &srcPixels.front(), imageInfo.minRowBytes());
535 pixmap.erase(SK_ColorTRANSPARENT);
536 pixmap.erase(SK_ColorRED, { 24, 24, 192, 192 } );
537 pixmap.erase(SK_ColorTRANSPARENT, { 48, 48, 168, 168 } );
538 SkBitmap bitmap;
539 bitmap.installPixels(pixmap);
540 canvas->drawBitmap(bitmap, 0, 0);
541 canvas->drawBitmap(bitmap, 48, 48);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400542##
543
Cary Clark682c58d2018-05-16 07:07:07 -0400544#SeeAlso SK_AlphaTRANSPARENT SkCanvas::clear
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400545
Cary Clark682c58d2018-05-16 07:07:07 -0400546##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400547
548# ------------------------------------------------------------------------------
549
Cary Clark682c58d2018-05-16 07:07:07 -0400550#Subtopic Black
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400551
552#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400553 std::vector<uint32_t> srcPixels;
554 constexpr int width = 256;
555 constexpr int height = 256;
556 srcPixels.resize(width * height);
557 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(width, height);
558 SkPixmap pixmap(imageInfo, &srcPixels.front(), imageInfo.minRowBytes());
559 pixmap.erase(SK_ColorTRANSPARENT);
560 pixmap.erase(SK_ColorRED, { 24, 24, 192, 192 } );
561 pixmap.erase(SK_ColorBLACK, { 48, 48, 168, 168 } );
562 SkBitmap bitmap;
563 bitmap.installPixels(pixmap);
564 canvas->drawBitmap(bitmap, 0, 0);
565 canvas->drawBitmap(bitmap, 48, 48);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400566##
567
Cary Clark682c58d2018-05-16 07:07:07 -0400568#SeeAlso SK_ColorTRANSPARENT
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400569
Cary Clark682c58d2018-05-16 07:07:07 -0400570##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400571
572# ------------------------------------------------------------------------------
573
Cary Clark682c58d2018-05-16 07:07:07 -0400574#Subtopic White
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400575
576#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400577 std::vector<uint32_t> srcPixels;
578 constexpr int width = 256;
579 constexpr int height = 256;
580 srcPixels.resize(width * height);
581 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(width, height);
582 SkPixmap pixmap(imageInfo, &srcPixels.front(), imageInfo.minRowBytes());
583 pixmap.erase(SK_ColorTRANSPARENT);
584 pixmap.erase(SK_ColorRED, { 24, 24, 192, 192 } );
585 pixmap.erase(SK_ColorWHITE, { 48, 48, 168, 168 } );
586 SkBitmap bitmap;
587 bitmap.installPixels(pixmap);
588 canvas->drawBitmap(bitmap, 0, 0);
589 canvas->drawBitmap(bitmap, 48, 48);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400590##
591
Cary Clark682c58d2018-05-16 07:07:07 -0400592#SeeAlso SK_ColorTRANSPARENT
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400593
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400594##
595
Cary Clark682c58d2018-05-16 07:07:07 -0400596#Subtopic Color_Constants ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400597
598# ------------------------------------------------------------------------------
599
600#Subtopic HSV
601
602#Subtopic Hue
603Hue represents an angle, in degrees, on a color wheel. Hue has a positive value
604modulo 360, where zero degrees is red.
605##
606
607#Subtopic Saturation
608##
609
610#Subtopic Value
611##
612
Cary Clarkffb3d682018-05-17 12:17:28 -0400613#Method void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3])
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400614#In Function
Cary Clarkffb3d682018-05-17 12:17:28 -0400615#Line # converts RGB to HSV ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400616
Cary Clarkffb3d682018-05-17 12:17:28 -0400617Converts RGB to its HSV components.
Cary Clark682c58d2018-05-16 07:07:07 -0400618hsv[0] contains Hue, a value from zero to less than 360.
619hsv[1] contains Saturation, a value from zero to one.
620hsv[2] contains Value, a value from zero to one.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400621
Cary Clark682c58d2018-05-16 07:07:07 -0400622#Param red red component value from zero to 255 ##
623#Param green green component value from zero to 255 ##
624#Param blue blue component value from zero to 255 ##
625#Param hsv three element array which holds the resulting HSV components
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400626##
627
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400628#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400629#Image 3
630 canvas->drawBitmap(source, 0, 0);
631 SkPaint bgPaint;
632 bgPaint.setColor(0xafffffff);
633 canvas->drawRect({20, 30, 110, 90}, bgPaint);
634 SkScalar hsv[3];
635 SkColor c = source.getColor(226, 128);
636 SkRGBToHSV(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c), hsv);
637 canvas->drawString(("h: " + std::to_string(hsv[0]).substr(0, 6)).c_str(), 27, 45, SkPaint());
638 canvas->drawString(("s: " + std::to_string(hsv[1]).substr(0, 6)).c_str(), 27, 65, SkPaint());
639 canvas->drawString(("v: " + std::to_string(hsv[2]).substr(0, 6)).c_str(), 27, 85, SkPaint());
640 canvas->drawLine(110, 90, 226, 128, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400641##
642
Cary Clarkffb3d682018-05-17 12:17:28 -0400643#SeeAlso SkColorToHSV SkHSVToColor
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400644
645#Method ##
646
647# ------------------------------------------------------------------------------
648
Cary Clarkffb3d682018-05-17 12:17:28 -0400649#Method void SkColorToHSV(SkColor color, SkScalar hsv[3])
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400650#In Function
Cary Clark682c58d2018-05-16 07:07:07 -0400651#Line # converts RGB to HSV ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400652
Cary Clark682c58d2018-05-16 07:07:07 -0400653Converts ARGB to its HSV components. Alpha in ARGB is ignored.
654hsv[0] contains Hue, and is assigned a value from zero to less than 360.
655hsv[1] contains Saturation, a value from zero to one.
656hsv[2] contains Value, a value from zero to one.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400657
Cary Clark682c58d2018-05-16 07:07:07 -0400658#Param color ARGB color to convert
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400659##
Cary Clark682c58d2018-05-16 07:07:07 -0400660#Param hsv three element array which holds the resulting HSV components
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400661##
662
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400663#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400664#Image 3
665 canvas->drawBitmap(source, 0, 0);
666 for (int y = 0; y < 256; ++y) {
667 for (int x = 0; x < 256; ++x) {
668 SkScalar hsv[3];
669 SkColorToHSV(source.getColor(x, y), hsv);
670 SkTSwap(hsv[1], hsv[2]);
671 SkPaint paint;
672 paint.setColor(SkHSVToColor(hsv));
673 canvas->drawRect(SkRect::MakeXYWH(x, y, 1, 1), paint);
674 }
675 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400676##
677
Cary Clarkffb3d682018-05-17 12:17:28 -0400678#SeeAlso SkRGBToHSV SkHSVToColor
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400679
680#Method ##
681
682# ------------------------------------------------------------------------------
683
Cary Clarkffb3d682018-05-17 12:17:28 -0400684#Method SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3])
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400685#In Function
Cary Clark682c58d2018-05-16 07:07:07 -0400686#Line # converts HSV with Alpha to RGB ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400687
Cary Clarkffb3d682018-05-17 12:17:28 -0400688Converts HSV components to an ARGB color. Alpha is passed through unchanged.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400689hsv[0] represents Hue, an angle from zero to less than 360.
690hsv[1] represents Saturation, and varies from zero to one.
691hsv[2] represents Value, and varies from zero to one.
692
Cary Clarkffb3d682018-05-17 12:17:28 -0400693Out of range hsv values are pinned.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400694
Cary Clark682c58d2018-05-16 07:07:07 -0400695#Param alpha Alpha component of the returned ARGB color
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400696##
Cary Clark682c58d2018-05-16 07:07:07 -0400697#Param hsv three element array which holds the input HSV components
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400698##
699
Cary Clark682c58d2018-05-16 07:07:07 -0400700#Return ARGB equivalent to HSV
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400701##
702
703#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400704#Image 3
705 canvas->drawBitmap(source, 0, 0);
706 for (int y = 0; y < 256; ++y) {
707 for (int x = 0; x < 256; ++x) {
708 SkColor color = source.getColor(x, y);
709 SkScalar hsv[3];
710 SkColorToHSV(color, hsv);
711 hsv[0] = hsv[0] + 90 >= 360 ? hsv[0] - 270 : hsv[0] + 90;
712 SkPaint paint;
713 paint.setColor(SkHSVToColor(x + y, hsv));
714 canvas->drawRect(SkRect::MakeXYWH(x, y, 1, 1), paint);
715 }
716 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400717##
718
Cary Clarkffb3d682018-05-17 12:17:28 -0400719#SeeAlso SkColorToHSV SkRGBToHSV
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400720
721#Method ##
722
723# ------------------------------------------------------------------------------
724
Cary Clarkffb3d682018-05-17 12:17:28 -0400725#Method SkColor SkHSVToColor(const SkScalar hsv[3])
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400726#In Function
Cary Clarkffb3d682018-05-17 12:17:28 -0400727#Line # converts HSV to RGB ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400728
Cary Clarkffb3d682018-05-17 12:17:28 -0400729Convert HSV components to an ARGB color. Alpha is set to 0xFF.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400730hsv[0] represents Hue, an angle from zero to less than 360.
731hsv[1] represents Saturation, and varies from zero to one.
732hsv[2] represents Value, and varies from zero to one.
733
Cary Clarkffb3d682018-05-17 12:17:28 -0400734Out of range hsv values are pinned.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400735
Cary Clarkffb3d682018-05-17 12:17:28 -0400736#Param hsv three element array which holds the input HSV components
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400737##
738
Cary Clarkffb3d682018-05-17 12:17:28 -0400739#Return RGB equivalent to HSV
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400740##
741
742#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400743#Image 3
744 canvas->drawBitmap(source, 0, 0);
745 for (int y = 0; y < 256; ++y) {
746 for (int x = 0; x < 256; ++x) {
747 SkColor color = source.getColor(x, y);
748 SkScalar hsv[3];
749 SkColorToHSV(color, hsv);
750 hsv[0] = hsv[0] + 90 >= 360 ? hsv[0] - 270 : hsv[0] + 90;
751 SkPaint paint;
752 paint.setColor(SkHSVToColor(hsv));
753 canvas->drawRect(SkRect::MakeXYWH(x, y, 1, 1), paint);
754 }
755 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400756##
757
Cary Clarkffb3d682018-05-17 12:17:28 -0400758#SeeAlso SkColorToHSV SkRGBToHSV
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400759
760#Method ##
761
762#Subtopic HSV ##
763
764# ------------------------------------------------------------------------------
765
Cary Clark682c58d2018-05-16 07:07:07 -0400766#Subtopic PMColor
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400767
Cary Clark682c58d2018-05-16 07:07:07 -0400768#Typedef uint32_t SkPMColor
769#Line # defines Premultiplied Color as 32 bits ##
770
Cary Clarkffb3d682018-05-17 12:17:28 -0400771#Code
772typedef uint32_t SkPMColor;
773##
774
Cary Clark682c58d2018-05-16 07:07:07 -040077532-bit ARGB color value, Premultiplied. The byte order for this value is
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400776configuration dependent, matching the format of kBGRA_8888_SkColorType bitmaps.
Cary Clark682c58d2018-05-16 07:07:07 -0400777This is different from SkColor, which is Unpremultiplied, and is always in the
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400778same byte order.
779
780#Typedef ##
781
782# ------------------------------------------------------------------------------
783
Cary Clarkffb3d682018-05-17 12:17:28 -0400784#Method SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400785#In Function
Cary Clark682c58d2018-05-16 07:07:07 -0400786#Line # converts Unpremultiplied ARGB to Premultiplied PMColor ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400787
Cary Clarkffb3d682018-05-17 12:17:28 -0400788Returns a SkPMColor value from Unpremultiplied 8-bit component values.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400789
Cary Clarkffb3d682018-05-17 12:17:28 -0400790#Param a amount of Alpha, from fully transparent (0) to fully opaque (255) ##
791#Param r amount of red, from no red (0) to full red (255) ##
792#Param g amount of green, from no green (0) to full green (255) ##
793#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400794
Cary Clarkffb3d682018-05-17 12:17:28 -0400795#Return Premultiplied Color ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400796
797#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400798#Height 128
799#Width 300
800 SkPMColor premultiplied = SkPreMultiplyARGB(160, 128, 160, 192);
801 canvas->drawString("Unpremultiplied:", 20, 20, SkPaint());
802 canvas->drawString("alpha=160 red=128 green=160 blue=192", 20, 40, SkPaint());
803 canvas->drawString("Premultiplied:", 20, 80, SkPaint());
804 std::string str = "alpha=" + std::to_string(SkColorGetA(premultiplied));
805 str += " red=" + std::to_string(SkColorGetR(premultiplied));
806 str += " green=" + std::to_string(SkColorGetG(premultiplied));
807 str += " blue=" + std::to_string(SkColorGetB(premultiplied));
808 canvas->drawString(str.c_str(), 20, 100, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400809##
810
Cary Clarkffb3d682018-05-17 12:17:28 -0400811#SeeAlso SkPreMultiplyColor
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400812
813#Method ##
814
815# ------------------------------------------------------------------------------
816
Cary Clarkffb3d682018-05-17 12:17:28 -0400817#Method SkPMColor SkPreMultiplyColor(SkColor c)
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400818#In Function
Cary Clark682c58d2018-05-16 07:07:07 -0400819#Line # converts Unpremultiplied Color to Premultiplied PMColor ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400820
Cary Clark682c58d2018-05-16 07:07:07 -0400821Returns PMColor closest to Color c. Multiplies c RGB components by the c Alpha,
822and arranges the bytes to match the format of kN32_SkColorType.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400823
Cary Clarkffb3d682018-05-17 12:17:28 -0400824#Param c Unpremultiplied ARGB Color ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400825
Cary Clarkffb3d682018-05-17 12:17:28 -0400826#Return Premultiplied Color ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400827
828#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400829#Height 128
830#Width 300
831 SkColor unpremultiplied = SkColorSetARGB(160, 128, 160, 192);
832 SkPMColor premultiplied = SkPreMultiplyColor(unpremultiplied);
833 canvas->drawString("Unpremultiplied:", 20, 20, SkPaint());
834 std::string str = "alpha=" + std::to_string(SkColorGetA(unpremultiplied));
835 str += " red=" + std::to_string(SkColorGetR(unpremultiplied));
836 str += " green=" + std::to_string(SkColorGetG(unpremultiplied));
837 str += " blue=" + std::to_string(SkColorGetB(unpremultiplied));
838 canvas->drawString(str.c_str(), 20, 40, SkPaint());
839 canvas->drawString("Premultiplied:", 20, 80, SkPaint());
840 str = "alpha=" + std::to_string(SkColorGetA(premultiplied));
841 str += " red=" + std::to_string(SkColorGetR(premultiplied));
842 str += " green=" + std::to_string(SkColorGetG(premultiplied));
843 str += " blue=" + std::to_string(SkColorGetB(premultiplied));
844 canvas->drawString(str.c_str(), 20, 100, SkPaint());
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400845##
846
Cary Clarkffb3d682018-05-17 12:17:28 -0400847#SeeAlso SkPreMultiplyARGB
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400848
849#Method ##
850
Cary Clark682c58d2018-05-16 07:07:07 -0400851#Subtopic PMColor ##
852
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400853#Topic Color ##