INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
rt::Color Struct Reference

#include <Color.h>

Public Types

enum  Channel { Red , Green , Blue }
 

Public Member Functions

 Color (Real red, Real green, Real blue)
 
Colorclamp ()
 Garantees that color channels are between 0 and 1.
 
 operator float * ()
 
 operator const float * () const
 
Real r () const
 
Real g () const
 
Real b () const
 
Realr ()
 
Realg ()
 
Realb ()
 
Color operator* (Real v) const
 
Color operator* (Color other) const
 
Color operator+ (Color other) const
 
Coloroperator+= (Color other)
 
Color sup (Color other) const
 
Channel argmax () const
 
Real max () const
 
Real min () const
 
void getHSV (int &h, Real &s, Real &v) const
 
void setHSV (int h, Real s, Real v)
 

Detailed Description

This structure codes the color of an object, as well as its transparency. Color channels are stored as floating point values between 0 and 1.

Definition at line 15 of file Color.h.

Member Enumeration Documentation

◆ Channel

enum rt::Color::Channel

Definition at line 93 of file Color.h.

93{ Red, Green, Blue };

Constructor & Destructor Documentation

◆ Color() [1/2]

rt::Color::Color ( )
inline

Definition at line 20 of file Color.h.

20: my_channels( 0.0, 0.0, 0.0 ) {}

◆ Color() [2/2]

rt::Color::Color ( Real  red,
Real  green,
Real  blue 
)
inline

Definition at line 21 of file Color.h.

22 : my_channels( red, green, blue )
23 {
24 clamp();
25 }
Color & clamp()
Garantees that color channels are between 0 and 1.
Definition Color.h:27

Member Function Documentation

◆ argmax()

Channel rt::Color::argmax ( ) const
inline

Definition at line 94 of file Color.h.

95 {
96 if ( r() >= g() ) return r() >= b() ? Red : Blue;
97 else return g() >= b() ? Green : Blue;
98 }

◆ b() [1/2]

Real & rt::Color::b ( )
inline

Definition at line 44 of file Color.h.

44{ return my_channels[ 2 ]; }

◆ b() [2/2]

Real rt::Color::b ( ) const
inline

Definition at line 41 of file Color.h.

41{ return my_channels[ 2 ]; }

◆ clamp()

Color & rt::Color::clamp ( )
inline

Garantees that color channels are between 0 and 1.

Definition at line 27 of file Color.h.

28 {
29 my_channels[ 0 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 0 ] ) );
30 my_channels[ 1 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 1 ] ) );
31 my_channels[ 2 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 2 ] ) );
32 return *this;
33 }

◆ g() [1/2]

Real & rt::Color::g ( )
inline

Definition at line 43 of file Color.h.

43{ return my_channels[ 1 ]; }

◆ g() [2/2]

Real rt::Color::g ( ) const
inline

Definition at line 40 of file Color.h.

40{ return my_channels[ 1 ]; }

◆ getHSV()

void rt::Color::getHSV ( int &  h,
Real s,
Real v 
) const
inline

Definition at line 102 of file Color.h.

103 {
104 // Taking care of hue
105 if ( max() == min() ) h = 0;
106 else {
107 switch ( argmax() ) {
108 case Red: h = ( (int) ( 60.0 * ( g() - b() ) / ( max() - min() ) + 360.0 ) ) % 360;
109 break;
110 case Green: h = ( (int) ( 60.0 * ( b() - r() ) / ( max() - min() ) + 120.0 ) );
111 break;
112 case Blue: h = ( (int) ( 60.0 * ( r() - g() ) / ( max() - min() ) + 240.0 ) );
113 break;
114 }
115 }
116 // Taking care of saturation
117 s = max() == 0.0 ? 0.0 : 1.0 - min() / max();
118 // Taking care of value
119 v = max();
120 }

◆ max()

Real rt::Color::max ( ) const
inline

Definition at line 99 of file Color.h.

99{ return std::max( std::max( r(), g() ), b() ); }

◆ min()

Real rt::Color::min ( ) const
inline

Definition at line 100 of file Color.h.

100{ return std::min( std::min( r(), g() ), b() ); }

◆ operator const float *()

rt::Color::operator const float * ( ) const
inline

Definition at line 37 of file Color.h.

37{ return my_channels; }

◆ operator float *()

rt::Color::operator float * ( )
inline

Definition at line 35 of file Color.h.

35{ return my_channels; }

◆ operator*() [1/2]

Color rt::Color::operator* ( Color  other) const
inline

Definition at line 57 of file Color.h.

58 {
59 Color tmp( *this );
60 tmp.my_channels[ 0 ] *= other.my_channels[ 0 ];
61 tmp.my_channels[ 1 ] *= other.my_channels[ 1 ];
62 tmp.my_channels[ 2 ] *= other.my_channels[ 2 ];
63 return tmp;
64 }

◆ operator*() [2/2]

Color rt::Color::operator* ( Real  v) const
inline

Definition at line 47 of file Color.h.

48 {
49 Color tmp( *this );
50 tmp.my_channels[ 0 ] *= v;
51 tmp.my_channels[ 1 ] *= v;
52 tmp.my_channels[ 2 ] *= v;
53 return tmp;
54 }

◆ operator+()

Color rt::Color::operator+ ( Color  other) const
inline

Definition at line 67 of file Color.h.

68 {
69 Color tmp( *this );
70 tmp.my_channels[ 0 ] += other.my_channels[ 0 ];
71 tmp.my_channels[ 1 ] += other.my_channels[ 1 ];
72 tmp.my_channels[ 2 ] += other.my_channels[ 2 ];
73 return tmp;
74 }

◆ operator+=()

Color & rt::Color::operator+= ( Color  other)
inline

Definition at line 77 of file Color.h.

78 {
79 my_channels[ 0 ] += other.my_channels[ 0 ];
80 my_channels[ 1 ] += other.my_channels[ 1 ];
81 my_channels[ 2 ] += other.my_channels[ 2 ];
82 return *this;
83 }

◆ r() [1/2]

Real & rt::Color::r ( )
inline

Definition at line 42 of file Color.h.

42{ return my_channels[ 0 ]; }

◆ r() [2/2]

Real rt::Color::r ( ) const
inline

Definition at line 39 of file Color.h.

39{ return my_channels[ 0 ]; }

◆ setHSV()

void rt::Color::setHSV ( int  h,
Real  s,
Real  v 
)
inline

Definition at line 122 of file Color.h.

123 {
124 int t = ( h / 60 ) % 6;
125 Real f = ( (Real) h / 60.0 ) - (Real) t;
126 Real bv = v;
127 Real bl = (int) ( v * ( 1 - s ) );
128 Real bm = (int) ( v * ( 1 - f * s ) );
129 Real bn = (int) ( v * ( 1 - ( 1 - f ) * s ) );
130 switch ( t ) {
131 case 0: r() = bv; g() = bn; b() = bl; break;
132 case 1: r() = bm; g() = bv; b() = bl; break;
133 case 2: r() = bl; g() = bv; b() = bn; break;
134 case 3: r() = bl; g() = bm; b() = bv; break;
135 case 4: r() = bn; g() = bl; b() = bv; break;
136 case 5: r() = bv; g() = bl; b() = bm; break;
137 }
138 clamp();
139 }
float Real
the type for representing a real number.

◆ sup()

Color rt::Color::sup ( Color  other) const
inline

Definition at line 85 of file Color.h.

86 {
87 other[ 0 ] = std::max( (*this)[ 0 ], other[ 0 ] );
88 other[ 1 ] = std::max( (*this)[ 1 ], other[ 1 ] );
89 other[ 2 ] = std::max( (*this)[ 2 ], other[ 2 ] );
90 return other;
91 }

The documentation for this struct was generated from the following file: