-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (140 loc) · 4.74 KB
/
main.cpp
File metadata and controls
170 lines (140 loc) · 4.74 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
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
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
#include "ColorWheelSchemes.h"
struct RgbColor {
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
RgbColor() = default;
RgbColor(float red, float green, float blue) : r(red), g(green), b(blue) {}
static float limit() {
return 255.0f;
}
static float clampChannel(float v) {
return std::clamp(v, 0.0f, limit());
}
static float wrapHue(float h) {
float wrapped = std::fmod(h, limit());
if (wrapped < 0.0f) {
wrapped += limit();
}
return wrapped;
}
static RgbColor fromHsb(float hue, float saturation, float brightness) {
const float h = (wrapHue(hue) / limit()) * 360.0f;
const float s = clampChannel(saturation) / limit();
const float v = clampChannel(brightness) / limit();
if (s <= 0.0f) {
const float gray = v * limit();
return RgbColor(gray, gray, gray);
}
const float hh = h / 60.0f;
const int i = static_cast<int>(std::floor(hh)) % 6;
const float f = hh - std::floor(hh);
const float p = v * (1.0f - s);
const float q = v * (1.0f - s * f);
const float t = v * (1.0f - s * (1.0f - f));
switch (i) {
case 0:
return RgbColor(v * limit(), t * limit(), p * limit());
case 1:
return RgbColor(q * limit(), v * limit(), p * limit());
case 2:
return RgbColor(p * limit(), v * limit(), t * limit());
case 3:
return RgbColor(p * limit(), q * limit(), v * limit());
case 4:
return RgbColor(t * limit(), p * limit(), v * limit());
default:
return RgbColor(v * limit(), p * limit(), q * limit());
}
}
void toHsb(float& hue, float& saturation, float& brightness) const {
const float rr = clampChannel(r) / limit();
const float gg = clampChannel(g) / limit();
const float bb = clampChannel(b) / limit();
const float maxv = std::max(rr, std::max(gg, bb));
const float minv = std::min(rr, std::min(gg, bb));
const float delta = maxv - minv;
brightness = maxv * limit();
saturation = (maxv <= 0.0f) ? 0.0f : (delta / maxv) * limit();
if (delta <= 0.0f) {
hue = 0.0f;
return;
}
float hueDegrees;
if (maxv == rr) {
hueDegrees = 60.0f * std::fmod(((gg - bb) / delta), 6.0f);
} else if (maxv == gg) {
hueDegrees = 60.0f * (((bb - rr) / delta) + 2.0f);
} else {
hueDegrees = 60.0f * (((rr - gg) / delta) + 4.0f);
}
if (hueDegrees < 0.0f) {
hueDegrees += 360.0f;
}
hue = (hueDegrees / 360.0f) * limit();
}
float getHue() const {
float h = 0.0f;
float s = 0.0f;
float v = 0.0f;
toHsb(h, s, v);
return h;
}
float getHueAngle() const {
return (getHue() / limit()) * 360.0f;
}
float getSaturation() const {
float h = 0.0f;
float s = 0.0f;
float v = 0.0f;
toHsb(h, s, v);
return s;
}
float getBrightness() const {
float h = 0.0f;
float s = 0.0f;
float v = 0.0f;
toHsb(h, s, v);
return v;
}
void setHue(float hue) {
*this = fromHsb(hue, getSaturation(), getBrightness());
}
void setSaturation(float saturation) {
*this = fromHsb(getHue(), saturation, getBrightness());
}
void setBrightness(float brightness) {
*this = fromHsb(getHue(), getSaturation(), brightness);
}
RgbColor& lerp(const RgbColor& other, float amount) {
const float t = std::clamp(amount, 0.0f, 1.0f);
r += (other.r - r) * t;
g += (other.g - g) * t;
b += (other.b - b) * t;
return *this;
}
};
int main() {
auto scheme = ofxColorTheory::ColorWheelSchemes_<RgbColor>::make(ofxColorTheory::ColorRule::Complementary);
if (!scheme) {
std::cerr << "Failed to construct scheme\n";
return 1;
}
scheme->setPrimaryColor(RgbColor(240.0f, 120.0f, 30.0f));
scheme->regenerate();
const auto palette = scheme->interpolate(12, ofxColorTheory::ColorSpace::Lch);
std::cout << "Generated " << palette.size() << " colors:\n";
for (std::size_t i = 0; i < palette.size(); ++i) {
const auto& c = palette[i];
std::cout << std::setw(2) << i << ": "
<< "(" << std::setw(6) << std::fixed << std::setprecision(1) << c.r << ", "
<< std::setw(6) << c.g << ", "
<< std::setw(6) << c.b << ")\n";
}
return 0;
}