-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrgb.h
123 lines (95 loc) · 3.26 KB
/
rgb.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
// rgb.h
#ifndef _RGB_H_
#define _RGB_H_ 1
#include <iostream>
#include <ostream>
class rgb
{
public:
rgb() { this->_r = this->_g = this->_b = 0; }
rgb(float red, float green, float blue);
rgb(const rgb & original) { *this = original; }
void setRed(float red) { this->_r = red; }
void setGreen(float green) { this->_g = green; }
void setBlue(float blue) { this->_b = blue; }
// clamping missing - so be carefull
rgb& operator=(const rgb & right_op);
rgb& operator+=(const rgb & right_op);
rgb& operator*=(const rgb & right_op);
rgb& operator/=(const rgb & right_op);
rgb& operator*=(float right_op);
rgb& operator/=(float right_op);
rgb operator+()const { return *this; }
rgb operator-()const { return rgb(-(this->_r),
-(this->_g),
-(this->_b)); }
float r() const { return this->_r; }
float g() const { return this->_g; }
float b() const { return this->_b; }
void clamp();
friend std::ostream& operator<<(std::ostream & out, const rgb & the_rgb);
friend rgb operator*(const rgb& c, float f);
friend rgb operator*(float f, const rgb& c);
friend rgb operator/(const rgb& c, float f);
friend rgb operator*(const rgb& c1, const rgb& c2);
friend rgb operator/(const rgb& c1, const rgb& c2);
friend rgb operator+(const rgb& c1, const rgb& c2);
float _r;
float _g;
float _b;
};
inline rgb::rgb(float red, float green, float blue)
: _r(red), _g(green), _b(blue) {}
inline rgb& rgb::operator+=(const rgb & right_op) {
*this = *this + right_op;
return *this;
}
inline rgb& rgb::operator*=(float right_op) {
*this = *this * right_op;
return *this;
}
inline rgb& rgb::operator/=(float right_op) {
*this = *this / right_op;
return *this;
}
inline rgb& rgb::operator*=(const rgb & right_op) {
*this = *this * right_op;
return *this;
}
inline rgb& rgb::operator/=(const rgb & right_op) {
*this = *this / right_op;
return *this;
}
inline rgb& rgb::operator=(const rgb & right_op) {
_r = right_op._r;
_g = right_op._g;
_b = right_op._b;
return *this;
}
inline void rgb::clamp() {
if (_r > 1.0f) _r = 1.0f;
if (_g > 1.0f) _g = 1.0f;
if (_b > 1.0f) _b = 1.0f;
if (_r < 0.0f) _r = 0.0f;
if (_g < 0.0f) _g = 0.0f;
if (_b < 0.0f) _b = 0.0f;
}
inline std::ostream& operator<<(std::ostream & out, const rgb & the_rgb) {
out << the_rgb._r << ' '
<< the_rgb._g << ' '
<< the_rgb._b << ' ';
return out;
}
inline rgb operator*(const rgb& c, float f)
{ return rgb(c._r*f, c._g*f, c._b*f); }
inline rgb operator*(float f, const rgb& c)
{ return rgb(c._r*f, c._g*f, c._b*f); }
inline rgb operator/(const rgb& c, float f)
{ return rgb(c._r/f, c._g/f, c._b/f); }
inline rgb operator*(const rgb& c1, const rgb& c2)
{ return rgb(c1._r*c2._r, c1._g*c2._g, c1._b*c2._b); }
inline rgb operator/(const rgb& c1, const rgb& c2)
{ return rgb(c1._r/c2._r, c1._g/c2._g, c1._b/c2._b); }
inline rgb operator+(const rgb& c1, const rgb& c2)
{ return rgb(c1._r+c2._r, c1._g+c2._g, c1._b+c2._b); }
#endif // _RGB_H_