WoW:USERAPI ColorGradient: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
 
(Optimized function)
Line 7: Line 7:
  local num = select("#", ...) / 3
  local num = select("#", ...) / 3
   
   
  if perc == 1 then return select(num-2, ...), select(num-1, ...), select(num, ...) end
  if perc >= 1 then
local r, g, b = select(num-2, ...)
return r, g, b
elseif perc <= 0 then
local r, g, b = ...
return r, g, b
end
   
   
  local segment, relperc = math.modf(perc*(num-1))
  local segment, relperc = math.modf(perc*(num-1))
  local r1, g1, b1 = select((segment*3)+1, ...), select((segment*3)+2, ...), select((segment*3)+3, ...)
  local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)
local r2, g2, b2 = select((segment*3)+4, ...), select((segment*3)+5, ...), select((segment*3)+6, ...)
   
   
  return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
  return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc

Revision as of 00:11, 15 April 2007

This page documents a user-defined function that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

User defined functions


Converts a percent value into a gradient from 2 or more RGB percent values (0.0-1.0). If more than 2 colors are passed, the gradient will be between the two colors which perc lies in an evenly divided range.

local function ColorGradient(perc, ...)
	local num = select("#", ...) / 3

	if perc >= 1 then
		local r, g, b = select(num-2, ...)
		return r, g, b
	elseif perc <= 0 then
		local r, g, b = ...
		return r, g, b
	end

	local segment, relperc = math.modf(perc*(num-1))
	local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)

	return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
end

Example

For this example we'll use 3 colors: red, yellow and green. For a percent value of 0.75 we'd expect to get back a color that's halfway between yellow and green. 0.5 would yield pure yellow, 0.25 would yield an orange.

local r,g,b = ColorGradient(0.75, 1,0,0, 1,1,0, 0,1,0)
-- r = 0.5, g = 1.0, b = 0.0