I have a linux/bash script that sends multiple RGB color fades to multiple light. Every light can have it's own color but all colors should use the same amount of time to fade in/out. So it sends out a sequence of 3 differently targeted value-ranges depending on the initial values and based on a speed multiplier.
The problem I run into is that colors are defined by 3 channels Red, Green & Blue (0-255) And depending on the targeted color it could mean that one channel has a value of 10 and the other of 230. And for it to work smoothly I need them to start at 0 and finish at their maximum in the same amount of time/steps. To make it more problematic is the fact that I cannot use values as 0.112. It needs to be either 0 or 1.
At the moment I've been able to solve this problem by limiting the amount of colors I use and only set "half-range" values per channel. eg. R255 G128 B000. In this way I've been able to make it work (within an acceptable margin of inaccuracy.) for each channel I've made a separate base-multiplier that will influence the speed of each channel fading in/out (so for 255 2x, for 128 1x, for 000, 0x)
Since I have 3 lights and I only want to send out the value sequence to the lights that I need I also add up the 3 RGB values to see if the equal zero, if so, that assigned light will not be triggered.
[Q] Can somebody help me to optimise this script so it could work with all RGB values and I would also be able to make it fade between colors. The most important conditions will be that I need to be able to apply a global speed multiplier and the can not be values behind the comma.
Below is the script what I've made so far. I've taken out the duplicates for the other lights as it's basically copy-paste of the same lines, but with other names.
maxR=000, maxG=000, maxB=255, speed=10,
toggle=$(($maxR + $maxG + $maxB))
subR=$(echo $maxR | cut -c-1), subG=$(echo $maxG | cut -c-1), subB=$(echo $maxB | cut -c-1),
mulR=$(($subR*$speed)), mulG=$(($subG*$speed)), mulB=$(($subB*$speed))
count=0
while [ $count -lt 3 ] ; do
count=$(($count +1))
while [ $varR -lt $maxR ] || [ $valG -lt $maxG ] || [ $valB -lt $maxB ] ; do
[[ $toggle -gt 0 ]] && $valR to red && $valG to green && $valB to blue
varR=$(($varR +$mulR))
valG=$(($valG +$mulG))
valB=$(($valB +$mulB))
done
sleep 1
while [ $varR -gt 0 ] || [ $valG -gt 0 ] || [ $valB -gt 0 ] ; do
[[ $toggle -gt 0 ]] && $valR to red && $valG to green && $valB to blue
varR=$(($varR -$mulR))
valG=$(($valG -$mulG))
valB=$(($valB -$mulB))
done
sleep 3
done