1 --[[
3 Luci statistics - diagram color helper class
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
12 \$Id\$
14 ]]--
16 module("luci.statistics.rrdtool.colors", package.seeall)
18 require("luci.util")
19 require("luci.bits")
22 Instance = luci.util.class()
24 function Instance.from_string( self, s )
25 return {
26 luci.bits.Hex2Dec(s:sub(1,2)),
27 luci.bits.Hex2Dec(s:sub(3,4)),
28 luci.bits.Hex2Dec(s:sub(5,6))
29 }
30 end
32 function Instance.to_string( self, c )
33 return string.format(
34 "%02x%02x%02x",
35 math.floor(c[1]),
36 math.floor(c[2]),
37 math.floor(c[3])
38 )
39 end
41 function Instance.random( self )
42 local r = math.random(255)
43 local g = math.random(255)
44 local min = 0
45 local max = 255
47 if ( r + g ) < 255 then
48 min = 255 - r - g
49 else
50 max = 511 - r - g
51 end
53 local b = min + math.floor( math.random() * ( max - min ) )
55 return { r, g, b }
56 end
58 function Instance.faded( self, fg, opts )
59 opts = opts or {}
60 opts.background = opts.background or { 255, 255, 255 }
61 opts.alpha = opts.alpha or 0.25
63 if type(opts.background) == "string" then
64 opts.background = _string_to_color(opts.background)
65 end
67 local bg = opts.background
69 return {
70 ( opts.alpha * fg[1] ) + ( ( 1.0 - opts.alpha ) * bg[1] ),
71 ( opts.alpha * fg[2] ) + ( ( 1.0 - opts.alpha ) * bg[2] ),
72 ( opts.alpha * fg[3] ) + ( ( 1.0 - opts.alpha ) * bg[3] )
73 }
74 end