What if we wanted to slowly vary the color of one of our pixels through all the colors of the rainbow? What we would like is a function that would take in an number from 0 to 255 and walk around the color wheel as you change the input value. This is known as the `wheel function:
1 2 3 4 5 6 7 8 9101112
defwheel(pos):# Input a value 0 to 255 to get a color value.# The colors are a transition r - g - b - back to r.ifpos<0orpos>255:return(0,0,0)ifpos<85:return(255-pos*3,pos*3,0)ifpos<170:pos-=85return(0,255-pos*3,pos*3)pos-=170return(pos*3,0,255-pos*3)
If you follow the code, you will see that the colors range from red through green and to blue and back to red:
1234
wheel(1)=(255,0,0)# redwheel(85)=(0,255,0)# greenwheel(170)=(0,0,255)# bluewheel(255)=(255,0,0)# red
importmachinefromneopixelimportNeoPixelfromutimeimportsleepNEOPIXEL_PIN=0NUMBER_PIXELS=1strip=NeoPixel(machine.Pin(NEOPIXEL_PIN),NUMBER_PIXELS)defwheel(pos):# Input a value 0 to 255 to get a color value.# The colors are a transition r - g - b - back to r.ifpos<0orpos>255:return(0,0,0)ifpos<85:return(255-pos*3,pos*3,0)ifpos<170:pos-=85return(0,255-pos*3,pos*3)pos-=170return(pos*3,0,255-pos*3)counter=0whileTrue:strip[0]=wheel(counter)strip.write()sleep(.01)counter+=1# reset the counterifcounter==255:counter=0