19 {
20
24
25
29
30 u16 h = 0;
31 u16 s = 0;
33
34
35 if (mx > 0) {
36
37
38 if (delta == mx) {
39 s = 65535;
40 } else {
41 s = (u16)(((u32)delta * 65535 + (mx >> 1)) / mx);
42 }
43 }
44
45
46 if (delta > 0) {
47 u32 hue_calc = 0;
48
49 if (mx == r) {
50
51 if (g >= b) {
52
53 u32 numerator = (u32)(g - b) * 65535;
54 if (delta <= 42) {
55 hue_calc = numerator / (6 * delta);
56 } else {
57 hue_calc = numerator / delta / 6;
58 }
59 } else {
60 u32 numerator = (u32)(b - g) * 65535;
61 if (delta <= 42) {
62 hue_calc = 65535 - numerator / (6 * delta);
63 } else {
64 hue_calc = 65535 - numerator / delta / 6;
65 }
66 }
67 } else if (mx == g) {
68
69
70 i32 signed_diff = (i32)b - (i32)r;
71 u32 sector_offset = 65535 / 3;
72
73 if (signed_diff >= 0) {
74
75 u32 numerator = (u32)signed_diff * 65535;
76 if (delta <= 42) {
77 hue_calc = sector_offset + numerator / (6 * delta);
78 } else {
79 hue_calc = sector_offset + numerator / delta / 6;
80 }
81 } else {
82
83 u32 numerator = (u32)(-signed_diff) * 65535;
84 if (delta <= 42) {
85 hue_calc = sector_offset - numerator / (6 * delta);
86 } else {
87 hue_calc = sector_offset - numerator / delta / 6;
88 }
89 }
90 } else {
91
92
93 i32 signed_diff = (i32)r - (i32)g;
94 u32 sector_offset = (2 * 65535) / 3;
95
96 if (signed_diff >= 0) {
97
98 u32 numerator = (u32)signed_diff * 65535;
99 if (delta <= 42) {
100 hue_calc = sector_offset + numerator / (6 * delta);
101 } else {
102 hue_calc = sector_offset + numerator / delta / 6;
103 }
104 } else {
105
106 u32 numerator = (u32)(-signed_diff) * 65535;
107 if (delta <= 42) {
108 hue_calc = sector_offset - numerator / (6 * delta);
109 } else {
110 hue_calc = sector_offset - numerator / delta / 6;
111 }
112 }
113 }
114
115 h = (u16)(hue_calc & 0xFFFF);
116 }
117
118 return HSV16{h, s, v};
119}
FL_DISABLE_WARNING_PUSH U common_type_t< T, U > fl_min(T a, U b)
common_type_t< T, U > fl_max(T a, U b)
static u16 scale8_to_16_accurate(u8 x)