-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.cpp
192 lines (178 loc) · 7.24 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
#include "color.h"
#include <cstdint>
namespace Tracer {
Color_UINT8::Color_UINT8(std::uint8_t r, std::uint8_t g, std::uint8_t b)
: red(r), green(g), blue(b) {}
Color_UINT8::Color_UINT8(const Color_F& color_float) {
Color_UINT8 scaled_color = color_float.color_scale();
red = scaled_color.red;
green = scaled_color.green;
blue = scaled_color.blue;
}
std::int32_t Color_UINT8::generate_hex() const {
return static_cast<std::int32_t>(red) << 8 |
static_cast<std::int32_t>(green) << 4 |
static_cast<std::int32_t>(blue);
}
void Color_UINT8::print_hex() const {
printf("[%04x]", (static_cast<std::int32_t>(red) << 8) |
(static_cast<std::int32_t>(green) << 4) |
(static_cast<std::int32_t>(blue)));
}
Color_UINT8 Color_UINT8::operator*(float scalar) const {
std::uint8_t new_red =
(static_cast<std::int16_t>(red) * scalar >= 255) ? 255 : red * scalar;
std::uint8_t new_green =
(static_cast<std::int16_t>(green) * scalar >= 255) ? 255 : green * scalar;
std::uint8_t new_blue =
(static_cast<std::int16_t>(blue) * scalar >= 255) ? 255 : blue * scalar;
return Color_UINT8(new_red, new_green, new_blue);
}
void Color_UINT8::operator*=(float scalar) {
std::uint8_t new_red =
(static_cast<std::int16_t>(red) * scalar >= 255) ? 255 : red * scalar;
std::uint8_t new_green =
(static_cast<std::int16_t>(green) * scalar >= 255) ? 255 : green * scalar;
std::uint8_t new_blue =
(static_cast<std::int16_t>(blue) * scalar >= 255) ? 255 : blue * scalar;
red = new_red;
green = new_green;
blue = new_blue;
}
Color_UINT8 Color_UINT8::multiply_with_scaling(float scalar) const {
float new_red = red * scalar;
float new_green = green * scalar;
float new_blue = blue * scalar;
float total_color = new_red + new_green + new_blue;
return Color_UINT8(static_cast<std::uint8_t>(new_red / total_color + .5),
static_cast<std::uint8_t>(new_green / total_color + .5),
static_cast<std::uint8_t>(new_blue / total_color + .5));
}
void Color_UINT8::multiply_with_scaling_in_place(float scalar) {
float new_red = red * scalar;
float new_green = green * scalar;
float new_blue = blue * scalar;
float total_color = new_red + new_green + new_blue;
red = static_cast<std::uint8_t>(new_red / total_color + .5);
green = static_cast<std::uint8_t>(new_green / total_color + .5);
blue = static_cast<std::uint8_t>(new_blue / total_color + .5);
}
Color_UINT8 Color_UINT8::operator+(const Color_UINT8& rhs) const {
std::uint8_t new_red =
(static_cast<std::int16_t>(red) + static_cast<std::int16_t>(rhs.red) >=
255)
? 255
: red + rhs.red;
std::uint8_t new_green = (static_cast<std::int16_t>(green) +
static_cast<std::int16_t>(rhs.green) >=
255)
? 255
: green + rhs.green;
std::uint8_t new_blue =
(static_cast<std::int16_t>(blue) + static_cast<std::int16_t>(rhs.blue) >=
255)
? 255
: blue + rhs.blue;
return Color_UINT8(new_red, new_green, new_blue);
}
void Color_UINT8::operator+=(const Color_UINT8& rhs) {
red = (static_cast<std::int16_t>(red) + static_cast<std::int16_t>(rhs.red) >=
255)
? 255
: red + rhs.red;
green = (static_cast<std::int16_t>(green) +
static_cast<std::int16_t>(rhs.green) >=
255)
? 255
: green + rhs.green;
blue =
(static_cast<std::int16_t>(blue) + static_cast<std::int16_t>(rhs.blue) >=
255)
? 255
: blue + rhs.blue;
}
Color_UINT8 Color_UINT8::addition_with_scaling(const Color_UINT8& rhs) const {
float new_red = red + rhs.red;
float new_green = green + rhs.green;
float new_blue = blue + rhs.blue;
float total_color = new_red + new_green + new_blue;
return Color_UINT8(static_cast<std::uint8_t>(new_red / total_color + .5),
static_cast<std::uint8_t>(new_green / total_color + .5),
static_cast<std::uint8_t>(new_blue / total_color + .5));
}
Color_UINT8 Color_UINT8::addition_with_scaling(const Color_F& rhs) const {
float new_red = red + rhs.red;
float new_green = green + rhs.green;
float new_blue = blue + rhs.blue;
float total_color = new_red + new_green + new_blue;
return Color_UINT8(static_cast<std::uint8_t>(new_red / total_color + .5),
static_cast<std::uint8_t>(new_green / total_color + .5),
static_cast<std::uint8_t>(new_blue / total_color + .5));
}
void Color_UINT8::addition_with_scaling_in_place(const Color_UINT8& rhs) {
float new_red = red + rhs.red;
float new_green = green + rhs.green;
float new_blue = blue + rhs.blue;
float total_color = new_red + new_green + new_blue;
red = static_cast<std::uint8_t>(new_red / total_color + .5);
green = static_cast<std::uint8_t>(new_green / total_color + .5);
blue = static_cast<std::uint8_t>(new_blue / total_color + .5);
}
void Color_UINT8::addition_with_scaling_in_place(const Color_F& rhs) {
float new_red = red + rhs.red;
float new_green = green + rhs.green;
float new_blue = blue + rhs.blue;
float total_color = new_red + new_green + new_blue;
red = static_cast<std::uint8_t>(new_red / total_color + .5);
green = static_cast<std::uint8_t>(new_green / total_color + .5);
blue = static_cast<std::uint8_t>(new_blue / total_color + .5);
}
Color_F::Color_F(float r, float g, float b) : red(r), green(g), blue(b) {}
Color_F::Color_F(const Color_UINT8& color_u)
: red(static_cast<float>(color_u.red)),
green(static_cast<float>(color_u.green)),
blue(static_cast<float>(color_u.blue)) {}
std::int32_t Color_F::generate_hex() const {
return static_cast<std::int32_t>(red) << 8 |
static_cast<std::int32_t>(green) << 4 |
static_cast<std::int32_t>(blue);
}
void Color_F::print_hex() const {
printf("[%04x]", (static_cast<std::int32_t>(red) << 8) |
(static_cast<std::int32_t>(green) << 4) |
(static_cast<std::int32_t>(blue)));
}
Color_F Color_F::operator*(float scalar) const {
return Color_F(red * scalar, green * scalar, blue * scalar);
}
void Color_F::operator*=(float scalar) {
red *= scalar;
green *= scalar;
blue *= scalar;
}
Color_F Color_F::operator+(const Color_F& rhs) const {
return Color_F(red + rhs.red, green + rhs.green, blue + rhs.blue);
}
Color_F Color_F::operator+(const Color_UINT8& rhs) const {
return Color_F(red + rhs.red, green + rhs.green, blue + rhs.blue);
}
void Color_F::operator+=(const Color_F& rhs) {
red += rhs.red;
green += rhs.green;
blue += rhs.blue;
}
void Color_F::operator+=(const Color_UINT8& rhs) {
red += rhs.red;
green += rhs.green;
blue += rhs.blue;
}
Color_UINT8 Color_F::color_scale() const {
float total_color = red + green + blue;
float new_red = red / total_color * 255 + .5;
float new_green = green / total_color * 255 + .5;
float new_blue = blue / total_color * 255 + .5;
return Color_UINT8(static_cast<std::uint8_t>(new_red),
static_cast<std::uint8_t>(new_green),
static_cast<std::uint8_t>(new_blue));
}
} // namespace Tracer