Color Save/Restore

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Color Save/Restore

Post by Ranguna259 »

This is not a library or a game it's just two pieces of code that became really helpful to me, so why not share it ;)

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)
Color profile exemple:

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)
You can even save the current color to a profile:

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)
Those were some (or all) exemples of what these two function can do.
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
( 'SaveRestor' profile is used for fast save and restore, so if you set a RGB to it, it'll change it again if you do saveColor() )
Licence free to use, share and copy for commercial and none commercial works, no need to copy the licence to your work.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Color Save/Restore

Post by raidho36 »

Making a function for a one-liner piece of code is an utter performance waste. And I don't honestly beleive that something as simple as that really needs to be a library.

Code: Select all

r, g, b, a= love.graphics.getColor ( )
love.graphics.draw ( )
love.graphics.setColor ( r, g, b, a )
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Color Save/Restore

Post by Ranguna259 »

Actualy

Code: Select all

r, g, b, a= love.graphics.getColor ( )
love.graphics.setColor(your color)
love.graphics.draw ( )
love.graphics.setColor ( r, g, b, a )
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Color Save/Restore

Post by raidho36 »

You can actually make a useful library by making hsv-rgb converter. Although I'm pretty sure there's vast loads of them already.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Color Save/Restore

Post by Ranguna259 »

Yeah, found this:

Code: Select all

function HSV(h, s, v)
    if s <= 0 then return v,v,v end
    h, s, v = h/256*6, s/255, v/255
    local c = v*s
    local x = (1-math.abs((h%2)-1))*c
    local m,r,g,b = (v-c), 0,0,0
    if h < 1     then r,g,b = c,x,0
    elseif h < 2 then r,g,b = x,c,0
    elseif h < 3 then r,g,b = 0,c,x
    elseif h < 4 then r,g,b = 0,x,c
    elseif h < 5 then r,g,b = x,0,c
    else              r,g,b = c,0,x
    end return (r+m)*255,(g+m)*255,(b+m)*255
end
But the main thing about those two functions is the saveColor(name of color,r,g,b) and then you can do stuff like restoreColor('red') and restoreColor('brown')
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Color Save/Restore

Post by raidho36 »

Then how about replace that with "createColor" and "reuseColor"?
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Color Save/Restore

Post by Ranguna259 »

Sure, what are your ideas ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Color Save/Restore

Post by Germanunkol »

Well, I think it's nice that it lets you use names and associate them with colors.

Even if that's very simple to do as well, this library, or at least the idea behind it, can come in handy when using lots of color switches.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Color Save/Restore

Post by raidho36 »

If I was doing it, I'd made it like that:

Code: Select all

--color.lua
local _r = { }
local _g = { }
local _b = { }
local _a = { }

function loadColors ( colors )
    for col, tbl in pairs ( colors ) do
        _r[ col ] = tbl[ 1 ]
        _g[ col ] = tbl[ 2 ]
        _b[ col ] = tbl[ 3 ]
        _a[ col ] = tbl[ 4 ] or 255
    end
end

function newColor ( color, r, g, b, a )
    if b then    
        _r[ color ] = r
        _g[ color ] = g
        _b[ color ] = b
        _a[ color ] = a or 255
    else
        _r[ color ], _g[ color ], _b[ color ], _a[ color ] = love.graphics.getColor ( )
    end
end

function useColor ( color )
    love.graphics.setColor ( _r[ color ], _g[ color ], _b[ color ] )
end

function useColorAlpha ( color )
    love.graphics.setColor ( _r[ color ], _g[ color ], _b[ color ], _a[ color ] )
end
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Color Save/Restore

Post by Ranguna259 »

How about this:

Code: Select all

--color.lua
local _r = { }
local _g = { }
local _b = { }
local _a = { }

function loadColors ( colors )
    for col, tbl in pairs ( colors ) do
        _r[ col ] = tbl[ 1 ]
        _g[ col ] = tbl[ 2 ]
        _b[ col ] = tbl[ 3 ]
        _a[ col ] = tbl[ 4 ] or 255
    end
end

function newColor ( color, r, g, b, a )
	if not color then
		color='FastColor'
	end
	if b then   
		_r[ color ] = r
		_g[ color ] = g
		_b[ color ] = b
		_a[ color ] = a or 255
	else
		_r[ color ], _g[ color ], _b[ color ], _a[ color ] = love.graphics.getColor ( )
	end
end

function useColor ( color, alpha )
	if not color then
		color='FastColor'
	end
	if alpha~=true then
		love.graphics.setColor ( _r[ color ], _g[ color ], _b[ color ] )
	else
		love.graphics.setColor ( _r[ color ], _g[ color ], _b[ color ], _a[ color ] )
	end
end
exemple code:

Code: Select all

require 'color'
function love.load()
	 newColor('white',255,255,255)
	 newColor('black',0,0,0)
	 newColor('magenta',255,0,255,100)
end
function love.draw()
	 love.graphics.setColor(255,0,0)
	 newColor()
	 love.graphics.print('red',10,10)
	 useColor('white')
	 love.graphics.print('white',10,25)
	 useColor('magenta')
	 love.graphics.print('magenta',10,40)
	 useColor('magenta',true)
	 love.graphics.print('alphaed magenta',10,55)
	 useColor()
	 love.graphics.print('red again',10,70)
	 useColor('black')
	 love.graphics.print("can't see me",10,85)
end
FastColor is fast load reload, like SaveRestor
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: No registered users and 212 guests