Skip to content

Commit 501031d

Browse files
committed
fix HSL
1 parent d0db041 commit 501031d

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

common/color.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ class HSL {
172172
HSL rotate(float angle) {
173173
return HSL(fract(H + angle), S, L);
174174
}
175+
void printTo(Print& p) {
176+
p.print("HSL:");
177+
p.print(H);
178+
p.write(',');
179+
p.print(S);
180+
p.write(',');
181+
p.print(L);
182+
}
175183
float H; // 0 - 1.0
176184
float S; // 0 - 1.0
177185
float L; // 0 - 1.0
@@ -305,23 +313,23 @@ class Color16 {
305313
int MAX = std::max(r, std::max(g, b));
306314
int MIN = std::min(r, std::min(g, b));
307315
int C = MAX - MIN;
308-
int H;
316+
float H;
309317
// Note 16384 = 60 degrees.
310318
if (C == 0) {
311319
H = 0;
312320
} else if (r == MAX) {
313321
// r is biggest
314-
H = 16384 * (g - b) / C;
322+
H = (g - b) / (float)C;
315323
} else if (g == MAX) {
316324
// g is biggest
317-
H = 16384 * (b - r) / C + 16384 * 2;
325+
H = (b - r) / (float)C + 2.0f;
318326
} else {
319327
// b is biggest
320-
H = 16384 * (r - g) / C + 16384 * 4;
328+
H = (r - g) / (float)C + 4.0f;
321329
}
322330
int L = MIN + MAX;
323331
float S = (MAX*2 - L) / (float)std::min<int>(L, 131072 - L);
324-
return HSL(H / 98304.0, S, L / 131070.0);
332+
return HSL(fract(H / 6.0f), S, L / 131070.0);
325333
}
326334

327335
explicit Color16(HSL hsl) {

common/tests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ BladeConfig* current_config;
127127
if (fabs(x_ - y_) > D) { std::cerr << #X << " (" << x_ << ") ~!= " << #Y << " (" << y_ << ") line " << __LINE__ << std::endl; exit(1); } \
128128
} while(0)
129129

130+
#define CHECK_NEAR_MSG(X, Y, D, MSG) do { \
131+
auto x_ = (X); \
132+
auto y_ = (Y); \
133+
if (fabs(x_ - y_) > D) { STDOUT << #X << " (" << x_ << ") ~!= " << #Y << " (" << y_ << ") " << MSG << " line " << __LINE__ << "\n"; exit(1); } \
134+
} while(0)
135+
130136
#define CHECK_LT(X, Y) do { \
131137
auto x_ = (X); \
132138
auto y_ = (Y); \
@@ -326,6 +332,17 @@ void test_rotate(Color16 c, int angle) {
326332

327333
// Test HSL
328334
HSL hsl = c.toHSL();
335+
CHECK_LE(0.0, hsl.H);
336+
CHECK_LE(0.0, hsl.S);
337+
CHECK_LE(0.0, hsl.L);
338+
CHECK_LE(hsl.H, 1.0);
339+
CHECK_LE(hsl.S, 1.0);
340+
CHECK_LE(hsl.L, 1.0);
341+
Color16 result3(hsl);
342+
CHECK_NEAR_MSG(result3.r, c.r, 1, " in:" << c << " out:" << result3 << " hsl:" << hsl);
343+
CHECK_NEAR_MSG(result3.g, c.g, 1, " in:" << c << " out:" << result3 << " hsl:" << hsl);
344+
CHECK_NEAR_MSG(result3.b, c.b, 1, " in:" << c << " out:" << result3 << " hsl:" << hsl);
345+
329346
hsl = hsl.rotate(angle / (float)(32768 * 3));
330347
// fprintf(stderr, "Angle = %d HSL={%f,%f,%f} RGB=%d,%d,%d\n", angle, hsl.H, hsl.S, hsl.L, c.r, c.g, c.b);
331348
CHECK_LE(0.0, hsl.H);

0 commit comments

Comments
 (0)