Jump to content




Colour fading function does not work.


1 reply to this topic

#1 rahph

  • Members
  • 81 posts

Posted 13 July 2017 - 02: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.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 July 2017 - 12: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 by Bomb Bloke, 14 July 2017 - 12:34 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users