-
Notifications
You must be signed in to change notification settings - Fork 1
/
Color.cpp
213 lines (157 loc) · 5.05 KB
/
Color.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Color.cpp: implementation of the CColor class.
//
//////////////////////////////////////////////////////////////////////
#include "Color.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CColor::CColor()
{
}
CColor::~CColor()
{
}
/*********************************************************************
NAME : CColor
DESCRIPTION: parameterized constructor for CColor object
PARAMETER : r : the red component of the color
g : the green component of the color
b : the blue component of the color
RETURN : NONE
EXCEPTION : NONE.
*********************************************************************/
CColor::CColor (float r, float g, float b)
{
this->fRed = r;
this->fGreen = g;
this->fBlue = b;
}
/*********************************************************************
NAME : SetColor
DESCRIPTION: Sets the value of the rgb triplet
PARAMETER : r : the red component of the color
g : the green component of the color
b : the blue component of the color
RETURN : NONE
EXCEPTION : NONE.
*********************************************************************/
void CColor::SetColor (float r, float g, float b)
{
this->fRed = r;
this->fGreen = g;
this->fBlue = b;
}
/*********************************************************************
NAME : GetRed
DESCRIPTION: Gets the red component of the color
PARAMETER : NONE
RETURN : The red component of the color
EXCEPTION : NONE.
*********************************************************************/
float CColor::GetRed ()
{
return fRed;
}
/*********************************************************************
NAME : GetGreen
DESCRIPTION: Fetches the green component of the color
PARAMETER : NONE
RETURN : The green component of the color
EXCEPTION : NONE.
*********************************************************************/
float CColor::GetGreen()
{
return fGreen;
}
/*********************************************************************
NAME : GetBlue
DESCRIPTION: Fetches the blue component of the color
PARAMETER : NONE
RETURN : The blue component
EXCEPTION : NONE.
*********************************************************************/
float CColor::GetBlue ()
{
return fBlue;
}
/*********************************************************************
NAME : operator *
DESCRIPTION: Multiplies the color with an intensity. ie, scales it.
PARAMETER : intensity : The factor to scale the color by. max 1.
RETURN : The resultant color
EXCEPTION : NONE.
*********************************************************************/
CColor CColor::operator * (float intensity)
{
CColor result;
if (intensity > 1)
{
intensity = 1;
}
if (intensity < 0)
{
intensity = 0;
}
result.fBlue = this->fBlue * intensity;
result.fGreen= this->fGreen * intensity;
result.fRed = this->fRed * intensity;
return result;
}
/*********************************************************************
NAME : operator * (CColor)
DESCRIPTION: Multiplies the color with another color
PARAMETER : col2 : The color to multiply with
RETURN : The resultant color
EXCEPTION : NONE.
*********************************************************************/
CColor CColor::operator * (CColor col2)
{
CColor result;
result.fBlue = (float)(int((this->fBlue * col2.fBlue) * 1000.0f)) / 1000.0f;
result.fGreen= (float)(int((this->fGreen* col2.fGreen) * 1000.0f)) / 1000.0f;
result.fRed = (float)(int((this->fRed * col2.fRed) * 1000.0f)) / 1000.0f;
return result;
}
/*********************************************************************
NAME : operator +=
DESCRIPTION: Adds a given color to itself.
PARAMETER : col2 : the color to add.
RETURN : void
EXCEPTION : NONE.
*********************************************************************/
void CColor::operator += (CColor col2)
{
this->fBlue += col2.fBlue;
this->fGreen+= col2.fGreen;
this->fRed += col2.fRed;
if(fBlue > 1) fBlue = 1;
if(fGreen> 1) fGreen= 1;
if(fRed > 1) fRed = 1;
if(fBlue < 0) fBlue = 0;
if(fGreen< 0) fGreen= 0;
if(fRed < 0) fRed = 0;
}
CColor CColor::Average (CColor *colors, int count)
{
float avgRed, avgGreen, avgBlue;
avgRed = avgGreen = avgBlue = 0.0f;
/******************************************************************
NOTE:
The purpose of this function is to serve as a utility to calculate
the average of many colors. One of the most obvious uses is anti-
aliasing. Right now it just computes a simple mean, but it might
use a more advanced algorithm later on.
******************************************************************/
for (int i = 0; i < count; i ++ ) {
avgRed += colors[i].GetRed();
avgGreen += colors[i].GetGreen();
avgBlue += colors[i].GetBlue();
}
return CColor (avgRed/(float)count, avgGreen/(float)count, avgBlue/(float)count);
}
bool CColor::operator != (CColor col2)
{
if (this->fBlue != col2.fBlue || this->fGreen != col2.fGreen || this->fRed != col2.fRed)
return true;
return false;
}