Great job, WillFa!
I think using HSL color space gives a more pleasing gradient then RGB color space.
HSL Color Theory Computation in Lua
http://sputnik.freewisdom.org/lib/colors/
Example Image (top gradient uses RGB & bottom gradient uses HSL)
http://i38.tinypic.com/29o0q4k.jpg
The following Lua script shows three power gauges using WindowGradient, WindowLine with RGB values and WindowLine with HSL values.
local function hsl_to_rgb(h, s, L) -- h = Hue
local h = h/360 -- s = Saturation
local m1, m2 -- L = Lightness
if L<=0.5 then
m2 = L*(s+1) -- hsl_to_rgb(120, 1, 0.5) = 0x00FF00
else -- hsl_to_rgb( 0, 1, 0.5) = 0x0000FF
m2 = L+s-L*s
end -- ColourNameToRGB("lime") = 0x00FF00
m1 = L*2-m2 -- ColourNameToRGB("red") = 0x0000FF
local function _h2rgb(m1, m2, h)
if h<0 then h = h+1 end
if h>1 then h = h-1 end
if h*6<1 then
return m1+(m2-m1)*h*6
elseif h*2<1 then
return m2
elseif h*3<2 then
return m1+(m2-m1)*(2/3-h)*6
else
return m1
end
end
return math.floor(255*_h2rgb(m1, m2, h+1/3))*16^0+
math.floor(255*_h2rgb(m1, m2, h ))*16^2+
math.floor(255*_h2rgb(m1, m2, h-1/3))*16^4
end
local width = 800
WindowCreate("test", -- miniwindow name
0, 0, -- left, top (ignore)
width, 350, -- width, height
12, -- position (centre all)
8, -- flags (ignore mouse)
ColourNameToRGB("black")) -- background colour
WindowShow("test", true) -- show miniwindow
WindowGradient("test", -- miniwindow name
0, 0, width-1, 100-1, -- left, top, right, bottom
ColourNameToRGB("lime"), -- start colour (0x00FF00)
ColourNameToRGB("red"), -- end colour (0x0000FF)
1) -- mode (horizontal)
for n = 0, width-1 do
local r, g, rgb
r = math.floor(255*n/(width-1))
g = math.floor(255*(width-1-n)/(width-1))
rgb = r*16^0+g*16^2 -- rgb colour space
WindowLine("test", -- miniwindow name
n, 125, n, 225-1, -- left, top, right, bottom
rgb, 0, 1) -- pen colour, pen style (solid), pen width
rgb = hsl_to_rgb(120-120*n/(width-1), 1, 0.5) -- hsl colour space
WindowLine("test", -- miniwindow name
n, 250, n, 350-1, -- left, top, right, bottom
rgb, 0, 1) -- pen colour, pen style (solid), pen width
end
Redraw()
|