blob: 9bb85965c64dc66ddc883f25e6cc40bbc2c94e5e [file] [log] [blame]
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001#Topic Color4f
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Color4f_Reference ##
Cary Clark61313f32018-10-08 14:57:48 -04003#Substitute SkColor4f
Cary Clark2d4bf5f2018-04-16 08:37:38 -04004
Cary Clark61313f32018-10-08 14:57:48 -04005#Struct SkPM4f
6##
7
8#Struct SkRGBA4f
9
10#Code
11#Populate
12##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040013
Cary Clarkffb3d682018-05-17 12:17:28 -040014Each component is stored as a 32-bit single precision floating point float value.
15All values are allowed, but only the range from zero to one is meaningful.
16
Cary Clark61313f32018-10-08 14:57:48 -040017Components are independent of the others if defined with kUnpremul_SkAlphaType;
18fA Alpha is may be greater or smaller than fG green, fB blue, or fR red.
19SkColor4f is shorthand for Unpremultiplied SkRGBA4f.
20
21Components are connnected if defined with kPremul_SkAlphaType;
22fA Alpha is equal to or larger than fG green, fB blue, and fR red. The values
23stored in fG, fB, and fR combine the color component with the Alpha component.
Cary Clarkffb3d682018-05-17 12:17:28 -040024
25Values smaller than zero or larger than one are allowed. Values out of range
26may be used with Blend_Mode so that the final component is in range.
Cary Clark2d4bf5f2018-04-16 08:37:38 -040027
Cary Clark2d4bf5f2018-04-16 08:37:38 -040028#Member float fR
Cary Clarkffb3d682018-05-17 12:17:28 -040029#Line # red component ##
30Single precision float for red ranges from no red (0.0) to full red (1.0).
Cary Clark2d4bf5f2018-04-16 08:37:38 -040031##
32
33#Member float fG
Cary Clarkffb3d682018-05-17 12:17:28 -040034#Line # green component ##
35Single precision float for green ranges from no green (0.0) to full green (1.0).
Cary Clark2d4bf5f2018-04-16 08:37:38 -040036##
37
38#Member float fB
Cary Clarkffb3d682018-05-17 12:17:28 -040039#Line # blue component ##
40Single precision float for blue ranges from no blue (0.0) to full blue (1.0).
Cary Clark2d4bf5f2018-04-16 08:37:38 -040041##
42
43#Member float fA
Cary Clarkffb3d682018-05-17 12:17:28 -040044#Line # alpha component ##
45Single precision float for Alpha ranges from no Alpha (0.0) to full Alpha (1.0).
Cary Clark2d4bf5f2018-04-16 08:37:38 -040046##
47
Cary Clark61313f32018-10-08 14:57:48 -040048
Cary Clark2d4bf5f2018-04-16 08:37:38 -040049# ------------------------------------------------------------------------------
50
Cary Clark61313f32018-10-08 14:57:48 -040051#Method bool operator==(const SkRGBA4f& other)_const
52#Line # compares SkRGBA4f for equality ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040053
Cary Clark61313f32018-10-08 14:57:48 -040054Compares SkRGBA4f with other, and returns true if all components are equivalent.
Cary Clark2d4bf5f2018-04-16 08:37:38 -040055
Cary Clark61313f32018-10-08 14:57:48 -040056#Param other SkRGBA4f to compare ##
Cary Clarkffb3d682018-05-17 12:17:28 -040057
Cary Clark61313f32018-10-08 14:57:48 -040058#Return true if SkRGBA4f equals other ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040059
60#Example
Cary Clarkffb3d682018-05-17 12:17:28 -040061 SkColor4f colorRed = { 1, 0, 0, 1 };
62 SkColor4f colorNamedRed = SkColor4f::FromColor(SK_ColorRED);
63 SkDebugf("colorRed %c= colorNamedRed", colorRed == colorNamedRed ? '=' : '!');
64#StdOut
65colorRed == colorNamedRed
66##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040067##
68
Cary Clark61313f32018-10-08 14:57:48 -040069#SeeAlso operator!=(const SkRGBA4f& other)_const
Cary Clark2d4bf5f2018-04-16 08:37:38 -040070
71#Method ##
72
73# ------------------------------------------------------------------------------
74
Cary Clark61313f32018-10-08 14:57:48 -040075#Method bool operator!=(const SkRGBA4f& other)_const
76#Line # compares SkRGBA4f for inequality ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040077
Cary Clark61313f32018-10-08 14:57:48 -040078Compares SkRGBA4f with other, and returns true if all components are not
Cary Clarkffb3d682018-05-17 12:17:28 -040079equivalent.
Cary Clark2d4bf5f2018-04-16 08:37:38 -040080
Cary Clark61313f32018-10-08 14:57:48 -040081#Param other SkRGBA4f to compare ##
Cary Clarkffb3d682018-05-17 12:17:28 -040082
Cary Clark61313f32018-10-08 14:57:48 -040083#Return true if SkRGBA4f is not equal to other ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040084
85#Example
Cary Clarkffb3d682018-05-17 12:17:28 -040086 SkColor4f colorGray = { .5, .5, .5, 1 };
87 SkColor4f colorNamedGray = SkColor4f::FromColor(SK_ColorGRAY);
88 SkDebugf("colorGray %c= colorNamedGray ", colorGray != colorNamedGray ? '!' : '=');
89#StdOut
90colorGray != colorNamedGray
91##
Cary Clark2d4bf5f2018-04-16 08:37:38 -040092##
93
Cary Clark61313f32018-10-08 14:57:48 -040094#SeeAlso operator==(const SkRGBA4f& other)_const
95
96#Method ##
97
98#Method SkRGBA4f operator*(float scale)_const
99#Line # multiplies components by scale ##
100
101Multiplies each component by scale. Does not pin the result.
102
103#Param scale component multiplier ##
104
105#Return scaled color ##
106
107#NoExample
108##
109
110#SeeAlso SkBlendMode::kMultiply
111
112#Method ##
113
114#Method SkRGBA4f operator*(const SkRGBA4f& scale)_const
115
116Multiplies each component by scale component. Does not pin the result.
117
118#Param scale SkRGBA4f component multipliers ##
119
120#Return scaled color ##
121
122#NoExample
123##
124
125#SeeAlso SkBlendMode::kMultiply
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400126
127#Method ##
128
129# ------------------------------------------------------------------------------
130
Cary Clark61313f32018-10-08 14:57:48 -0400131#Subtopic Property_Functions
132#Line # member values ##
133#Subtopic Property_Functions ##
134
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400135#Method const float* vec() const
Cary Clark61313f32018-10-08 14:57:48 -0400136#In Property_Functions
Cary Clarkffb3d682018-05-17 12:17:28 -0400137#Line # returns array of components ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400138
Cary Clark61313f32018-10-08 14:57:48 -0400139Returns SkRGBA4f components as a read-only array.
Cary Clarkffb3d682018-05-17 12:17:28 -0400140
141#Return components as read-only array ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400142
143#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400144 SkColor4f color = SkColor4f::FromColor(0x884488CC);
145 SkDebugf("red=%g green=%g blue=%g alpha=%g\n", color.fR, color.fG, color.fB, color.fA);
146 const float* array = color.vec();
147 SkDebugf("[0]=%g [1]=%g [2]=%g [3]=%g\n", array[0], array[1], array[2], array[3]);
148#StdOut
Cary Clark5b1f9532018-08-28 14:53:37 -0400149red=0.266667 green=0.533333 blue=0.8 alpha=0.533333
150[0]=0.266667 [1]=0.533333 [2]=0.8 [3]=0.533333
Cary Clarkffb3d682018-05-17 12:17:28 -0400151##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400152##
153
Cary Clarkffb3d682018-05-17 12:17:28 -0400154#SeeAlso SkColor4f
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400155
156#Method ##
157
158# ------------------------------------------------------------------------------
159
160#Method float* vec()
Cary Clark61313f32018-10-08 14:57:48 -0400161#In Property_Functions
Cary Clarkffb3d682018-05-17 12:17:28 -0400162#Line # returns array of components ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400163
Cary Clark61313f32018-10-08 14:57:48 -0400164Returns SkRGBA4f components as a writable array.
Cary Clarkffb3d682018-05-17 12:17:28 -0400165
166#Return components as writable array ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400167
168#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400169 SkColor4f color = SkColor4f::FromColor(0x884488CC);
170 SkDebugf("red=%g green=%g blue=%g alpha=%g\n", color.fR, color.fG, color.fB, color.fA);
171 float* array = color.vec();
172 array[3] = 1;
173 SkDebugf("[0]=%g [1]=%g [2]=%g [3]=%g\n", array[0], array[1], array[2], array[3]);
174#StdOut
Cary Clark5b1f9532018-08-28 14:53:37 -0400175red=0.266667 green=0.533333 blue=0.8 alpha=0.533333
176[0]=0.266667 [1]=0.533333 [2]=0.8 [3]=1
Cary Clarkffb3d682018-05-17 12:17:28 -0400177##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400178##
179
Cary Clarkffb3d682018-05-17 12:17:28 -0400180#SeeAlso SkColor4f
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400181
182#Method ##
183
Cary Clark61313f32018-10-08 14:57:48 -0400184#Method float operator[](int index)_const
185#Line # returns component by index ##
186
187Returns SkRGBA4f component by index, zero through three. index out of range
188triggers an assert in debug builds.
189
190#Param index component, zero through three ##
191#Return component by index ##
192
193#NoExample
194##
195
196#SeeAlso vec
197
198#Method ##
199
200#Method float& operator[](int index)
201#Line # returns writable component reference ##
202
203Returns writable component reference by index, zero through three. index out of range
204triggers an assert in debug builds.
205
206#Param index component, zero through three ##
207#Return writable component reference by index ##
208
209#NoExample
210##
211
212#SeeAlso vec
213
214#Method ##
215
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400216# ------------------------------------------------------------------------------
217
Cary Clark61313f32018-10-08 14:57:48 -0400218#Subtopic Utility_Functions
219#Line # less common functions ##
220#Subtopic Utility_Functions ##
221
222#Method bool isOpaque() const
223#In Utility_Functions
224#Line # returns if Alpha component is at maximum ##
225
226Returns true if Alpha component is one. Color has no transparency regardless of
227whether color is Premultiplied or Unpremultiplied. Triggers a debugging assert
228if Alpha not valid.
229
230#Return true if Alpha is one ##
231
232#NoExample
233##
234
235#SeeAlso vec SkColorGetA
236
237##
238
239# ------------------------------------------------------------------------------
240
241#Method static SkRGBA4f Pin(float r, float g, float b, float a)
242#In Utility_Functions
Cary Clarkffb3d682018-05-17 12:17:28 -0400243#Line # sets components to valid range ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400244
Cary Clark61313f32018-10-08 14:57:48 -0400245Constructs and returns SkRGBA4f with each component pinned from zero to one.
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400246
Cary Clarkffb3d682018-05-17 12:17:28 -0400247#Param r red component ##
248#Param g green component ##
249#Param b blue component ##
250#Param a Alpha component ##
251
252#Return Color4f with valid components ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400253
254#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400255#Height 40
256 uint32_t storage[8];
257 SkImageInfo info = SkImageInfo::MakeN32Premul(8, 1);
258 SkPixmap pixmap(info, storage, info.minRowBytes());
259 pixmap.erase(SK_ColorWHITE);
260 SkIRect bounds = {0, 0, 1, 1};
261 SkColor4f colors[] = { SkColor4f::Pin(1.5, 0.45f, 0.0, 1),
262 SkColor4f::Pin(1, 0.45f, -0.25, 1),
263 {1.5, 0.45f, 0.0, 1},
264 {1, 0.45f, -0.25, 1},
265 };
266 for (auto color4f : colors) {
267 pixmap.erase(color4f, &bounds);
268 bounds.offset(2, 0);
269 }
270 SkBitmap bitmap;
271 canvas->scale(20, 20);
272 bitmap.installPixels(pixmap);
273 canvas->drawBitmap(bitmap, 0, 0);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400274##
275
Cary Clarkffb3d682018-05-17 12:17:28 -0400276#SeeAlso pin() FromColor
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400277
278#Method ##
279
280# ------------------------------------------------------------------------------
281
Cary Clark61313f32018-10-08 14:57:48 -0400282#Method SkRGBA4f pin() const
283#In Utility_Functions
Cary Clarkffb3d682018-05-17 12:17:28 -0400284#Line # sets components to valid range ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400285
Cary Clark61313f32018-10-08 14:57:48 -0400286Returns SkRGBA4f with all components in the range from zero to one.
Cary Clarkffb3d682018-05-17 12:17:28 -0400287
Cary Clark61313f32018-10-08 14:57:48 -0400288#Return SkRGBA4f with valid components ##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400289
290#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400291#Height 40
292 uint32_t storage[8];
293 SkImageInfo info = SkImageInfo::MakeN32Premul(8, 1);
294 SkPixmap pixmap(info, storage, info.minRowBytes());
295 pixmap.erase(SK_ColorWHITE);
296 SkIRect bounds = {0, 0, 1, 1};
297 SkColor4f colors[] = { {1.5, 0.45f, 0.0, 1},
298 {1, 0.45f, -0.25, 1},
299 };
300 for (auto color4f : colors) {
301 pixmap.erase(color4f, &bounds);
302 bounds.offset(2, 0);
303 pixmap.erase(color4f.pin(), &bounds);
304 bounds.offset(2, 0);
305 }
306 SkBitmap bitmap;
307 canvas->scale(20, 20);
308 bitmap.installPixels(pixmap);
309 canvas->drawBitmap(bitmap, 0, 0);
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400310##
311
Cary Clarkffb3d682018-05-17 12:17:28 -0400312#SeeAlso Pin
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400313
314#Method ##
315
316# ------------------------------------------------------------------------------
317
Cary Clark61313f32018-10-08 14:57:48 -0400318#Method static SkRGBA4f FromColor(SkColor)
319#In Utility_Functions
320#Line # sets components from Color ##
321
322Converts to closest SkRGBA4f.
323
324#Param SkColor Color with Alpha, red, blue, and green components ##
325
326#Return SkRGBA4f equivalent ##
327
328#Example
329 uint8_t red = 77, green = 101, blue = 153, alpha = 43;
330 SkColor argb = SkColorSetARGB(alpha, red, green, blue);
331 SkColor4f color4f = SkColor4f::FromColor(argb);
332 SkDebugf("red=%g green=%g blue=%g alpha=%g\n", color4f.fR, color4f.fG, color4f.fB, color4f.fA);
333 SkColor fromColor4f = color4f.toSkColor();
334 SkDebugf("red=%d green=%d blue=%d alpha=%d\n", SkColorGetR(fromColor4f),
335 SkColorGetG(fromColor4f), SkColorGetB(fromColor4f), SkColorGetA(fromColor4f));
336#StdOut
337red=0.301961 green=0.396078 blue=0.6 alpha=0.168627
338red=77 green=101 blue=153 alpha=43
339##
340##
341
342#SeeAlso toSkColor
343
344#Method ##
345
346# ------------------------------------------------------------------------------
347
348#Method SkColor toSkColor() const
349#In Utility_Functions
350#Line # returns closest Color ##
351
352Converts to closest SkColor.
353
354#Return closest Color ##
355
356#Example
357 float red = 0.07, green = 0.13, blue = 0.32, alpha = 0.17;
358 SkColor4f color4f = { red, green, blue, alpha };
359 SkColor argb = color4f.toSkColor();
360 SkDebugf("red=%d green=%d blue=%d alpha=%d\n", SkColorGetR(argb),
361 SkColorGetG(argb), SkColorGetB(argb), SkColorGetA(argb));
362 SkColor4f fromSkColor = SkColor4f::FromColor(argb);
363 SkDebugf("red=%g green=%g blue=%g alpha=%g\n", fromSkColor.fR, fromSkColor.fG,
364 fromSkColor.fB, fromSkColor.fA);
365#StdOut
366red=18 green=33 blue=82 alpha=43
367red=0.0705882 green=0.129412 blue=0.321569 alpha=0.168627
368##
369##
370
371#SeeAlso FromColor
372
373#Method ##
374
375# ------------------------------------------------------------------------------
376
377#Method static SkRGBA4f FromPMColor(SkPMColor)
378#In Utility_Functions
379#Line # converts from Premultiplied Color ##
380
381Converts from Premultiplied integer components to Unpremultiplied float
382components.
383
384#Param SkPMColor Premultiplied color ##
385
386#Return Unpremultiplied color ##
387
388#NoExample
389##
390
391#SeeAlso FromColor
392
393#Method ##
394
395# ------------------------------------------------------------------------------
396
397#Method SkRGBA4f<kPremul_SkAlphaType> premul() const
Cary Clarkb94f6da2018-06-08 11:54:32 -0400398#In Utility
Cary Clarkc9b49fe2018-09-21 14:26:30 -0400399#Line # returns Premultiplied color ##
400
401Returns SkColor4f with all components premultiplied by Alpha.
402
403#Return Premultiplied color ##
404
405#NoExample
406##
407
408#SeeAlso unpremul
409
410#Method ##
411
Cary Clark61313f32018-10-08 14:57:48 -0400412#Method SkRGBA4f<kUnpremul_SkAlphaType> unpremul() const
Cary Clarkc9b49fe2018-09-21 14:26:30 -0400413#In Utility
414#Line # returns Unpremultiplied color ##
415
Cary Clark61313f32018-10-08 14:57:48 -0400416Returns SkRGBA4f with all components independent of Alpha.
Cary Clarkc9b49fe2018-09-21 14:26:30 -0400417
418#Return Unpremultiplied color ##
419
420#NoExample
421##
422
423#SeeAlso premul
424
425#Method ##
426
Cary Clark61313f32018-10-08 14:57:48 -0400427#Struct ##
Cary Clarkc9b49fe2018-09-21 14:26:30 -0400428
Cary Clark61313f32018-10-08 14:57:48 -0400429#Typedef SkRGBA4f SkColor4f
430#Line # defines Unpremultiplied Color using floats ##
431
432#Code
433using SkColor4f = SkRGBA4f<kUnpremul_SkAlphaType>;
Cary Clarkb94f6da2018-06-08 11:54:32 -0400434##
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400435
Cary Clark682c58d2018-05-16 07:07:07 -0400436##
437
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400438#Topic Color4f ##