8f28524aa68a29298a49094a40ff480315a05cbe
[project/luci.git] / applications / luci-statistics / luasrc / statistics / rrdtool / colors.lua
1 --[[
2
3 Luci statistics - diagram color helper class
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.statistics.rrdtool.colors", package.seeall)
17
18 require("luci.util")
19 require("luci.bits")
20
21
22 Instance = luci.util.class()
23
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
31
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
40
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
46
47 if ( r + g ) < 255 then
48 min = 255 - r - g
49 else
50 max = 511 - r - g
51 end
52
53 local b = min + math.floor( math.random() * ( max - min ) )
54
55 return { r, g, b }
56 end
57
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
62
63 if type(opts.background) == "string" then
64 opts.background = _string_to_color(opts.background)
65 end
66
67 local bg = opts.background
68
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