Gradient
From awesome
Contents |
[edit] Introduction
Our eyes can process graphical data much faster then numerical. The following two functions allow you to convert numerical data into a hexadecimal colorgradient given three variables {min, val, max} with the relation: min <= val <= max. The actual color gradient looks like this:
[edit] Gradient
The following function converts a value between min, max into a hexadecimal color.
function gradient(min, max, val)
if (val > max) then val = max end
if (val < min) then val = min end
local v = val - min
local d = (max - min) * 0.5
local r, g
if (v <= d) then
r = (255 * v) / d
g = 255
else
r = 255
g = 255 - (255 * (v-d)) / (max - min - d)
end
return "#"..dec2hex(tostring(r))..dec2hex(tostring(g)).."00"
end
[edit] dec2hex
The dec2hex function maps decimals to hexadecimals.
function dec2hex(s)
s = string.format("%02x", s)
return s
end
[edit] Example
The following example shows my cores temperature and usage in a color gradient using wicked.
-- {{{ CPU USAGE
cputextwidget = widget({
type = 'textbox',
name = 'cputextwidget',
align = 'right'
})
wicked.register(cputextwidget, 'cpu', function(widget, args)
cpuinfo = title("CPU")
cpuinfo = color(cpuinfo..'['..args[2]..'% '..cputemp(0)..'°C] ', gradient(0, 100, tonumber(args[2])))
cpuinfo = color(cpuinfo..'['..args[3]..'% '..cputemp(1)..'°C] ', gradient(0, 100, tonumber(args[3])))
cpuinfo = color(cpuinfo..'['..args[4]..'% '..cputemp(2)..'°C] ', gradient(0, 100, tonumber(args[4])))
cpuinfo = color(cpuinfo..'['..args[5]..'% '..cputemp(3)..'°C] ', gradient(0, 100, tonumber(args[5])))
return cpuinfo
end, 1, nil, 2)
-- }}}
[edit] Graph
This is what the actual function does on numerical data from [0..100] for the red and green channels (blue remains 0).


