B4R Code Snippet ByteToRGB algorithm

Greetings,
Most of the data that I am processing for use with WS2812B LEDs is 8 bits and the WS2812B libraries (rNSRainbow and rAdafruitNeoPixels) require RGB format. So I needed a way to convert bytes into RGB. I remembered having done so in an earlier Arduino project and was able to translated the algorithm into Basic. The following Basic code can produce a full range of 256 colors.

I'm not an expert programmer and cannot explain exactly how the code works, but please let me know if you have any suggestions or corrections.

B4X:
' Input a value of 0 To 255 to get an RGB color value. The colors are a transition r - g - b - back To r.
Private Sub ByteToRGB(Value As Byte)
    If Value < 85 Then
        'Log(Value * 3, 255 - Value * 3, 0)
        Strip1.setColor(LedNum, (Value * 3), ((255 - Value) * 3), 0)
    Else If Value < 170 Then
        Value = Value -85
        'Log(255 - Value * 3, 0, Value * 3)
        Strip1.setColor(LedNum, ((255 - Value) * 3), 0, (Value * 3))
    Else
        Value = Value - 170
        'Log(0, Value * 3, 255 - Value * 3)
        Strip1.setColor(LedNum, 0, (Value * 3), ((255 - Value) * 3))
    End If
    Strip1.show
End Sub
 
Last edited:
Top