-
Notifications
You must be signed in to change notification settings - Fork 16
/
Color.cpp
172 lines (142 loc) · 3.15 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
////////////////////////////////////////////////////////////////////////////////////
// Copyright © Charalambos "Charis" Poullis, [email protected] //
// This work can only be used under an exclusive license of the author. //
////////////////////////////////////////////////////////////////////////////////////
#ifndef __COLOR_CPP__
#define __COLOR_CPP__
#include "Color.h"
Color::Color() {
data(0) = 0.0f;
data(1) = 0.0f;
data(2) = 0.0f;
data(3) = 1.0f;
}
Color::Color(Color const &_color) {
data(0) = _color.r();
data(1) = _color.g();
data(2) = _color.b();
data(3) = _color.a();
}
Color::Color(float _grayscale) {
data(0) = _grayscale;
data(1) = _grayscale;
data(2) = _grayscale;
data(3) = 1.0f;
}
Color::Color(float _r, float _g, float _b) {
data(0) = _r;
data(1) = _g;
data(2) = _b;
data(3) = 1.0f;
}
Color::Color(float _r, float _g, float _b, float _a) {
data(0) = _r;
data(1) = _g;
data(2) = _b;
data(3) = _a;
}
Color::~Color() {
}
float Color::r() const {
return data(0);
}
float &Color::r() {
return data(0);
}
float Color::g() const {
return data(1);
}
float &Color::g() {
return data(1);
}
float Color::b() const {
return data(2);
}
float &Color::b() {
return data(2);
}
float Color::a() const {
return data(3);
}
float &Color::a() {
return data(3);
}
float& Color::operator()(int elementNumber) {
return data(elementNumber);
}
float Color::operator()(int elementNumber) const {
return data(elementNumber);
}
Color operator*(Color const &c1, Color const &c2) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) * c2(i);
return result;
}
Color operator*(Color const &c1, float scalar) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) * scalar;
return result;
}
Color operator*(float scalar, Color const &c1) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) * scalar;
return result;
}
Color operator+(Color const &c1, Color const &c2) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) + c2(i);
return result;
}
Color operator-(Color const &c1, Color const &c2) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) - c2(i);
return result;
}
Color operator/(Color const &c1, Color const &c2) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) / std::max(float(COLOR_EPSILON), c2(i));
return result;
}
Color operator/(Color const &c1, float scalar) {
Color result;
for (int i = 0; i < 3; i++)
result(i) = c1(i) / std::max(float(COLOR_EPSILON), scalar);
return result;
}
bool operator==(Color const &c1, Color const &c2) {
for (int i = 0; i < 3; i++)
if (fabs(c1(i) - c2(i)) > COLOR_EPSILON)
return false;
return true;
}
bool operator!=(Color const &c1, Color const &c2) {
for (int i = 0; i < 3; i++)
if (fabs(c1(i) - c2(i)) > COLOR_EPSILON)
return true;
return false;
}
Color &Color::operator+=(Color const &vec) {
for (int i = 0; i < 3; i++) {
data(i) += vec.data(i);
}
return *this;
}
Color &Color::operator*=(float scalar) {
for (int i = 0; i < 3; i++) {
data(i) *= scalar;
}
return *this;
}
Color &Color::operator/=(float scalar) {
for (int i = 0; i < 3; i++) {
data(i) /= scalar;
}
return *this;
}
#endif