-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color.h
154 lines (100 loc) · 2.71 KB
/
Color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Tyrus Malmstrom
// Header file for the Color.cpp
#ifndef COLOR_H_INCLUDE
#define COLOR_H_INCLUDE
// directives:
#include <iostream>
// Namespace:
using namespace std;
class Color {
public:
// class instance variables:
// colors values represented as a double :: colors for this assignment should be in the range (inclusive) of 0-1:
double red;
double green;
double blue;
// Constructor(s):
Color():
red( 0.0 )
,green( 0.0 )
,blue( 0.0 )
{};
Color(const double& _red, const double& _green, const double& _blue):
red( _red )
,green( _green )
,blue( _blue )
{};
// pprint member function:
void pprint(ostream& out = cout) const;
// operator overloading:
Color& operator += (const Color& other_color){
red += other_color.red;
green += other_color.green;
blue += other_color.blue;
return *this;
}
Color& operator += (const double& factor){
red += factor;
green += factor;
blue += factor;
return *this;
}
Color& operator *= (const Color& other_color){
red *= other_color.red;
green *= other_color.green;
blue *= other_color.blue;
return *this;
}
Color& operator *= (const double& factor){
red *= factor;
green *= factor;
blue *= factor;
return *this;
}
Color& operator /= (const double& num){
red /= num;
green /= num;
blue /= num;
return *this;
}
// copy assignment operator: 1 of the BIG THREE
const Color& operator= (const Color& rhs){
if( this != &rhs ){ // Standard alias test...
red = rhs.red;
blue = rhs.blue;
green= rhs.green;
}
return *this;
}
// member function to check validity of color values:
void validate_colors() const{
if( red < 0.0 || green < 0.0 || blue < 0.0 ){
cerr << "This is not a valid color!" << endl;
}
}
};
//======================================================================
inline Color operator * (const Color& f_color, const Color& s_color){
return Color(
f_color.red * s_color.red,
f_color.green * s_color.green,
f_color.blue * s_color.blue
);
}
inline Color operator + (const Color& f_color, const Color& s_color){
return Color(
f_color.red + s_color.red,
f_color.green + s_color.green,
f_color.blue + s_color.blue
);
}
inline Color operator * ( const Color& f_color, const double& scalar){
return Color(
f_color.red * scalar,
f_color.green * scalar,
f_color.blue * scalar
);
}
// output stream overloading:
ostream& operator<< (ostream& out, const Color& c);
#endif // COLOR_H_INCLUDE