cgv
color.h
1 #pragma once
2 
3 #include <iostream>
4 #include <cgv/defines/assert.h>
5 #include <cgv/type/func/promote.h>
6 #include <cgv/type/traits/max.h>
7 #include <cgv/type/info/type_name.h>
8 #include "color_model.hh"
9 #include <math.h>
10 
11 namespace cgv {
12  namespace media {
13 
14 template <typename T>
15 struct color_one
16 {
17  static T value() { return type::traits::max<T>::value; }
18 };
19 template <>
20 struct color_one<float>
21 {
22  static float value() { return 1.0f; }
23 };
24 template <>
25 struct color_one<double>
26 {
27  static double value() { return 1.0; }
28 };
29 
30 template <ColorModel cm>
31 struct color_model_traits {
32  static const unsigned int nr_components = 3;
33 };
34 
35 template <>
36 struct color_model_traits<LUM> {
37  static const unsigned int nr_components = 1;
38 };
39 
40 template <AlphaModel am>
41 struct alpha_model_traits {
42  static const unsigned int nr_components = 1;
43 };
44 
45 template <>
46 struct alpha_model_traits<NO_ALPHA> {
47  static const unsigned int nr_components = 0;
48 };
49 
50 template <typename T, ColorModel cm = RGB, AlphaModel = NO_ALPHA>
51 class color;
52 
54 template <typename T, AlphaModel = NO_ALPHA>
56 {
57  T alpha;
58 };
59 
61 template <typename T1, typename T2>
62 inline void convert_color_component(const T1& from, T2& to) {
63  to = (T2)((typename type::func::promote<T1,T2>::type)(from)*color_one<T2>::value()/color_one<T1>::value());
64 }
65 
67 template <>
68 inline void convert_color_component(const unsigned int& from, unsigned short& to) {
69  to = from >> 16;
70 }
72 template <>
73 inline void convert_color_component(const unsigned int& from, unsigned char& to) {
74  to = from >> 24;
75 }
77 template <>
78 inline void convert_color_component(const unsigned short& from, unsigned char& to) {
79  to = from >> 8;
80 }
82 template <>
83 inline void convert_color_component(const unsigned short& from, unsigned int& to) {
84  to = (unsigned int) from << 16;
85 }
87 template <>
88 inline void convert_color_component(const unsigned char& from, unsigned int& to) {
89  to = (unsigned int)from << 24;
90 }
92 template <>
93 inline void convert_color_component(const unsigned char& from, unsigned short& to) {
94  to = (unsigned short)from << 8;
95 }
96 
98 template <typename T1, AlphaModel am1, typename T2, AlphaModel am2>
99 inline void convert_alpha_model(const dummy_alpha<T1,am1>& from, dummy_alpha<T2,am2>& to)
100 {
101  dummy_alpha<typename type::func::promote<T1,T2>::type,OPACITY> tmp;
102  convert_alpha_model(from,tmp);
103  convert_alpha_model(tmp,to);
104 }
105 
106 template <AlphaModel am1, AlphaModel am2, typename T>
107 inline T convert_alpha(const T& from)
108 {
109  dummy_alpha<T,am1> to;
110  convert_alpha_model((const dummy_alpha<T,am2>&) from, to);
111  return to.alpha;
112 }
114 template <typename T1, ColorModel cm1, typename T2, ColorModel cm2>
115 inline void convert_color_model(const color<T1,cm1>& from, color<T2,cm2>& to)
116 {
117  color<typename type::func::promote<T1,T2>::type,XYZ> tmp;
118  convert_color(from,tmp);
119  convert_color(tmp,to);
120 }
121 
123 template <typename T1, ColorModel cm1, AlphaModel am1, typename T2, ColorModel cm2, AlphaModel am2>
124 inline void convert_color(const color<T1,cm1,am1>& from, color<T2,cm2,am2>& to)
125 {
126  convert_color_model(*((const color<T1,cm1>*)&from),(color<T2,cm2>&)to);
127  convert_alpha_model(*((const dummy_alpha<T1,am1>*)&from.alpha()),(dummy_alpha<T2,am2>&)(to.alpha()));
128 }
129 
131 template <typename ta_derived> struct rgb_color_interface {};
132 
134 template <typename T, ColorModel cm, AlphaModel am>
135 struct rgb_color_interface<color<T,cm,am> >
136 {
138  T R() const { return color<T,RGB>(*static_cast<const color<T,cm,am>*>(this))[0]; }
140  T G() const { return color<T,RGB>(*static_cast<const color<T,cm,am>*>(this))[1]; }
142  T B() const { return color<T,RGB>(*static_cast<const color<T,cm,am>*>(this))[2]; }
143 };
144 
146 template <typename T, AlphaModel am>
147 struct rgb_color_interface<color<T,RGB,am> >
148 {
150  T& R() { return (*static_cast<color<T,RGB,am>*>(this))[0]; }
152  const T& R() const { return (*static_cast<const color<T,RGB,am>*>(this))[0]; }
154  T& G() { return (*static_cast<color<T,RGB,am>*>(this))[1]; }
156  const T& G() const { return (*static_cast<const color<T,RGB,am>*>(this))[1]; }
158  T& B() { return (*static_cast<color<T,RGB,am>*>(this))[2]; }
160  const T& B() const { return (*static_cast<const color<T,RGB,am>*>(this))[2]; }
161 };
162 
164 template <typename ta_derived> struct hls_color_interface {};
165 
167 template <typename T, ColorModel cm, AlphaModel am>
168 struct hls_color_interface<color<T,cm,am> > : public rgb_color_interface<color<T,cm,am> >
169 {
171  T H() const { return color<T,HLS>(*static_cast<const color<T,cm,am>*>(this))[0]; }
173  T L() const { return color<T,HLS>(*static_cast<const color<T,cm,am>*>(this))[1]; }
175  T S() const { return color<T,HLS>(*static_cast<const color<T,cm,am>*>(this))[2]; }
176 };
177 
179 template <typename T, AlphaModel am>
180 struct hls_color_interface<color<T,HLS,am> > : public rgb_color_interface<color<T,HLS,am> >
181 {
183  T& H() { return (*static_cast<color<T,HLS,am>*>(this))[0]; }
185  const T& H() const { return (*static_cast<const color<T,HLS,am>*>(this))[0]; }
187  T& L() { return (*static_cast<color<T,HLS,am>*>(this))[1]; }
189  const T& L() const { return (*static_cast<const color<T,HLS,am>*>(this))[1]; }
191  T& S() { return (*static_cast<color<T,HLS,am>*>(this))[2]; }
193  const T& S() const { return (*static_cast<const color<T,HLS,am>*>(this))[2]; }
194 };
195 
197 template <typename ta_derived> struct xyz_color_interface {};
198 
200 template <typename T, ColorModel cm, AlphaModel am>
201 struct xyz_color_interface<color<T,cm,am> > : public hls_color_interface<color<T,cm,am> >
202 {
204  T X() const { return color<T,XYZ>(*static_cast<const color<T,cm,am>*>(this))[0]; }
206  T Y() const { return color<T,XYZ>(*static_cast<const color<T,cm,am>*>(this))[1]; }
208  T Z() const { return color<T,XYZ>(*static_cast<const color<T,cm,am>*>(this))[2]; }
209 };
210 
212 template <typename T, AlphaModel am>
213 struct xyz_color_interface<color<T,XYZ,am> > : public hls_color_interface<color<T,XYZ,am> >
214 {
216  T& X() { return (*static_cast<color<T,XYZ,am>*>(this))[0]; }
218  const T& X() const { return (*static_cast<const color<T,XYZ,am>*>(this))[0]; }
220  T& Y() { return (*static_cast<color<T,XYZ,am>*>(this))[1]; }
222  const T& Y() const { return (*static_cast<const color<T,XYZ,am>*>(this))[1]; }
224  T& Z() { return (*static_cast<color<T,XYZ,am>*>(this))[2]; }
226  const T& Z() const { return (*static_cast<const color<T,XYZ,am>*>(this))[2]; }
227 };
228 
229 
231 template <typename ta_derived> struct opacity_alpha_interface {};
232 
234 template <typename T, ColorModel cm, AlphaModel am>
235 struct opacity_alpha_interface<color<T,cm,am> > : public xyz_color_interface<color<T,cm,am> >
236 {
238  T opacity() const {
240  convert_alpha_model((const dummy_alpha<T,am>&)(static_cast<const color<T,cm,am>*>(this)->alpha()),opa);
241  return opa.alpha;
242  }
243 };
244 
246 template <typename T, ColorModel cm>
247 struct opacity_alpha_interface<color<T,cm,OPACITY> > : public xyz_color_interface<color<T,cm,OPACITY> >
248 {
250  T& opacity() { return static_cast<color<T,cm,OPACITY>*>(this)->alpha(); }
252  const T& opacity() const { return static_cast<const color<T,cm,OPACITY>*>(this)->alpha(); }
253 };
254 
256 template <typename ta_derived> struct transparency_alpha_interface{};
257 
259 template <typename T, ColorModel cm, AlphaModel am>
260 struct transparency_alpha_interface<color<T,cm,am> > : public opacity_alpha_interface<color<T,cm,am> >
261 {
263  T transparency() const {
265  convert_alpha_model((const dummy_alpha<T,am>&)(static_cast<const color<T,cm,am>*>(this)->alpha()),tra);
266  return tra.alpha;
267  }
268 };
269 
271 template <typename T, ColorModel cm>
272 struct transparency_alpha_interface<color<T,cm,TRANSPARENCY> > : public opacity_alpha_interface<color<T,cm,TRANSPARENCY> >
273 {
275  T& transparency() { return static_cast<color<T,cm,TRANSPARENCY>*>(this)->alpha(); }
277  const T& transparency() const { return static_cast<const color<T,cm,TRANSPARENCY>*>(this)->alpha(); }
278 };
279 
281 template <typename ta_derived> struct extinction_alpha_interface {};
282 
284 template <typename T, ColorModel cm, AlphaModel am>
285 struct extinction_alpha_interface<color<T,cm,am> > : public transparency_alpha_interface<color<T,cm,am> >
286 {
288  T extinction() const {
290  convert_alpha_model((const dummy_alpha<T,am>&)(static_cast<const color<T,cm,am>*>(this)->alpha()),ext);
291  return ext.alpha;
292  }
293 };
294 
296 template <typename T, ColorModel cm>
297 struct extinction_alpha_interface<color<T,cm,EXTINCTION> > : public transparency_alpha_interface<color<T,cm,EXTINCTION> >
298 {
300  T& extinction() { return static_cast<color<T,cm,EXTINCTION>*>(this)->alpha(); }
302  const T& extinction() const { return static_cast<const color<T,cm,EXTINCTION>*>(this)->alpha(); }
303 };
304 
307 template <typename T, ColorModel cm, AlphaModel am>
308 class color : public extinction_alpha_interface<color<T,cm,am> >
309 {
310 public:
312  static const ColorModel clr_mod = cm;
314  static const AlphaModel alp_mod = am;
316  static const unsigned int nr_color_components = color_model_traits<cm>::nr_components;
318  static const unsigned int nr_alpha_components = alpha_model_traits<am>::nr_components;
322  typedef T component_type;
324  color() { }
326  color(const T& c) { *this = c; }
328  color(const T& c0, const T& c1) {
329  components[0] = c0;
330  if (nr_components > 1)
331  components[1] = c1;
332  }
334  color(const T& c0, const T& c1, const T& c2) {
335  components[0] = c0;
336  if (nr_components > 1)
337  components[1] = c1;
338  if (nr_components > 2)
339  components[2] = c2;
340  }
342  color(const T& c0, const T& c1, const T& c2, const T& c3) {
343  components[0] = c0;
344  if (nr_components > 1)
345  components[1] = c1;
346  if (nr_components > 2)
347  components[2] = c2;
348  if (nr_components > 3)
349  components[3] = c3;
350  }
352  template <typename T2, ColorModel cm2, AlphaModel am2>
354  convert_color(c2, *this);
355  }
357  template <typename T2, ColorModel cm2, AlphaModel am2>
359  convert_color(c2, *this);
360  return *this;
361  }
364  std::fill(components, components+nr_components, c);
365  return *this;
366  }
368  color<T,cm>& drop_alpha() { return *((color<T,cm>*)this); }
369  const color<T,cm>& drop_alpha() const { return *((const color<T,cm>*)this); }
371  template <typename T2, ColorModel cm2, AlphaModel am2>
373  color<T,cm,am> tmp(c2);
374  for (int i=0; i<nr_components; ++i)
375  components[i] *= tmp[i];
376  return *this;
377  }
379  template <typename T2, ColorModel cm2, AlphaModel am2>
381  color<T,cm,am> res(*this);
382  res *= c2;
383  return res;
384  }
387  for (int i=0; i<nr_components; ++i)
388  components[i] *= c;
389  return *this;
390  }
392  template <typename T2>
395  for (int i=0; i<nr_components; ++i)
396  res[i] = components[i]*c;
397  return res;
398  }
400  template <typename T2, ColorModel cm2, AlphaModel am2>
402  color<T,cm,am> tmp(c2);
403  for (int i=0; i<nr_components; ++i)
404  components[i] += tmp[i];
405  return *this;
406  }
408  template <typename T2, ColorModel cm2, AlphaModel am2>
410  color<T,cm,am> res(*this);
411  res += c2;
412  return res;
413  }
416  for (int i=0; i<nr_components; ++i)
417  components[i] += c;
418  return *this;
419  }
422  color<T,cm,am> res(*this);
423  res += c;
424  return res;
425  }
427  void clamp(const T& mn = 0, const T& mx = color_one<T>::value(), bool skip_alpha = false) {
428  unsigned int nr = nr_components;
429  if (skip_alpha)
430  nr = nr_color_components;
431  for (int i=nr-1; i>=0; --i)
432  if (components[i] < mn)
433  components[i] = mn;
434  else if (components[i] > mx)
435  components[i] = mx;
436  }
438  T& operator [] (unsigned int i) { return components[i]; }
440  const T& operator [] (unsigned int i) const { return components[i]; }
442  T& alpha() { return components[nr_color_components]; }
444  const T& alpha() const { return components[nr_color_components]; }
445 
446 protected:
447  T components[nr_components];
448 };
449 
450 /*********************************************************************
451 **
452 ** operators
453 **
454 *********************************************************************/
455 
457 template <typename T, ColorModel cm, AlphaModel am>
458 bool operator == (const color<T,cm,am>& c1, const color<T,cm,am>& c2) {
459  for (unsigned int i=0; i<color<T,cm,am>::nr_components; ++i)
460  if (c1[i] != c2[i])
461  return false;
462  return true;
463 }
464 
466 template <typename T1, ColorModel cm1, AlphaModel am1, typename T2, ColorModel cm2, AlphaModel am2>
467 color<typename type::func::promote<T1,T2>::type,cm1,am1> operator * (const color<T1,cm1,am1>& c1, const color<T2,cm2,am2>& c2) {
468  color<typename type::func::promote<T1,T2>::type,cm1,am1> res(c1);
469  res *= c2;
470  return res;
471 }
472 
474 template <typename T1, typename T2, ColorModel cm2, AlphaModel am2>
475 color<typename type::func::promote<T1,T2>::type,cm2,am2> operator * (const T1& c1, const color<T2,cm2,am2>& c2) {
476  color<typename type::func::promote<T1,T2>::type,cm2,am2> res(c2);
477  res *= c1;
478  return res;
479 }
480 
482 template <typename T1, ColorModel cm1, AlphaModel am1, typename T2, ColorModel cm2, AlphaModel am2>
483 color<typename type::func::promote<T1,T2>::type,cm1,am1> operator + (const color<T1,cm1,am1>& c1, const color<T2,cm2,am2>& c2) {
484  color<typename type::func::promote<T1,T2>::type,cm1,am1> res(c1);
485  res += c2;
486  return res;
487 }
488 
490 template <typename T1, ColorModel cm1, AlphaModel am1>
491 std::ostream& operator << (std::ostream& os, const color<T1,cm1,am1>& c) {
492  os << c[0];
493  for (unsigned int i=1; i<color<T1,cm1,am1>::nr_components; ++i)
494  os << " " << c[i];
495  return os;
496 }
498 template <typename T1, ColorModel cm1, AlphaModel am1>
499 std::istream& operator >> (std::istream& is, color<T1,cm1,am1>& c) {
500  is >> c[0];
501  for (unsigned int i=1; i<color<T1,cm1,am1>::nr_components; ++i)
502  is >> c[i];
503  return is;
504 }
505 
507 template <ColorModel cm, AlphaModel am>
508 std::ostream& operator << (std::ostream& os, const color<unsigned char,cm,am>& c) {
509  os << (int)c[0];
510  for (unsigned int i=1; i<color<unsigned char,cm,am>::nr_components; ++i)
511  os << " " << (int)c[i];
512  return os;
513 }
515 template <ColorModel cm, AlphaModel am>
516 std::istream& operator >> (std::istream& is, color<unsigned char,cm,am>& c) {
517  int tmp;
518  is >> tmp;
519  c[0] = (unsigned char)tmp;
520  for (unsigned int i=1; i<color<unsigned char,cm,am>::nr_components; ++i) {
521  is >> tmp;
522  c[i] = (unsigned char)tmp;
523  }
524  return is;
525 }
526 
527 
528 /*********************************************************************
529 **
530 ** alpha model conversions
531 **
532 *********************************************************************/
533 
534 
536 template <typename T1, typename T2>
537 void convert_alpha_model(const dummy_alpha<T1,NO_ALPHA>& from, dummy_alpha<T2,OPACITY>& to)
538 {
539  to.alpha = color_one<T2>::value();
540 }
542 template <typename T1, typename T2>
543 void convert_alpha_model(const dummy_alpha<T1,NO_ALPHA>& from, dummy_alpha<T2,TRANSPARENCY>& to)
544 {
545  to.alpha = 0;
546 }
548 template <typename T1, typename T2>
549 void convert_alpha_model(const dummy_alpha<T1,NO_ALPHA>& from, dummy_alpha<T2,EXTINCTION>& to)
550 {
551  to.alpha = color_one<T2>::value();
552 }
554 template <typename T1, typename T2>
555 void convert_alpha_model(const dummy_alpha<T1,OPACITY>& from, dummy_alpha<T2,TRANSPARENCY>& to)
556 {
557  convert_color_component(from.alpha,to.alpha);
558  to.alpha = color_one<T2>::value()-to.alpha;
559 }
561 template <typename T1, typename T2>
562 void convert_alpha_model(const dummy_alpha<T1,TRANSPARENCY>& from, dummy_alpha<T2,OPACITY>& to)
563 {
564  convert_color_component(from.alpha,to.alpha);
565  to.alpha = color_one<T2>::value()-to.alpha;
566 }
568 template <typename T1, typename T2>
569 void convert_alpha_model(const dummy_alpha<T1,OPACITY>& from, dummy_alpha<T2,EXTINCTION>& to)
570 {
571  double tmp;
572  convert_color_component(from.alpha,tmp);
573  tmp = -log(1-tmp);
574  convert_color_component(tmp, to.alpha);
575 }
577 template <typename T1, typename T2>
578 void convert_alpha_model(const dummy_alpha<T1,EXTINCTION>& from, dummy_alpha<T2,OPACITY>& to)
579 {
580  double tmp;
581  convert_color_component(from.alpha,tmp);
582  tmp = 1 - exp(-tmp);
583  convert_color_component(tmp, to.alpha);
584 }
585 
587 template <typename T1, AlphaModel am1, typename T2>
588 void convert_alpha_model(const dummy_alpha<T1,am1>& from, dummy_alpha<T2,NO_ALPHA>& to)
589 {
590 }
592 template <typename T1, typename T2>
593 void convert_alpha_model(const dummy_alpha<T1,NO_ALPHA>& from, dummy_alpha<T2,NO_ALPHA>& to)
594 {
595 }
597 template <typename T1, AlphaModel am, typename T2>
598 void convert_alpha_model(const dummy_alpha<T1,am>& from, dummy_alpha<T2,am>& to)
599 {
600  convert_color_component(from.alpha, to.alpha);
601 }
602 
603 /*********************************************************************
604 **
605 ** color model conversions
606 **
607 *********************************************************************/
608 
609 
611 template <typename T1, ColorModel cm, typename T2>
612 void convert_color_model(const color<T1,cm>& from, color<T2,cm>& to)
613 {
614  for (unsigned int i=0; i<color<T1,cm>::nr_components; ++i)
615  convert_color_component(from[i], to[i]);
616 }
617 
619 template <typename T1, typename T2>
620 void convert_color_model(const color<T1,XYZ>& from, color<T2,XYZ>& to)
621 {
622  for (unsigned int i=0; i<color<T1,XYZ>::nr_components; ++i)
623  convert_color_component(from[i], to[i]);
624 }
625 
627 template <typename T1, ColorModel cm1, typename T2>
628 void convert_color_model(const color<T1,cm1>& from, color<T2,XYZ>& to)
629 {
630  std::cerr << "conversion not implemented" << std::endl;
631 }
633 template <typename T1, ColorModel cm2, typename T2>
634 void convert_color_model(const color<T1,XYZ>& from, color<T2,cm2>& to)
635 {
636  std::cerr << "conversion not implemented" << std::endl;
637 }
638 
640 template <typename T1, typename T2>
641 void convert_color_model(const color<T1,LUM>& c1, color<T2,XYZ>& c2) {
642  c2[0] = 0;
643  c2[2] = 0;
644  convert_color_component(c1[0],c2[1]);
645 }
647 template <typename T1, typename T2>
648 void convert_color_model(const color<T1,XYZ>& c1, color<T2,LUM>& c2) {
649  convert_color_component(c1[1],c2[0]);
650 }
652 template <typename T1, typename T2>
653 void convert_color_model(const color<T1,RGB>& _c1, color<T2,XYZ>& c2) {
654  color<double,RGB> c1(_c1);
655  convert_color_component(0.412453*c1[0]+0.357580*c1[1]+0.180423*c1[2], c2[0]);
656  convert_color_component(0.212671*c1[0]+0.715160*c1[1]+0.072169*c1[2], c2[1]);
657  convert_color_component(0.019334*c1[0]+0.119193*c1[1]+0.950227*c1[2], c2[2]);
658 }
660 template <typename T1, typename T2>
661 void convert_color_model(const color<T1,XYZ>& _c1, color<T2,RGB>& c2) {
662  color<double,XYZ> c1(_c1);
663  convert_color_component(3.2404813432*c1[0]-1.5371515163*c1[1]-0.4985363262*c1[2], c2[0]);
664  convert_color_component(-0.9692549500*c1[0]+1.8759900015*c1[1]+0.0415559266*c1[2], c2[1]);
665  convert_color_component(0.0556466391*c1[0]-0.2040413384*c1[1]+1.0573110696*c1[2], c2[2]);
666 }
667 
669 template <typename T1, typename T2>
670 void convert_color_model(const color<T1,RGB>& rgb, color<T2,HLS>& hls) {
671  double mx, mn;
672  convert_color_component(std::max(rgb[0], std::max(rgb[1],rgb[2])), mx);
673  convert_color_component(std::min(rgb[0], std::min(rgb[1],rgb[2])), mn);
674  double LL = (mx+mn)/2;
675  if (mn == mx) {
676  hls[0] = hls[2] = 0;
677  convert_color_component(LL,hls[1]);
678  }
679  else {
680  double DM = mx-mn;
681  double SM = mx+mn;
682  double SS = (LL <= 0.5) ? DM/SM : DM/(2-SM);
683  color<double,RGB> c(rgb);
684  double HH;
685  if (c[0] == mx) HH = (c[1]-c[2])/DM;
686  else if (c[1] == mx) HH = 2+(c[2]-c[0])/DM;
687  else HH = 4+(c[0]-c[1])/DM;
688  if (HH < 0) HH += 6;
689  HH /= 6;
690  convert_color_component(HH,hls[0]);
691  convert_color_component(LL,hls[1]);
692  convert_color_component(SS,hls[2]);
693  }
694 }
696 template <typename T1, typename T2>
697 void convert_color_model(const color<T1,HLS>& hls, color<T2,RGB>& rgb) {
698  double HH,LL,SS;
699  convert_color_component(hls[0],HH); HH *= 6;
700  convert_color_component(hls[1],LL);
701  convert_color_component(hls[2],SS);
702  int I = (int) HH;
703  double F = HH-I;
704  while (I >= 6) I -= 6;
705  double mx = (LL <= 0.5) ? LL*(1+SS) : LL+SS-LL*SS;
706  double mn = 2*LL - mx;
707  double DM = mx -mn;
708  if (SS == 0) {
709  T2 tmp;
710  convert_color_component(LL,tmp);
711  rgb = tmp;
712  }
713  else {
714  switch (I) {
715  case 0 :
716  convert_color_component(mx, rgb[0]);
717  convert_color_component(mn+F*DM,rgb[1]);
718  convert_color_component(mn, rgb[2]);
719  break;
720  case 1 :
721  convert_color_component(mn+(1-F)*DM, rgb[0]);
722  convert_color_component(mx, rgb[1]);
723  convert_color_component(mn, rgb[2]);
724  break;
725  case 2 :
726  convert_color_component(mn, rgb[0]);
727  convert_color_component(mx, rgb[1]);
728  convert_color_component(mn+F*DM, rgb[2]);
729  break;
730  case 3 :
731  convert_color_component(mn, rgb[0]);
732  convert_color_component(mn+(1-F)*DM, rgb[1]);
733  convert_color_component(mx, rgb[2]);
734  break;
735  case 4 :
736  convert_color_component(mn+F*DM, rgb[0]);
737  convert_color_component(mn, rgb[1]);
738  convert_color_component(mx, rgb[2]);
739  break;
740  case 5 :
741  convert_color_component(mx, rgb[0]);
742  convert_color_component(mn, rgb[1]);
743  convert_color_component(mn+(1-F)*DM, rgb[2]);
744  break;
745  }
746  }
747 }
748 
750 template <typename T1, typename T2>
751 void convert_color_model(const color<T1,HLS>& c1, color<T2,XYZ>& c2) {
752  color<typename type::func::promote<T1,T2>::type,RGB> tmp;
753  convert_color(c1,tmp);
754  convert_color(tmp,c2);
755 }
757 template <typename T1, typename T2>
758 void convert_color_model(const color<T1,XYZ>& c1, color<T2,HLS>& c2) {
759  color<typename type::func::promote<T1,T2>::type,RGB> tmp;
760  convert_color(c1,tmp);
761  convert_color(tmp,c2);
762 }
763 
764  }
765 /*
766  namespace type {
767  namespace info {
768 
769 template <typename T, cgv::media::ColorModel cm, cgv::media::AlphaModel am>
770 struct type_name<cgv::media::color<T,cm,am> >
771 {
772  inline static const char* get_name()
773  {
774  static std::string name;
775  if (name.empty()) {
776  name = "color<";
777  name += type_name<T>::get_name();
778  name += ',';
779  name += type_interface_traits<cgv::media::ColorModel>::get_interface()->
780  get_enum()->get_enum_name(cm);
781  if (am != cgv::media::NO_ALPHA)
782  name += 'A';
783  name += '>';
784  }
785  return name.c_str();
786  }
787 };
788  }
789  }
790  */
791 }
cgv::media::color::color
color()
standard constructor does not initialize components
Definition: color.h:324
cgv::media::hls_color_interface< color< T, HLS, am > >::H
const T & H() const
read access to H component of HLS color
Definition: color.h:185
cgv::media::rgb_color_interface< color< T, RGB, am > >::B
const T & B() const
read access to B component of RGB color
Definition: color.h:160
cgv::media::rgb_color_interface< color< T, RGB, am > >::G
T & G()
write access to G component of RGB color
Definition: color.h:154
cgv::media::opacity_alpha_interface< color< T, cm, OPACITY > >::opacity
T & opacity()
write access to opacity component
Definition: color.h:250
cgv::media::extinction_alpha_interface
the extinction_alpha_interface adds access function to the opacity, where write access is only grante...
Definition: color.h:281
cgv::media::opacity_alpha_interface
the opacity_alpha_interface adds access function to the opacity, where write access is only granted f...
Definition: color.h:231
cgv::media::rgb_color_interface< color< T, RGB, am > >::R
const T & R() const
read access to R component of RGB color
Definition: color.h:152
cgv::media::xyz_color_interface< color< T, cm, am > >::X
T X() const
convert color to XYZ and return X component
Definition: color.h:204
cgv::media::extinction_alpha_interface< color< T, cm, EXTINCTION > >::extinction
T & extinction()
write access to extinction component
Definition: color.h:300
cgv::media::rgb_color_interface< color< T, cm, am > >::R
T R() const
convert color to RGB and return R component
Definition: color.h:138
cgv::media::color::operator=
color< T, cm, am > & operator=(const color< T2, cm2, am2 > &c2)
assign to color with potential color conversion
Definition: color.h:358
cgv::media::color::alpha
T & alpha()
return alpha component
Definition: color.h:442
cgv::media::hls_color_interface< color< T, HLS, am > >::L
const T & L() const
read access to L component of HLS color
Definition: color.h:189
cgv::media::xyz_color_interface< color< T, XYZ, am > >::Z
T & Z()
write access to Z component of XYZ color
Definition: color.h:224
cgv::media::xyz_color_interface< color< T, XYZ, am > >::Y
const T & Y() const
read access to Y component of XYZ color
Definition: color.h:222
cgv::media::xyz_color_interface< color< T, XYZ, am > >::X
const T & X() const
read access to X component of XYZ color
Definition: color.h:218
cgv::media::rgb_color_interface< color< T, RGB, am > >::R
T & R()
write access to R component of RGB color
Definition: color.h:150
cgv::media::rgb_color_interface< color< T, RGB, am > >::B
T & B()
write access to B component of RGB color
Definition: color.h:158
cgv::media::color::nr_color_components
static const unsigned int nr_color_components
static and const access to number of color components
Definition: color.h:316
cgv::media::color::alp_mod
static const AlphaModel alp_mod
static and const access to alpha model
Definition: color.h:314
cgv::media::color::nr_alpha_components
static const unsigned int nr_alpha_components
static and const access to number of alpha components
Definition: color.h:318
cgv::media::color::operator[]
T & operator[](unsigned int i)
access to components
Definition: color.h:438
cgv::media::rgb_color_interface< color< T, cm, am > >::G
T G() const
convert color to RGB and return G component
Definition: color.h:140
cgv::media::rgb_color_interface< color< T, cm, am > >::B
T B() const
convert color to RGB and return B component
Definition: color.h:142
cgv::media::color::color
color(const T &c0, const T &c1)
set first two components to given values
Definition: color.h:328
cgv::media::hls_color_interface< color< T, HLS, am > >::L
T & L()
write access to L component of HLS color
Definition: color.h:187
cgv::media::hls_color_interface< color< T, cm, am > >::H
T H() const
convert color to HLS and return H component
Definition: color.h:171
cgv::media::xyz_color_interface< color< T, XYZ, am > >::Z
const T & Z() const
read access to Z component of XYZ color
Definition: color.h:226
cgv::media::color
Definition: color.h:51
cgv::media::transparency_alpha_interface< color< T, cm, TRANSPARENCY > >::transparency
T & transparency()
write access to transparency component
Definition: color.h:275
cgv::media::dummy_alpha
this dummy class is only used for template argument matching
Definition: color.h:56
cgv::media::transparency_alpha_interface
the transparency_alpha_interface adds access function to the opacity, where write access is only gran...
Definition: color.h:256
cgv::media::color::operator+=
color< T, cm, am > & operator+=(const color< T2, cm2, am2 > &c2)
add color
Definition: color.h:401
cgv::media::xyz_color_interface< color< T, cm, am > >::Y
T Y() const
convert color to XYZ and return Y component
Definition: color.h:206
cgv::media::color::operator*=
color< T, cm, am > & operator*=(const color< T2, cm2, am2 > &c2)
multiply with color
Definition: color.h:372
cgv::media::color::color
color(const T &c0, const T &c1, const T &c2, const T &c3)
set all four components to given values
Definition: color.h:342
cgv::media::color::operator*
color< T, cm, am > operator*(const color< T2, cm2, am2 > &c2)
post multiply with color
Definition: color.h:380
cgv::media::color::clamp
void clamp(const T &mn=0, const T &mx=color_one< T >::value(), bool skip_alpha=false)
clamp to the given range, which defaults to [0,1] of the component type
Definition: color.h:427
cgv::media::rgb_color_interface
the rgb_color_interface adds access function to the R, G, and B-channels of the color,...
Definition: color.h:131
cgv::media::hls_color_interface
the hls_color_interface adds access function to the H, L, and S-channels of the color,...
Definition: color.h:164
cgv::media::xyz_color_interface
the xyz_color_interface adds access function to the X, Y, and Z-channels of the color,...
Definition: color.h:197
cgv::media::xyz_color_interface< color< T, cm, am > >::Z
T Z() const
convert color to XYZ and return Z component
Definition: color.h:208
cgv::media::color::color
color(const T &c0, const T &c1, const T &c2)
set first three components to given values
Definition: color.h:334
cgv::media::color::component_type
T component_type
type of color components
Definition: color.h:322
cgv::media::hls_color_interface< color< T, HLS, am > >::S
T & S()
write access to S component of HLS color
Definition: color.h:191
cgv::media::rgb_color_interface< color< T, RGB, am > >::G
const T & G() const
read access to G component of RGB color
Definition: color.h:156
cgv::media::transparency_alpha_interface< color< T, cm, am > >::transparency
T transparency() const
convert alpha to transparency
Definition: color.h:263
cgv::media::hls_color_interface< color< T, cm, am > >::L
T L() const
convert color to HLS and return L component
Definition: color.h:173
cgv::media::color::color
color(const color< T2, cm2, am2 > &c2)
copy constructor uses color conversion if necessary
Definition: color.h:353
cgv::media::color::color
color(const T &c)
set all components to given value
Definition: color.h:326
cgv::media::opacity_alpha_interface< color< T, cm, OPACITY > >::opacity
const T & opacity() const
read access to opacity component
Definition: color.h:252
cgv::media::extinction_alpha_interface< color< T, cm, EXTINCTION > >::extinction
const T & extinction() const
read access to extinction component
Definition: color.h:302
cgv::media::extinction_alpha_interface< color< T, cm, am > >::extinction
T extinction() const
convert alpha to extinction
Definition: color.h:288
cgv::media::hls_color_interface< color< T, cm, am > >::S
T S() const
convert color to HLS and return S component
Definition: color.h:175
cgv::media::color::nr_components
static const unsigned int nr_components
static and const access to total number of components
Definition: color.h:320
cgv::media::color::alpha
const T & alpha() const
return alpha component
Definition: color.h:444
cgv::media::hls_color_interface< color< T, HLS, am > >::S
const T & S() const
read access to S component of HLS color
Definition: color.h:193
cgv::media::transparency_alpha_interface< color< T, cm, TRANSPARENCY > >::transparency
const T & transparency() const
read access to transparency component
Definition: color.h:277
cgv::media::opacity_alpha_interface< color< T, cm, am > >::opacity
T opacity() const
convert alpha to opacity
Definition: color.h:238
cgv::media::xyz_color_interface< color< T, XYZ, am > >::X
T & X()
write access to X component of XYZ color
Definition: color.h:216
cgv::media::xyz_color_interface< color< T, XYZ, am > >::Y
T & Y()
write access to Y component of XYZ color
Definition: color.h:220
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::media::color::clr_mod
static const ColorModel clr_mod
static and const access to color model
Definition: color.h:312
cgv::media::color::operator+
color< T, cm, am > operator+(const color< T2, cm2, am2 > &c2)
add color
Definition: color.h:409
cgv::media::color::drop_alpha
color< T, cm > & drop_alpha()
drop the alpha component if any by a cast operator
Definition: color.h:368
cgv::media::hls_color_interface< color< T, HLS, am > >::H
T & H()
write access to H component of HLS color
Definition: color.h:183