cdd6f71057e2069c256000b01e287eebbe37e031
[project/luci.git] / applications / luci-statistics / src / statistics / rrdtool / colors.lua
1 module("luci.statistics.rrdtool.colors", package.seeall)
2
3 require("luci.util")
4 require("luci.bits")
5
6
7 Instance = luci.util.class()
8
9 function Instance.from_string( self, s )
10 return {
11 luci.bits.Hex2Dec(s:sub(1,2)),
12 luci.bits.Hex2Dec(s:sub(3,4)),
13 luci.bits.Hex2Dec(s:sub(5,6))
14 }
15 end
16
17 function Instance.to_string( self, c )
18 return string.format(
19 "%02x%02x%02x",
20 math.floor(c[1]),
21 math.floor(c[2]),
22 math.floor(c[3])
23 )
24 end
25
26 function Instance.random( self )
27 local r = math.random(255)
28 local g = math.random(255)
29 local min = 0
30 local max = 255
31
32 if ( r + g ) < 255 then
33 min = 255 - r - g
34 else
35 max = 511 - r - g
36 end
37
38 local b = min + math.floor( math.random() * ( max - min ) )
39
40 return { r, g, b }
41 end
42
43 function Instance.faded( self, fg, opts )
44 opts = opts or {}
45 opts.background = opts.background or { 255, 255, 255 }
46 opts.alpha = opts.alpha or 0.25
47
48 if type(opts.background) == "string" then
49 opts.background = _string_to_color(opts.background)
50 end
51
52 local bg = opts.background
53
54 return {
55 ( opts.alpha * fg[1] ) + ( ( 1.0 - opts.alpha ) * bg[1] ),
56 ( opts.alpha * fg[2] ) + ( ( 1.0 - opts.alpha ) * bg[2] ),
57 ( opts.alpha * fg[3] ) + ( ( 1.0 - opts.alpha ) * bg[3] )
58 }
59 end