I've came across times where I had to save the current color into local variables, change the color to the color I wanted and then changing it back, this process occupies a total of three lines of code, not much but It can get smaller to two lines (not much but) and WAY better.
With this code you can do things like fast RGB changes with the actual color name, type one line to save and another to restore, simple.
Some exemples for the use:
Code: Select all
--(love's default color is white, 255,255,255)
saveColor()
love.graphics.print('white',10,10)
love.graphics.setColor(255,0,0)
love.graphics.print('red',10,25)
restoreColor()
love.graphics.print('white again',10,40)
Code: Select all
saveColor('magenta',255,0,255)
saveColor()
love.graphics.print('white',10,10)
restoreColor('magenta')
love.graphics.print('magenta',10,25)
retoreColor()
love.graphics.print('white again',10,40)
Code: Select all
saveColor()
love.graphics.print('white',10,10)
love.graphics.setColor(255,0,0)
saveColor('red')
love.graphics.print('red',10,25)
restoreColor()
love.graphics.print('white again',10,40)
restoreColor('red')
love.graphics.print('red again',10,55)
And here they are:
Code: Select all
function restoreColor(profile)
if colorsavetable==nil then colorsavetable={} return end
if profile==nil then profile='SaveRestore' end
love.graphics.setColor(colorsavetable[profile].r,colorsavetable[profile].g,colorsavetable[profile].b)
end
function saveColor(profile,r,g,b)
if colorsavetable==nil then colorsavetable={} end
if profile==nil then profile='SaveRestor' end
colorsavetable[profile]={}
if profile~='SaveRestore' and r~=nil and g~=nil and b~=nil then
colorsavetable[profile].r,colorsavetable[profile].g,colorsavetable[profile].b=r,g,b
else
colorsavetable[profile].r,colorsavetable[profile].g,colorsavetable[profile].b=love.graphics.getColor()
end
end
Licence free to use, share and copy for commercial and none commercial works, no need to copy the licence to your work.