-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cpp
More file actions
121 lines (99 loc) · 3.21 KB
/
Color.cpp
File metadata and controls
121 lines (99 loc) · 3.21 KB
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
#include "Color.hpp"
unsigned char denormalizeColor(double color)
{
unsigned char result;
if (color > 1.0)
{
result = 255;
}
else if (color < 0)
{
result = 0;
}
else
{
result = static_cast<unsigned char>(color * 255.0);
}
return result;
}
Color Color::getDenormalizedColor(double r, double g, double b)
{
Color result{ 0, 0, 0 };
result.r = static_cast<unsigned char>(denormalizeColor(r));
result.g = static_cast<unsigned char>(denormalizeColor(g));
result.b = static_cast<unsigned char>(denormalizeColor(b));
return result;
}
Color::Color(double color)
{
r = static_cast<unsigned char>(0x00ffffff << 8 >> 24);
r = static_cast<unsigned char>(r * color);
g = static_cast<unsigned char>(0x00ffffff << 16 >> 24);
g = static_cast<unsigned char>(g * color);
b = static_cast<unsigned char>(0x00ffffff << 24 >> 24);
b = static_cast<unsigned char>(b * color);
}
Color::Color(unsigned int color)
{
r = static_cast<unsigned char>(color << 8 >> 24);
g = static_cast<unsigned char>(color << 16 >> 24);
b = static_cast<unsigned char>(color << 24 >> 24);
}
unsigned int Color::asUnsigned() const
{
return 0xff000000 | (r << 16) | (g << 8) | b;
}
ColorChannels Color::getColorChannels() const
{
return std::make_tuple(r, g, b);
}
NormalizedColorChannels Color::getNormalizedColorChannels() const
{
auto red = static_cast<double>(r) / 255.0;
auto green = static_cast<double>(g) / 255.0;
auto blue = static_cast<double>(b) / 255.0;
return std::make_tuple(red, green, blue);
}
bool Color::operator==(const Color& other) const
{
return other.asUnsigned() == asUnsigned();
}
Color operator*(const Color& l, const Color& r)
{
auto leftChannels = l.getNormalizedColorChannels();
auto lr = std::get<0>(leftChannels);
auto lg = std::get<1>(leftChannels);
auto lb = std::get<2>(leftChannels);
auto rightChannels = r.getNormalizedColorChannels();
auto rr = std::get<0>(rightChannels);
auto rg = std::get<1>(rightChannels);
auto rb = std::get<2>(rightChannels);
return Color::getDenormalizedColor(lr*rr, lg*rg, lb*rb);
}
Color operator+(const Color & l, const Color & r)
{
auto leftChannels = l.getNormalizedColorChannels();
auto lr = std::get<0>(leftChannels);
auto lg = std::get<1>(leftChannels);
auto lb = std::get<2>(leftChannels);
auto rightChannels = r.getNormalizedColorChannels();
auto rr = std::get<0>(rightChannels);
auto rg = std::get<1>(rightChannels);
auto rb = std::get<2>(rightChannels);
return Color::getDenormalizedColor(lr+rr, lg+rg, lb+rb);
}
Color colorWithOpacity(const Color& newColor, const Color& oldColor, double opacity)
{
auto oldColorChannels = oldColor.getColorChannels();
auto oldRed = std::get<0>(oldColorChannels);
auto oldGreen = std::get<1>(oldColorChannels);
auto oldBlue = std::get<2>(oldColorChannels);
auto newColorChannels = newColor.getColorChannels();
auto newRed = std::get<0>(newColorChannels);
auto newGreen = std::get<1>(newColorChannels);
auto newBlue = std::get<2>(newColorChannels);
auto red = static_cast<unsigned char>((opacity * newRed) + ((1 - opacity)*oldRed));
auto green = static_cast<unsigned char>((opacity * newGreen) + ((1 - opacity)*oldGreen));
auto blue = static_cast<unsigned char>((opacity * newBlue) + ((1 - opacity)*oldBlue));
return Color(red, green, blue);
}