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
|
/*
math/color.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <cstring>
#include "math/color.h"
namespace math
{
Color::Color() :
r(rgba_data[0]),
g(rgba_data[1]),
b(rgba_data[2]),
a(rgba_data[3])
{
assign(1.0f, 1.0f);
}
Color::Color(float red, float green, float blue, float alpha) :
r(rgba_data[0]),
g(rgba_data[1]),
b(rgba_data[2]),
a(rgba_data[3])
{
assign(red, green, blue, alpha);
}
Color::Color(const float grey, const float alpha) :
r(rgba_data[0]),
g(rgba_data[1]),
b(rgba_data[2]),
a(rgba_data[3])
{
assign(grey, alpha);
}
Color::Color(Color const &other) :
r(rgba_data[0]),
g(rgba_data[1]),
b(rgba_data[2]),
a(rgba_data[3])
{
assign(other);
}
void Color::assign(float red, float green, float blue, float alpha)
{
rgba_data[0] = red;
rgba_data[1] = green;
rgba_data[2] = blue;
rgba_data[3] = alpha;
}
void Color::assign(Color const & other)
{
for (int i =0; i < 4; i++)
rgba_data[i] = other.rgba_data[i];
}
void Color::assign(float grey, float alpha)
{
rgba_data[0] = rgba_data[1] = rgba_data[2] = grey;
rgba_data[3] = alpha;
}
const Color & Color::operator=(Color const & other)
{
assign(other);
return *this;
}
void Color::clamp()
{
for (int i =0; i < 4; i++)
if (rgba_data[i] < 0)
rgba_data[i] = 0;
float tmp = rgba_data[0];
if (rgba_data[1] > tmp)
tmp = rgba_data[1];
if (rgba_data[2] > tmp)
tmp = rgba_data[2];
if (tmp > 1) {
for (int i =0; i < 3; i++)
rgba_data[i] /= tmp;
}
if (rgba_data[3] > 1)
rgba_data[3] = 1;
}
float Color::red() const
{
return rgba_data[0];
}
float Color::green() const
{
return rgba_data[1];
}
float Color::blue() const
{
return rgba_data[2];
}
float Color::alpha() const
{
return rgba_data[3];
}
Color Color::operator*(float scalar) const
{
return Color(red()*scalar, green()*scalar, blue()*scalar, alpha());
}
Color const operator*(float scalar, Color const & color)
{
return color * scalar;
}
Color & Color::operator*=(const float scalar)
{
for (int i=0; i < 3; i++)
rgba_data[i] *= scalar;
return (*this);
}
std::ostream &operator<<(std::ostream &os, Color const & color)
{
os << color.red() << " " << color.green() << " " << color.blue(); // << " " << c.alpha();
return os;
}
std::istream &operator>>(std::istream & is, Color & color)
{
is >> color.r;
is >> color.g;
is >> color.b;
//is >> color.a;
color.a = 1.0;
return (is);
}
} // namespace math
|