INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Color.h
Go to the documentation of this file.
1/**
2@file Color.h
3@author JOL
4*/
5#pragma once
6#ifndef _COLOR_HPP_
7#define _COLOR_HPP_
8
9#include "PointVector.h"
10
11namespace rt {
12 /// This structure codes the color of an object, as well as its
13 /// transparency. Color channels are stored as floating point values
14 /// between 0 and 1.
15 struct Color {
16 private:
17 Vector3 my_channels;
18
19 public:
20 Color() : my_channels( 0.0, 0.0, 0.0 ) {}
21 Color( Real red, Real green, Real blue )
22 : my_channels( red, green, blue )
23 {
24 clamp();
25 }
26 /// Garantees that color channels are between 0 and 1.
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 }
34 // Useful for conversion to OpenGL vectors
35 operator float*() { return my_channels; }
36 // Useful for conversion to OpenGL vectors
37 operator const float*() const { return my_channels; }
38
39 Real r() const { return my_channels[ 0 ]; }
40 Real g() const { return my_channels[ 1 ]; }
41 Real b() const { return my_channels[ 2 ]; }
42 Real& r() { return my_channels[ 0 ]; }
43 Real& g() { return my_channels[ 1 ]; }
44 Real& b() { return my_channels[ 2 ]; }
45
46 // Operations between colors
47 Color operator*( Real v ) const
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 }
55
56 // Operations between colors
57 Color operator*( Color other ) const
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 }
65
66 // Operations between colors
67 Color operator+( Color other ) const
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 }
75
76 // Operations between colors
77 Color& operator+=( Color other )
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 }
84
85 Color sup( Color other ) const
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 }
92
93 enum Channel { Red, Green, Blue };
94 Channel argmax() const
95 {
96 if ( r() >= g() ) return r() >= b() ? Red : Blue;
97 else return g() >= b() ? Green : Blue;
98 }
99 Real max() const { return std::max( std::max( r(), g() ), b() ); }
100 Real min() const { return std::min( std::min( r(), g() ), b() ); }
101
102 void getHSV( int & h, Real & s, Real & v ) const
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 }
121
122 void setHSV( int h, Real s, Real v )
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 }
140 };
141
142 // Operations between colors
143 inline Color operator*( Real v, const Color& other )
144 {
145 Color tmp( other );
146 return tmp * v;
147 }
148
149 inline Real distance( const Color& c1, const Color& c2 )
150 {
151 return std::max( std::fabs( c1.r() - c2.r() ),
152 std::max( std::fabs( c1.g() - c2.g() ),
153 std::fabs( c1.b() - c2.b() ) ) );
154 }
155} // namespace rt
156
157#endif //_COLOR_HPP_
Namespace RayTracer.
Definition Color.h:11
float Real
the type for representing a real number.
Color & clamp()
Garantees that color channels are between 0 and 1.
Definition Color.h:27