FastLED 3.9.15
Loading...
Searching...
No Matches

◆ hsv2rgb_raw_C()

void hsv2rgb_raw_C ( const struct CHSV & hsv,
struct CRGB & rgb )

HSV to RGB implementation in raw C, platform independent.

Definition at line 41 of file hsv2rgb.cpp.

42{
43 // Convert hue, saturation and brightness ( HSV/HSB ) to RGB
44 // "Dimming" is used on saturation and brightness to make
45 // the output more visually linear.
46
47 // Apply dimming curves
48 uint8_t value = APPLY_DIMMING( hsv.val); // cppcheck-suppress selfAssignment
49 uint8_t saturation = hsv.sat;
50
51 // The brightness floor is minimum number that all of
52 // R, G, and B will be set to.
53 uint8_t invsat = APPLY_DIMMING( 255 - saturation); // cppcheck-suppress selfAssignment
54 uint8_t brightness_floor = (value * invsat) / 256;
55
56 // The color amplitude is the maximum amount of R, G, and B
57 // that will be added on top of the brightness_floor to
58 // create the specific hue desired.
59 uint8_t color_amplitude = value - brightness_floor;
60
61 // Figure out which section of the hue wheel we're in,
62 // and how far offset we are withing that section
63 uint8_t section = hsv.hue / HSV_SECTION_3; // 0..2
64 uint8_t offset = hsv.hue % HSV_SECTION_3; // 0..63
65
66 uint8_t rampup = offset; // 0..63
67 uint8_t rampdown = (HSV_SECTION_3 - 1) - offset; // 63..0
68
69 // We now scale rampup and rampdown to a 0-255 range -- at least
70 // in theory, but here's where architecture-specific decsions
71 // come in to play:
72 // To scale them up to 0-255, we'd want to multiply by 4.
73 // But in the very next step, we multiply the ramps by other
74 // values and then divide the resulting product by 256.
75 // So which is faster?
76 // ((ramp * 4) * othervalue) / 256
77 // or
78 // ((ramp ) * othervalue) / 64
79 // It depends on your processor architecture.
80 // On 8-bit AVR, the "/ 256" is just a one-cycle register move,
81 // but the "/ 64" might be a multicycle shift process. So on AVR
82 // it's faster do multiply the ramp values by four, and then
83 // divide by 256.
84 // On ARM, the "/ 256" and "/ 64" are one cycle each, so it's
85 // faster to NOT multiply the ramp values by four, and just to
86 // divide the resulting product by 64 (instead of 256).
87 // Moral of the story: trust your profiler, not your insticts.
88
89 // Since there's an AVR assembly version elsewhere, we'll
90 // assume what we're on an architecture where any number of
91 // bit shifts has roughly the same cost, and we'll remove the
92 // redundant math at the source level:
93
94 // // scale up to 255 range
95 // //rampup *= 4; // 0..252
96 // //rampdown *= 4; // 0..252
97
98 // compute color-amplitude-scaled-down versions of rampup and rampdown
99 uint8_t rampup_amp_adj = (rampup * color_amplitude) / (256 / 4);
100 uint8_t rampdown_amp_adj = (rampdown * color_amplitude) / (256 / 4);
101
102 // add brightness_floor offset to everything
103 uint8_t rampup_adj_with_floor = rampup_amp_adj + brightness_floor;
104 uint8_t rampdown_adj_with_floor = rampdown_amp_adj + brightness_floor;
105
106
107 if( section ) {
108 if( section == 1) {
109 // section 1: 0x40..0x7F
110 rgb.r = brightness_floor;
111 rgb.g = rampdown_adj_with_floor;
112 rgb.b = rampup_adj_with_floor;
113 } else {
114 // section 2; 0x80..0xBF
115 rgb.r = rampup_adj_with_floor;
116 rgb.g = brightness_floor;
117 rgb.b = rampdown_adj_with_floor;
118 }
119 } else {
120 // section 0: 0x00..0x3F
121 rgb.r = rampdown_adj_with_floor;
122 rgb.g = rampup_adj_with_floor;
123 rgb.b = brightness_floor;
124 }
125}
#define APPLY_DIMMING(X)
Apply dimming compensation to values.
Definition hsv2rgb.cpp:31
#define HSV_SECTION_3
Divide the color wheel into four sections, 64 elements each.
Definition hsv2rgb.cpp:39

References APPLY_DIMMING, and HSV_SECTION_3.

Referenced by hsv2rgb_raw().

+ Here is the caller graph for this function: