Thread Tools Display Modes
05-02-09, 11:16 AM   #1
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
Storing Colors in Lua

The most commen way to store colors in Lua is using tables, like this
Code:
local color = { r = 1, b = 0.5, g = 0.75 }
Although I prefer this way, because it works with unpack()
Code:
local color = { 1, 0.5, 0.75 }
But I've been thinking, wouldn't storing colors in a single number be more efficient, as that would use no tables. Then you just have to throw them through a function like this to get the rgb values.
Code:
function DecodeColor(color)
	local r = bit.band(bit.rshift(color,16),255);
	local g = bit.band(bit.rshift(color,8),255);
	local b = bit.band(color,255);
	return r / 255, g / 255, b / 255;
end

print(DecodeColor(0xff80c0))
  Reply With Quote
05-02-09, 12:16 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I'm confused.... Wouldn't you be doing more work there? I'm a beginner, so please enlighten me on why having a function with three bit.band calls (referencing another function) be better. I thought lua was very efficient with table lookups?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
05-02-09, 12:38 PM   #3
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
I very much doubt it would make enough difference to be worthwhile.
  Reply With Quote
05-02-09, 12:59 PM   #4
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
You gain a bit of memory efficiency at the expense of a bit of CPU time. It won't matter either way most of the time, but generally it's better to be CPU efficient. Still, if you're transmitting a lot of data to other users, quickly, or storing huge amounts, then there's a point where this kind of compression will be useful.
  Reply With Quote
05-02-09, 01:55 PM   #5
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
Yes, there would be times where this is not the ideal solution, but lets make a scenario where you just have to keep a lot of user defined colors which are stored between sessions, and all you do is to apply them when variables has been loaded. In such a case, wouldn't this be ideal, rather than loads of tables?
  Reply With Quote
05-02-09, 07:52 PM   #6
Exawatt
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Feb 2009
Posts: 36
Originally Posted by Aezay View Post
Yes, there would be times where this is not the ideal solution, but lets make a scenario where you just have to keep a lot of user defined colors which are stored between sessions, and all you do is to apply them when variables has been loaded. In such a case, wouldn't this be ideal, rather than loads of tables?
Depending on how they're going to be accessed, you could try using one table, like so
Code:
colors = {
	[1] = 255, --1r
	[2] = 0, --1g
	[3] = 0, --1b
	[4] = 255, --2r
	[5] = 255, --2g
	[6] = 0, --2b
}
Then use something like this to get your colors out:
Code:
whichColor = 2

r = colors[3 * whichColor - 2]
g = colors[3 * whichColor - 1]
b = colors[3 * whichColor]
Or something like that. That way, you're only using one table for all your colors.
  Reply With Quote
05-03-09, 09:36 AM   #7
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
Exawatt
That is a good alternative, didn't even think of that, thanks for the suggestion.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Storing Colors in Lua


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off