FastLED 3.10.8
Loading...
Searching...
No Matches

◆ RGBtoHSV16()

static HSV16 fl::RGBtoHSV16 ( const CRGB & rgb)
static

Definition at line 20 of file hsv16.cpp.hpp.

20 {
21 // Work with 8-bit values directly
22 u8 r = rgb.r;
23 u8 g = rgb.g;
24 u8 b = rgb.b;
25
26 // Find min and max
27 u8 mx = max(r, max(g, b));
28 u8 mn = min(r, min(g, b));
29 u8 delta = mx - mn;
30
31 u16 h = 0;
32 u16 s = 0;
33 u16 v = scale8_to_16_accurate(mx);
34
35 // Calculate saturation using improved scaling
36 if (mx > 0) {
37 // s = (delta * 65535) / mx, but with better accuracy
38 // Use the same technique as scale8_to_16_accurate but for arbitrary denominator
39 if (delta == mx) {
40 s = 65535; // Saturation is 100%
41 } else {
42 s = (u16)(((u32)delta * 65535 + (mx >> 1)) / mx);
43 }
44 }
45
46 // Calculate hue using improved algorithms
47 if (delta > 0) {
48 u32 hue_calc = 0;
49
50 if (mx == r) {
51 // Hue in red sector (0-60 degrees)
52 if (g >= b) {
53 // Use improved division: hue_calc = (g - b) * 65535 / (6 * delta)
54 u32 numerator = (u32)(g - b) * 65535;
55 if (delta <= 42) { // 6 * 42 = 252, safe for small delta
56 hue_calc = numerator / (6 * delta);
57 } else {
58 hue_calc = numerator / delta / 6; // Avoid overflow
59 }
60 } else {
61 u32 numerator = (u32)(b - g) * 65535;
62 if (delta <= 42) {
63 hue_calc = 65535 - numerator / (6 * delta);
64 } else {
65 hue_calc = 65535 - numerator / delta / 6;
66 }
67 }
68 } else if (mx == g) {
69 // Hue in green sector (60-180 degrees)
70 // Handle signed arithmetic properly to avoid integer underflow
71 i32 signed_diff = (i32)b - (i32)r;
72 u32 sector_offset = 65535 / 3; // 60 degrees (120 degrees in 16-bit space)
73
74 if (signed_diff >= 0) {
75 // Positive case: b >= r
76 u32 numerator = (u32)signed_diff * 65535;
77 if (delta <= 42) {
78 hue_calc = sector_offset + numerator / (6 * delta);
79 } else {
80 hue_calc = sector_offset + numerator / delta / 6;
81 }
82 } else {
83 // Negative case: b < r
84 u32 numerator = (u32)(-signed_diff) * 65535;
85 if (delta <= 42) {
86 hue_calc = sector_offset - numerator / (6 * delta);
87 } else {
88 hue_calc = sector_offset - numerator / delta / 6;
89 }
90 }
91 } else { // mx == b
92 // Hue in blue sector (180-300 degrees)
93 // Handle signed arithmetic properly to avoid integer underflow
94 i32 signed_diff = (i32)r - (i32)g;
95 u32 sector_offset = (2 * 65535) / 3; // 240 degrees (240 degrees in 16-bit space)
96
97 if (signed_diff >= 0) {
98 // Positive case: r >= g
99 u32 numerator = (u32)signed_diff * 65535;
100 if (delta <= 42) {
101 hue_calc = sector_offset + numerator / (6 * delta);
102 } else {
103 hue_calc = sector_offset + numerator / delta / 6;
104 }
105 } else {
106 // Negative case: r < g
107 u32 numerator = (u32)(-signed_diff) * 65535;
108 if (delta <= 42) {
109 hue_calc = sector_offset - numerator / (6 * delta);
110 } else {
111 hue_calc = sector_offset - numerator / delta / 6;
112 }
113 }
114 }
115
116 h = (u16)(hue_calc & 0xFFFF);
117 }
118
119 return HSV16{h, s, v};
120}
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
InputGamut g
Definition rgbw.h:125
static u16 scale8_to_16_accurate(u8 x) FL_NO_EXCEPT
Definition hsv16.cpp.hpp:12

References FL_NO_EXCEPT, g, max(), min(), and scale8_to_16_accurate().

Referenced by fl::HSV16::HSV16().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: