Library of Assembled Shared Sources
color_rgba.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44#include "lass_common.h"
45#include "color_rgba.h"
46#include "vector_3d.h"
47
48// for this translation unit, we don't want any warnings on numerical conversions.
49#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
50# pragma warning(disable: 4305 4244)
51#endif
52
53namespace lass
54{
55namespace prim
56{
57
58
59
60// --- public --------------------------------------------------------------------------------------
61
62/** construct an unexisting color (black with zero alpha)
63 */
65 r(TNumTraits::zero),
66 g(TNumTraits::zero),
67 b(TNumTraits::zero),
68 a(TNumTraits::zero)
69{
70 LASS_ASSERT(isZero());
71}
72
73
74
75/** construct a color with values for all channels
76 */
77ColorRGBA::ColorRGBA(TParam red, TParam green, TParam blue, TParam alpha):
78 r(red),
79 g(green),
80 b(blue),
81 a(alpha)
82{
83}
84
85
86
87/** construct a color in white range (r == g == b), with an alpha value
88 */
89ColorRGBA::ColorRGBA(TParam white, TParam alpha):
90 r(white),
91 g(white),
92 b(white),
93 a(alpha)
94{
95}
96
97
98
99/** construct a color from a raw vector
100 */
101ColorRGBA::ColorRGBA(const TVector& vector):
102 r(vector.x),
103 g(vector.y),
104 b(vector.z),
105 a(vector.w)
106{
107}
108
109
110
111/** @e raw addition of @a other to this color, including alpha channel
112 */
114{
115 r += other.r;
116 g += other.g;
117 b += other.b;
118 a += other.a;
119 return *this;
120}
121
122
123
124/** @e raw subtraction @a other from this color, including alpha channel
125 */
127{
128 r -= other.r;
129 g -= other.g;
130 b -= other.b;
131 a -= other.a;
132 return *this;
133}
134
135
136
137/** @e raw multiplication of @a other with this color, including alpha channel
138 */
140{
141 r *= other.r;
142 g *= other.g;
143 b *= other.b;
144 a *= other.a;
145 return *this;
146}
147
148
149
150/** @e raw division of this color by @a other, including alpha channel
151 */
153{
154 r /= other.r;
155 g /= other.g;
156 b /= other.b;
157 a /= other.a;
158 return *this;
159}
160
161
162
163/** @e raw addition of @a white to this color, including alpha channel
164 */
166{
167 r += white;
168 g += white;
169 b += white;
170 a += white;
171 return *this;
172}
173
174
175
176/** @e raw subtraction @a white from this color, including alpha channel
177 */
179{
180 r -= white;
181 g -= white;
182 b -= white;
183 a -= white;
184 return *this;
185}
186
187
188
189/** @e raw multiplication of @a white with this color, including alpha channel
190 */
192{
193 r *= white;
194 g *= white;
195 b *= white;
196 a *= white;
197 return *this;
198}
199
200
201
202/** @e raw division of this color by @a white, including alpha channel
203 */
205{
206 const TValue invWhite = TNumTraits::one / white;
207 r *= invWhite;
208 g *= invWhite;
209 b *= invWhite;
210 a *= invWhite;
211 return *this;
212}
213
214
215
216ColorRGBA::TValue ColorRGBA::brightness() const
217{
218 return (r + g + b) / 3;
219}
220
221
222
223/** return true if all color components are zero
224 */
226{
227 return r == TNumTraits::zero && g == TNumTraits::zero && b == TNumTraits::zero;
228}
229
230
231
232/** return true if all components are zero
233 */
235{
236 return r == TNumTraits::zero && g == TNumTraits::zero && b == TNumTraits::zero &&
237 a == TNumTraits::zero;
238}
239
240
241
242/** Return true if at least one of the components is NaN
243 */
245{
246 return num::isNaN(r) || num::isNaN(g) || num::isNaN(b) || num::isNaN(a);
247}
248
249
250
251/** return darkened colour without changing the opaqueness.
252 *
253 * @pre @a *this are considered non-premultiplied
254 *
255 * @code
256 * alphaR = alphaA.
257 * ColorR = ColorA * darkenFactor.
258 * @endcode
259 *
260 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
261 */
262const ColorRGBA ColorRGBA::darkened(ColorRGBA::TParam factor) const
263{
264 ColorRGBA result(*this);
265 result *= factor;
266 result.a = a;
267 return result;
268}
269
270
271
272/** return color with dissolved opaqueness
273 *
274 * @pre @a a and @a b are considered non-premultiplied
275 *
276 * @code
277 * alphaR = alphaA * dissolveFactor.
278 * ColorR = ColorA.
279 * @endcode
280 *
281 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
282 */
283const ColorRGBA ColorRGBA::dissolved(TParam factor) const
284{
285 ColorRGBA result(*this);
286 result.a *= factor;
287 return result;
288}
289
290
291
292/** return gamma corrected color.
293 *
294 * corrects gamma of all channels, except alpha.
295 *
296 * @pre @a a and @a b are considered non-premultiplied
297 *
298 * @code
299 * alphaR = alphaA.
300 * ColorR = ColorA ** (1 / gamma).
301 * @endcode
302 */
303const ColorRGBA ColorRGBA::gammaCorrected(TParam gammaExponent) const
304{
305 const TValue invGamma = num::inv(gammaExponent);
306 return ColorRGBA(num::pow(r, invGamma), num::pow(g, invGamma), num::pow(b, invGamma), a);
307}
308
309
310
311/** return exposed color.
312 *
313 * apply exposure function to color and clamp alpha channel
314 *
315 * @pre @a a and @a b are considered non-premultiplied
316 * @post: all channel values ar in the range [0, 1].
317 *
318 * @code
319 * alphaR = alphaA.
320 * ColorR = 1 - exp(-time * ColorA).
321 * @endcode
322 */
323const ColorRGBA ColorRGBA::exposed(TParam exposureTime) const
324{
325 const TValue f = -exposureTime;
326 return ColorRGBA(
327 num::clamp(TNumTraits::one - num::exp(f * r), TNumTraits::zero, TNumTraits::one),
328 num::clamp(TNumTraits::one - num::exp(f * g), TNumTraits::zero, TNumTraits::one),
329 num::clamp(TNumTraits::one - num::exp(f * b), TNumTraits::zero, TNumTraits::one),
330 num::clamp(a, TNumTraits::zero, TNumTraits::one));
331}
332
333
334
335/** return result of inverse exposure function
336 *
337 * apply inverse of exposure function to color and clamp alpha channel
338 *
339 * @pre @a a and @a b are considered non-premultiplied
340 * @post: all channel values ar in the range [0, 1].
341 *
342 * @code
343 * alphaR = alphaA.
344 * ColorR = log(1 - colorA) / -time
345 * @endcode
346 */
347const ColorRGBA ColorRGBA::invExposed(TParam exposureTime) const
348{
349 const TValue f = num::inv(-exposureTime);
350 return ColorRGBA(
351 f * num::log(num::clamp(TNumTraits::one - r, TNumTraits::minStrictPositive, TNumTraits::one)),
352 f * num::log(num::clamp(TNumTraits::one - g, TNumTraits::minStrictPositive, TNumTraits::one)),
353 f * num::log(num::clamp(TNumTraits::one - b, TNumTraits::minStrictPositive, TNumTraits::one)),
354 a);
355}
356
357
358
359/** clamp all channels (including alpha channel) to the range [0, 1].
360 * @post: all channel values ar in the range [0, 1].
361 */
363{
364 return ColorRGBA(
365 num::clamp(r, TNumTraits::zero, TNumTraits::one),
366 num::clamp(g, TNumTraits::zero, TNumTraits::one),
367 num::clamp(b, TNumTraits::zero, TNumTraits::one),
368 num::clamp(a, TNumTraits::zero, TNumTraits::one));
369}
370
371
372
373/** convert a value in range [0, 1] to a color like in colormap 'autumn' of matlab.
374 */
376{
377 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
378 return ColorRGBA(TNumTraits::one, value, TNumTraits::zero);
379}
380
381
382
383/** convert a value in range [0, 1] to a color like in colormap 'bone' of matlab.
384 */
385const ColorRGBA ColorRGBA::mapBone(TParam value)
386{
387 const static ColorRGBA keys[] =
388 {
389 ColorRGBA(0.f , 0.f , 0.f ),
390 ColorRGBA(0.3194f, 0.3194f, 0.4444f),
391 ColorRGBA(0.6528f, 0.7778f, 0.7778f),
392 ColorRGBA(1.f , 1.f , 1.f )
393 };
394 return doMap(value, keys, 4);
395}
396
397
398
399/** convert a value in range [0, 1] to a color like in colormap 'cool' of matlab.
400 */
401const ColorRGBA ColorRGBA::mapCool(TParam value)
402{
403 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
404 return ColorRGBA(value, TNumTraits::one - value, TNumTraits::one);
405}
406
407
408
409/** convert a value in range [0, 1] to a color like in colormap 'copper' of matlab.
410 */
412{
413 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
414 return ColorRGBA(value < .8f ? value / .8f : 1.f, .8f * value, .5f * value);
415}
416
417
418
419/** convert a value in range [0, 1] to a color like in colormap 'gray' of matlab.
420 */
421const ColorRGBA ColorRGBA::mapGray(TParam value)
422{
423 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
424 return ColorRGBA(value);
425}
426
427
428
429/** convert a value in range [0, 1] to a color like in colormap 'hot' of matlab.
430 */
431const ColorRGBA ColorRGBA::mapHot(TParam value)
432{
433 const static ColorRGBA keys[] =
434 {
435 ColorRGBA(0.f , 0.f , 0.f),
436 ColorRGBA(1.f / 3.f, 0.f , 0.f),
437 ColorRGBA(2.f / 3.f, 0.f , 0.f),
438 ColorRGBA(1.f , 0.f , 0.f),
439 ColorRGBA(1.f , 1.f / 3.f, 0.f),
440 ColorRGBA(1.f , 2.f / 3.f, 0.f),
441 ColorRGBA(1.f , 1.f , 0.f),
442 ColorRGBA(1.f , 1.f , .5f),
443 ColorRGBA(1.f , 1.f , 1.f)
444 };
445 return doMap(value, keys, 9);
446}
447
448
449
450/** convert a value to a color like in colormap 'hsv' of matlab.
451 */
452const ColorRGBA ColorRGBA::mapHsv(TParam value)
453{
454 const static ColorRGBA keys[] =
455 {
456 ColorRGBA(1.f, 0.f, 0.f),
457 ColorRGBA(1.f, 1.f, 0.f),
458 ColorRGBA(0.f, 1.f, 0.f),
459 ColorRGBA(0.f, 1.f, 1.f),
460 ColorRGBA(0.f, 0.f, 1.f),
461 ColorRGBA(1.f, 0.f, 1.f),
462 ColorRGBA(1.f, 0.f, 0.f)
463 };
464 return doMap(value - num::floor(value), keys, 7);
465}
466
467
468
469/** convert a value in range [0, 1] to a color like in colormap 'jet' of matlab.
470 */
471const ColorRGBA ColorRGBA::mapJet(TParam value)
472{
473 const static ColorRGBA keys[] =
474 {
475 ColorRGBA(0.f, 0.f, .5f), // 240
476 ColorRGBA(0.f, 0.f, 1.f), // 240
477 ColorRGBA(0.f, .5f, 1.f), // 210
478 ColorRGBA(0.f, 1.f, 1.f), // 180
479 ColorRGBA(.5f, 1.f, .5f), // 90
480 ColorRGBA(1.f, 1.f, 0.f), // 60
481 ColorRGBA(1.f, .5f, 0.f), // 30
482 ColorRGBA(1.f, .0f, 0.f), // 0
483 ColorRGBA(.5f, .0f, 0.f) // 0
484 };
485
486 return doMap(value, keys, 9);
487}
488
489
490
491/** convert a value in range [0, 1] to a color like in colormap 'pink' of matlab.
492 */
493const ColorRGBA ColorRGBA::mapPink(TParam value)
494{
495 const static ColorRGBA keys[] =
496 {
497 ColorRGBA(0.f , 0.f , 0.f ),
498 ColorRGBA(0.2955f, 0.1782f, 0.1782f),
499 ColorRGBA(0.4303f, 0.2722f, 0.2722f),
500 ColorRGBA(0.5320f, 0.3412f, 0.3412f),
501 ColorRGBA(0.6172f, 0.3984f, 0.3984f),
502 ColorRGBA(0.6920f, 0.4484f, 0.4484f),
503 ColorRGBA(0.7594f, 0.4933f, 0.4933f),
504 ColorRGBA(0.7868f, 0.5842f, 0.5345f),
505 ColorRGBA(0.8133f, 0.6627f, 0.5727f),
506 ColorRGBA(0.8389f, 0.7328f, 0.6086f),
507 ColorRGBA(0.8637f, 0.7968f, 0.6424f),
508 ColorRGBA(0.8879f, 0.8560f, 0.6746f),
509 ColorRGBA(0.9114f, 0.9114f, 0.7052f),
510 ColorRGBA(0.9344f, 0.9344f, 0.7893f),
511 ColorRGBA(0.9567f, 0.9567f, 0.8653f),
512 ColorRGBA(0.9786f, 0.9786f, 0.9351f),
513 ColorRGBA(1.f , 1.f , 1.f ),
514 };
515 return doMap(value, keys, 17);
516}
517
518
519
520/** convert a value in range [0, 1] to a color like in colormap 'spring' of matlab.
521 */
523{
524 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
525 return ColorRGBA(TNumTraits::one, value, TNumTraits::one - value);
526}
527
528
529
530/** convert a value in range [0, 1] to a color like in colormap 'summer' of matlab.
531 */
533{
534 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
535 return ColorRGBA(value, (TNumTraits::one + value) / 2, 0.4f * TNumTraits::one);
536}
537
538
539
540/** convert a value in range [0, 1] to a color like in colormap 'winter' of matlab.
541 */
543{
544 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
545 return ColorRGBA(0, TNumTraits::one - value, TNumTraits::one / 2);
546}
547
548
549
550/** convert a value in range [0, 1] to a color from a custom color map.
551 */
552const ColorRGBA ColorRGBA::mapCustom(TParam value, const std::vector<ColorRGBA>& iColorMap)
553{
554 LASS_ASSERT(static_cast<int>(iColorMap.size()) >= 0);
555 return doMap(value, &iColorMap[0], static_cast<int>(iColorMap.size()));
556}
557
558
559
560// --- protected -----------------------------------------------------------------------------------
561
562
563
564// --- private -------------------------------------------------------------------------------------
565
566const ColorRGBA ColorRGBA::doMap(TParam value, const ColorRGBA* map, int mapSize)
567{
568 LASS_ASSERT(mapSize > 1);
569
570 num::inpclamp(value, TNumTraits::zero, TNumTraits::one);
571 const TValue x = value * static_cast<TValue>(mapSize - 1);
572 const TValue x0 = num::floor(x);
573
574 const int i = static_cast<int>(x0);
575 LASS_ASSERT(i >= 0 && i < mapSize);
576
577 if (i == mapSize - 1)
578 {
579 return map[i];
580 }
581
582 const TValue dx = x - x0;
583 return map[i] * (TNumTraits::one - dx) + map[i + 1] * dx;
584}
585
586// --- free ----------------------------------------------------------------------------------------
587
588/** raw addition of @a a and @a b, including alpha channels
589 * @relates lass::prim::ColorRGBA
590 */
592{
593 ColorRGBA result(a);
594 result += b;
595 return result;
596}
597
598
599
600/** raw subtraction of @a a and @a b, including alpha channels
601 * @relates lass::prim::ColorRGBA
602 */
604{
605 ColorRGBA result(a);
606 result -= b;
607 return result;
608}
609
610
611
612/** raw addition of @a a and @a b, including alpha channels
613 * @relates lass::prim::ColorRGBA
614 */
616{
617 ColorRGBA result(a);
618 result *= b;
619 return result;
620}
621
622
623
624/** raw addition of @a a and @a b, including alpha channels
625 * @relates lass::prim::ColorRGBA
626 */
628{
629 ColorRGBA result(a);
630 result /= b;
631 return result;
632}
633
634
635
636/** raw addition of @a a and @a b, including alpha channels
637 * @relates lass::prim::ColorRGBA
638 */
639ColorRGBA operator+(ColorRGBA::TParam a, const ColorRGBA& b)
640{
641 ColorRGBA result(b);
642 result += a;
643 return result;
644}
645
646
647
648/** raw subtraction of @a a and @a b, including alpha channels
649 * @relates lass::prim::ColorRGBA
650 */
651ColorRGBA operator-(ColorRGBA::TParam a, const ColorRGBA& b)
652{
653 ColorRGBA result(a);
654 result -= b;
655 return result;
656}
657
658
659
660/** raw addition of @a a and @a b, including alpha channels
661 * @relates lass::prim::ColorRGBA
662 */
663ColorRGBA operator*(ColorRGBA::TParam a, const ColorRGBA& b)
664{
665 ColorRGBA result(b);
666 result *= a;
667 return result;
668}
669
670
671
672/** raw addition of @a a and @a b, including alpha channels
673 * @relates lass::prim::ColorRGBA
674 */
675ColorRGBA operator/(ColorRGBA::TParam a, const ColorRGBA& b)
676{
677 ColorRGBA result(a);
678 result /= b;
679 return result;
680}
681
682
683
684/** raw addition of @a a and @a b, including alpha channels
685 * @relates lass::prim::ColorRGBA
686 */
687ColorRGBA operator+(const ColorRGBA& a, ColorRGBA::TParam b)
688{
689 ColorRGBA result(a);
690 result += b;
691 return result;
692}
693
694
695
696/** raw subtraction of @a a and @a b, including alpha channels
697 * @relates lass::prim::ColorRGBA
698 */
699ColorRGBA operator-(const ColorRGBA& a, ColorRGBA::TParam b)
700{
701 ColorRGBA result(a);
702 result -= b;
703 return result;
704}
705
706
707
708/** raw addition of @a a and @a b, including alpha channels
709 * @relates lass::prim::ColorRGBA
710 */
711ColorRGBA operator*(const ColorRGBA& a, ColorRGBA::TParam b)
712{
713 ColorRGBA result(a);
714 result *= b;
715 return result;
716}
717
718
719
720/** raw addition of @a a and @a b, including alpha channels
721 * @relates lass::prim::ColorRGBA
722 */
723ColorRGBA operator/(const ColorRGBA& a, ColorRGBA::TParam b)
724{
725 ColorRGBA result(a);
726 result /= b;
727 return result;
728}
729
730
731
732/** placement of foreground @a a in front of background @a b.
733 *
734 * @a a is painted over @a b and leaves only a that part of @a b visible that isn't painted
735 * over: 1 - alphaA.
736 *
737 * @pre @a a and @a b are considered non-premultiplied
738 *
739 * @code
740 * alphaR = alphaA + (1 - alphaA) * alphaB.
741 * ColorR * alphaR = ColorA * alphaA + ColorB * alphaB * (1 - alphaA).
742 * @endcode
743 *
744 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
745 * @arg http://en.wikipedia.org/wiki/Alpha_channel
746 */
748{
749 const ColorRGBA::TValue unfilteredB = (ColorRGBA::TNumTraits::one - a.a) * b.a;
750 const ColorRGBA::TValue alphaR = a.a + unfilteredB;
751 ColorRGBA result(b);
752 result *= unfilteredB;
753 result *= a * a.a;
754 result /= alphaR;
755 result.a = alphaR;
756 return result;
757}
758
759
760
761/** part of @a a inside @a b.
762 *
763 * @a a is only painted where @a b is present, and @a b is not painted at all.
764 * This is the equivalent of only drawing @a clipped by the alpha channel of @a b
765 *
766 * @pre @a a and @a b are considered non-premultiplied
767 *
768 * @code
769 * alphaR = alphaA * alphaB.
770 * ColorR = ColorA.
771 * @endcode
772 *
773 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
774 * @arg http://en.wikipedia.org/wiki/Alpha_channel
775 */
776ColorRGBA in(const ColorRGBA& a, const ColorRGBA& b)
777{
778 ColorRGBA result(a);
779 result.a *= b.a;
780 return result;
781}
782
783
784
785/** @a a held out by @a b, part of @a a outside @a b.
786 *
787 * @a a is only painted where @a b is not present, and @a b is not painted at all.
788 * This is the equivalent of only drawing @a clipped by the inverse alpha channel of @a b.
789 *
790 * @pre @a a and @a b are considered non-premultiplied
791 *
792 * @code
793 * alphaR = alphaA * (1 - alphaB).
794 * ColorR = ColorA.
795 * @endcode
796 *
797 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
798 * @arg http://en.wikipedia.org/wiki/Alpha_channel
799 */
800ColorRGBA out(const ColorRGBA& a, const ColorRGBA& b)
801{
802 ColorRGBA result(a);
803 result.a *= (ColorRGBA::TNumTraits::one - b.a);
804 return result;
805}
806
807
808
809/** union of @a a in @a b and @a b out @a a.
810 *
811 * @a a atop @a b includes @a a where it's on top of @a b, otherwise it's @a b. But nothing
812 * outside @a b is included.
813 *
814 * @pre @a a and @a b are considered non-premultiplied
815 *
816 * @code
817 * alphaR = alphaB.
818 * ColorR = ColorA * alphaA + ColorB * (1 - alphaA).
819 * @endcode
820 *
821 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
822 * @arg http://en.wikipedia.org/wiki/Alpha_channel
823 */
825{
826 ColorRGBA result(a);
827 result *= a.a;
828 result += b * (ColorRGBA::TNumTraits::one - a.a);
829 result.a = b.a;
830 return result;
831}
832
833
834
835/** @pre @a a and @a b are considered non-premultiplied
836 *
837 * @code
838 * alphaR = alphaA + alphaB
839 * colorR * alphaR = colorA * alphaA + colorB * alphaB.
840 * @endcode
841 *
842 * @arg T. Porter, T. Duff. Compositing Digital Images, Comp. Graphics, 18(3):253-259, July 1984.
843 * @arg http://en.wikipedia.org/wiki/Alpha_channel
844 */
846{
847 ColorRGBA result(a);
848 result *= a;
849 result += b * b.a;
850 result /= a.a + b.a;
851 result.a = a.a + b.a;
852 return result;
853}
854
855
856
857/** @a a seen through color filter @a b.
858 *
859 * @pre @a a and @a b are considered non-premultiplied
860 *
861 * @code
862 * alphaR = alphaA.
863 * colorR = colorA * (1 - alphaB) + colorA * colorB * alphaB.
864 * @endcode
865 */
867{
868 ColorRGBA result(a);
869 result *= b;
870 result *= b.a;
871 result += a * (ColorRGBA::TNumTraits::one - a.b);
872 result.a = a.a;
873 return result;
874}
875
876
877
878/** distance between non-non-premultiplied colours as 3D points (alpha channel is disregarded).
879 * @return num::sqrt(dR ** 2 + dG ** 2 + dB ** 2)
880 */
881ColorRGBA::TValue distance(const ColorRGBA& a, const ColorRGBA& b)
882{
883 ColorRGBA delta(a);
884 delta -= b;
885 return num::sqrt(num::sqr(delta.r) + num::sqr(delta.g) + num::sqr(delta.a));
886}
887
888}
889
890}
891
892// EOF
an [0, 1] floating point RGB colour with Alpha channel.
Definition color_rgba.h:62
ColorRGBA operator-(const ColorRGBA &a, ColorRGBA::TParam b)
raw subtraction of a and b, including alpha channels
ColorRGBA operator*(ColorRGBA::TParam a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
ColorRGBA & operator-=(const ColorRGBA &other)
raw subtraction other from this color, including alpha channel
static const ColorRGBA mapWinter(TParam value)
convert a value in range [0, 1] to a color like in colormap 'winter' of matlab.
static const ColorRGBA mapHot(TParam value)
convert a value in range [0, 1] to a color like in colormap 'hot' of matlab.
static const ColorRGBA mapPink(TParam value)
convert a value in range [0, 1] to a color like in colormap 'pink' of matlab.
ColorRGBA operator/(const ColorRGBA &a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
ColorRGBA operator/(const ColorRGBA &a, ColorRGBA::TParam b)
raw addition of a and b, including alpha channels
const ColorRGBA darkened(TParam factor) const
return darkened colour without changing the opaqueness.
static const ColorRGBA mapCopper(TParam value)
convert a value in range [0, 1] to a color like in colormap 'copper' of matlab.
bool isBlack() const
return true if all color components are zero
static const ColorRGBA mapCool(TParam value)
convert a value in range [0, 1] to a color like in colormap 'cool' of matlab.
const ColorRGBA exposed(TParam time) const
return exposed color.
static const ColorRGBA mapBone(TParam value)
convert a value in range [0, 1] to a color like in colormap 'bone' of matlab.
ColorRGBA operator-(const ColorRGBA &a, const ColorRGBA &b)
raw subtraction of a and b, including alpha channels
ColorRGBA operator+(ColorRGBA::TParam a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
static const ColorRGBA mapJet(TParam value)
convert a value in range [0, 1] to a color like in colormap 'jet' of matlab.
static const ColorRGBA mapAutumn(TParam value)
convert a value in range [0, 1] to a color like in colormap 'autumn' of matlab.
ColorRGBA()
construct an unexisting color (black with zero alpha)
bool isNaN() const
Return true if at least one of the components is NaN.
ColorRGBA operator+(const ColorRGBA &a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
const ColorRGBA invExposed(TParam time) const
return result of inverse exposure function
static const ColorRGBA mapGray(TParam value)
convert a value in range [0, 1] to a color like in colormap 'gray' of matlab.
static const ColorRGBA mapSummer(TParam value)
convert a value in range [0, 1] to a color like in colormap 'summer' of matlab.
const ColorRGBA clamped() const
clamp all channels (including alpha channel) to the range [0, 1].
ColorRGBA & operator*=(const ColorRGBA &other)
raw multiplication of other with this color, including alpha channel
ColorRGBA operator-(ColorRGBA::TParam a, const ColorRGBA &b)
raw subtraction of a and b, including alpha channels
const ColorRGBA gammaCorrected(TParam gamma) const
return gamma corrected color.
static const ColorRGBA mapCustom(TParam value, const std::vector< ColorRGBA > &colorMap)
convert a value in range [0, 1] to a color from a custom color map.
ColorRGBA operator/(ColorRGBA::TParam a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
const ColorRGBA dissolved(TParam factor) const
return color with dissolved opaqueness
ColorRGBA operator*(const ColorRGBA &a, ColorRGBA::TParam b)
raw addition of a and b, including alpha channels
ColorRGBA & operator+=(const ColorRGBA &other)
raw addition of other to this color, including alpha channel
static const ColorRGBA mapHsv(TParam value)
convert a value to a color like in colormap 'hsv' of matlab.
ColorRGBA & operator/=(const ColorRGBA &other)
raw division of this color by other, including alpha channel
ColorRGBA operator*(const ColorRGBA &a, const ColorRGBA &b)
raw addition of a and b, including alpha channels
static const ColorRGBA mapSpring(TParam value)
convert a value in range [0, 1] to a color like in colormap 'spring' of matlab.
bool isZero() const
return true if all components are zero
ColorRGBA operator+(const ColorRGBA &a, ColorRGBA::TParam b)
raw addition of a and b, including alpha channels
T sqr(const T &x)
return x * x
Definition basic_ops.h:162
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
const T & clamp(const T &x, const T &min, const T &max)
if x < min return min, else if x > max return max, else return x.
Definition basic_ops.h:226
T pow(const T &x, const T &p)
return exp(p * log(x));
Definition basic_ops.h:187
set of geometrical primitives
Definition aabb_2d.h:81
ColorRGBA plus(const ColorRGBA &a, const ColorRGBA &b)
ColorRGBA over(const ColorRGBA &a, const ColorRGBA &b)
placement of foreground a in front of background b.
ColorRGBA out(const ColorRGBA &a, const ColorRGBA &b)
a held out by b, part of a outside b.
ColorRGBA through(const ColorRGBA &a, const ColorRGBA &b)
a seen through color filter b.
ColorRGBA in(const ColorRGBA &a, const ColorRGBA &b)
part of a inside b.
ColorRGBA atop(const ColorRGBA &a, const ColorRGBA &b)
union of a in b and b out a.
Library for Assembled Shared Sources.
Definition config.h:53