-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour.cpp
179 lines (150 loc) · 2.86 KB
/
colour.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
/*****************************************************************************
Colour Class
NAME:colour.cpp
DATE:10/11/1996
AUTHOR: Z.A. Nolan
******************************************************************************/
#include "colour.h"
// Constructor
TColour::TColour ()
{
_Red=0 ;
_Green=0 ;
_Blue=0 ;
}
// Constructor
TColour::TColour(unsigned char Red,unsigned char Green,unsigned char Blue)
{
_Red=Red ;
_Green=Green ;
_Blue=Blue ;
}
// Constructor
TColour::TColour(double Red,double Green,double Blue)
{
// limit the value to 1.0
if (Red>1.0)
{
_Red=255 ;
}
else
{
_Red=(unsigned char)(Red*255) ;
}
// Limit value to one
if (Green>1.0)
{
_Green=255 ;
}
else
{
_Green=(unsigned char)(Green*255) ;
}
// Limit value to one
if (Blue>1.0)
{
_Blue=255 ;
}
else
{
_Blue=(unsigned char)(Blue*255) ;
}
}
// return the red part of the colour
double TColour::Red() const
{
return (_Red/255.0) ;
}
// return the green part of the colour
double TColour::Green() const
{
return (_Green/255.0) ;
}
// return the blue part of the colour
double TColour::Blue() const
{
return (_Blue/255.0) ;
}
// set the red part of the colour
TColour::SetRed(double Red)
{
if (Red>1.0)
{
_Red=255 ;
}
else
{
_Red=(unsigned char)(Red*255) ;
}
}
// set the green part of the colour
TColour::SetGreen(double Green)
{
if (Green>1.0)
{
_Green=255 ;
}
else
{
_Green=(unsigned char)(Green*255) ;
}
}
// set the blue part of the colour
TColour::SetBlue(double Blue)
{
if (Blue>1.0)
{
_Blue=255 ;
}
else
{
_Blue=(unsigned char)(Blue*255) ;
}
}
// * a colour by some amount
TColour TColour::operator* (const double Scalar) const
{
TColour NewColour((*this).Red()*Scalar,
(*this).Green()*Scalar,
(*this).Blue()*Scalar) ;
return (NewColour) ;
}
// add colours together
TColour TColour::operator+ (const TColour Colour) const
{
TColour NewColour ;
NewColour.SetRed((*this).Red()+Colour.Red()) ;
NewColour.SetGreen((*this).Green()+Colour.Green());
NewColour.SetBlue((*this).Blue()+Colour.Blue()) ;
return (NewColour) ;
}
// function to input a colour
istream &TColour::Read (istream &In)
{
char Comma ; // delimter for the vector items
double Red,Green,Blue ;
// read in the colour
In>>Red>>Comma>>Green>>Comma>>Blue ;
(*this)=TColour(Red,Green,Blue) ;
// return the stream
return In ;
}
// function to output a colour
ostream &TColour::Write (ostream &Out) const
{
// write out the colour
Out<<(*this).Red()<<','<<(*this).Green()<<','<<(*this).Blue() ;
// return the stream
return Out ;
}
// function to read in a vector
istream &operator>>(istream &In,TColour &Colour)
{
// input a colour
return Colour.Read(In) ;
}
ostream &operator<<(ostream &Out,const TColour &Colour)
{
// output the colour
return Colour.Write(Out) ;
}