This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rahph's profile picture

Colour fading function does not work.

Started by rahph, 13 July 2017 - 01:42 PM
rahph #1
Posted 13 July 2017 - 03:42 PM
Hello. I am making a program where I want the background to smoothly fade beetwen two diffrent colours.

My code so far:

local blue = {0x33,0x66,0xF2}
local lblue = {0x99,0xB2,0xFF}
local steps = 256
local s1 = (lblue[1] - blue[1])/128
local s2 = (lblue[2] - blue[2])/128
local s3 = (lblue[3] - blue[3])/128
print(s1)
print(s2)
print(s3)
local current = lblue
term.setBackgroundColor(colors.blue)
term.clear()
while true do
    for i=1,steps do
	    current[1] = current[1] - s1
	    current[2] = current[2] - s2
	    current[3] = current[3] - s3
	   sleep(0.05)
term.setPaletteColor(colors.blue,colors.rgb8(current[1]/255,current[2]/255,current[3]/255))
    end
    print("y")
    for i=1,steps do
	    current[1] = current[1] + s1
	    current[2] = current[2] + s2
	    current[3] = current[3] + s3
	    sleep(0.05)
	    term.setPaletteColor(colors.blue,colors.rgb8(current[1]/255,current[2]/255,current[3]/255))
    end
    print("h")
    os.queueEvent("_") os.pullEvent()
end
but it fails when decreasing the amount of steps. Also it randomly changes to magenta.

Please tell me where did I make a mistake. Any help will be appreciated.
Bomb Bloke #2
Posted 14 July 2017 - 01:34 AM
The difference between 0x99 (153) and 0x33 (51) is 0x66 (102). Dividing that by half your steps variable means that your loops will have current[1] vary by 0xCC (204), which is enough to send it into the negatives.

Because term.setPaletteColour() doesn't bother to do any range checking, this doesn't result in any sort of informative error, but rather you just see your red intensity act as if it'd looped back around to maximum again.

You should just divide by "steps".
Edited on 13 July 2017 - 11:34 PM