View Single Post
02-26-09, 11:04 AM   #819
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Two functions, both give exactly the same result and works fine.

Codewise, which one is the best and if possible to know why ?

OLD
Code:
local function UpdateColor(self, element, unit, func)
	local colorA, colorB

	if(UnitIsDead(unit) or UnitIsGhost(unit) or not UnitIsConnected(unit)) then
		colorA = colors.disconnected
	elseif(UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
		colorA = colors.tapped
	elseif(unit == 'player' or unit == 'pet') then
		local num, str = UnitPowerType(unit)
		colorA = colors.power[str]
	elseif(UnitIsPlayer(unit)) then
		local _, class = UnitClass(unit)
		colorA = colors.class[class]
	else
		colorB = FACTION_BAR_COLORS[UnitReaction(unit, 'player')]
	end

	if(colorA) then
		if(func == 'SetVertexColor') then
			element[func](element, colorA[1] * .33, colorA[2] * .33, colorA[3] * .33)
		else
			element[func](element, colorA[1], colorA[2], colorA[3])
		end
	elseif(colorB) then
		if(func == 'SetVertexColor') then
			element[func](element, colorB.r * .33, colorB.g * .33, colorB.b * .33)
		else
			element[func](element, colorB.r, colorB.g, colorB.b)
		end
	end
end
NEW
Code:
local function Hex(r, g, b)
	if(type(r) == 'table') then
		if(r.r) then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end

	if(not r or not g or not b) then
		r, g, b = 1, 1, 1
	end

	return r, g, b
end

local function UpdateColor(self, element, unit, func)
	if(UnitIsDead(unit) or UnitIsGhost(unit) or not UnitIsConnected(unit)) then
		r, g, b = Hex(colors.disconnected)
	elseif(UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
		r, g, b = Hex(colors.tapped)
	elseif(unit == 'player' or unit == 'pet') then
		local num, str = UnitPowerType(unit)
		r, g, b = Hex(colors.power[str])
	elseif(UnitIsPlayer(unit)) then
		local _, class = UnitClass(unit)
		r, g, b = Hex(colors.class[class])
	else
		local reaction = UnitReaction(unit, 'player')
		r, g, b = Hex(FACTION_BAR_COLORS[reaction])
	end

	if(func == 'SetVertexColor') then
		element[func](element, r * .33, g * .33, b * .33)
	else
		element[func](element, r, g, b)
	end
end
Now in case it is the second one:

is it possible to merge oUF original Hex function (with the string) with this one ?
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }