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 51 of file hsv2rgb.cpp.

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

References APPLY_DIMMING, HSV_SECTION_3, and offset().

Referenced by hsv2rgb_raw().

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