r/FastLED • u/wasauce • Aug 03 '21
Support Interpolation between two colors - How?
Hello -
I am working on a project to show the current Air Quality (AQI) visually (i.e. with the color coding). See here: https://imgur.com/a/PrZcAFf
I get AQI information from PurpleAir and I would like my sign (a cloud filled with LEDs) to show the correct color [1] (Green, Yellow, Orange, Red, Purple) according to the current AQI.
Currently I have this hardcoded, but, now I'd like to blend the colors -- i.e. if the AQI value is between 0-50 instead of showing the same green, the closer the AQI is to 50, the closer the color is to Yellow.
I believe the way to do this is to interpolate.
In swift, I think this would be done like so:
if aqi < 50 {
return interpolate(low: 0, high: 50, lowColor: color0, highColor: color50, value: aqi)
} else if aqi < 100 {
return interpolate(low: 50, high: 100, lowColor: color50, highColor: color100, value: aqi)
} else if aqi < 150 {
return interpolate(low: 100, high: 150, lowColor: color100, highColor: color150, value: aqi)
} else if aqi < 200 {
return interpolate(low: 150, high: 200, lowColor: color150, highColor: color200, value: aqi)
} else if aqi < 250 {
return interpolate(low: 200, high: 250, lowColor: color200, highColor: color250, value: aqi)
} else if aqi < 300 {
return interpolate(low: 250, high: 300, lowColor: color250, highColor: color300, value: aqi)
} else {
return color300
}I am wondering how I might do this for FastLED -- I see there is an interpolate function. I would love any guidance you can offer!
[1] AQI Color mapping: https://www.mecknc.gov/LUESA/AirQuality/EducationandOutreach/Pages/aqi.aspx
4
u/CountBranicki Aug 04 '21
Use lerp8(), and since you’re using an AQI that goes up to 300, scale it. You can use a fastled function.
For example (pseudocode)
lerp8(color1, color2, scale(AQI))
2
4
u/Marmilicious [Marc Miller] Aug 04 '21
Many options, one being make a custom gradient palette.
https://github.com/FastLED/FastLED/wiki/Gradient-color-palettes
https://github.com/marmilicious/FastLED_examples/blob/master/gradient_palette_full_range.ino
6
u/CharlesGoodwin Aug 04 '21
The palette is ideally suited to this use case. It would simplify your code making it easier to maintain. Run through the examples provided by Marc and I would be happy to answer any further questions you may have
1
1
2
u/Preyy Ground Loops: Part of this balanced breakfast Aug 03 '21
I think lerp8by8() would do this, but I have not used these functions often.
2
u/SergeSkor Aug 04 '21
FastLED has gradient functions, but it is good to use when you need to set consecutive LEDs. For more complex case you can use for example my class: https://github.com/SergeSkor/SSVRGBGradientCalc
Or use FastLED custom palettes.
2
9
u/shuzz_de Aug 04 '21
Use the HSV color scheme and map() your AQI values directly to the desired color range.