File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " doctest.h"
2+ #include " util/hsv.hh"
3+
4+ TEST_CASE (" HSV to Color" ) {
5+ // check primary colors
6+ CHECK (Color{HSVColor{0 , 255 , 255 }} == Colors::red);
7+ CHECK (Color{HSVColor{86 , 255 , 255 }} == Colors::green);
8+ CHECK (Color{HSVColor{172 , 255 , 255 }} == Colors::blue);
9+
10+ // check no saturation
11+ const auto ns = Color{HSVColor{172 , 0 , 255 }};
12+ CHECK (ns.red () == ns.green ());
13+ CHECK (ns.red () == ns.blue ());
14+ CHECK (ns.red () == 255 );
15+
16+ // check value
17+ const auto v = Color{HSVColor{0 , 255 , 0 }};
18+ CHECK (v == Colors::black);
19+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include " util/colors.hh"
4+ #include < cstdint>
5+
6+ class HSVColor {
7+ public:
8+ uint8_t h;
9+ uint8_t s;
10+ uint8_t v;
11+
12+ constexpr operator Color () const {
13+ if (!s) {
14+ return Color{v, v, v};
15+ }
16+
17+ const auto region = h / 43u ;
18+ const auto remainder = (h - (region * 43u )) * 6u ;
19+
20+ const uint8_t p = (v * (255u - s)) >> 8u ;
21+ const uint8_t q = (v * (255u - ((s * remainder) >> 8u ))) >> 8u ;
22+ const uint8_t t = (v * (255u - ((s * (255u - remainder)) >> 8u ))) >> 8u ;
23+
24+ switch (region) {
25+ case 0 :
26+ return Color{v, t, p};
27+ case 1 :
28+ return Color{q, v, p};
29+ case 2 :
30+ return Color{p, v, t};
31+ case 3 :
32+ return Color{p, q, v};
33+ case 4 :
34+ return Color{t, p, v};
35+ default :
36+ return Color{v, p, q};
37+ }
38+ }
39+ };
You can’t perform that action at this time.
0 commit comments