luci-app-statistics: add options for graph series sorting and color
[oweals/luci.git] / applications / luci-app-statistics / luasrc / statistics / rrdtool / colors.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.statistics.rrdtool.colors", package.seeall)
5
6 local util = require("luci.util")
7
8
9 Instance = util.class()
10
11 function Instance.from_string( self, s )
12         return {
13                 tonumber(s:sub(1,2), 16),
14                 tonumber(s:sub(3,4), 16),
15                 tonumber(s:sub(5,6), 16)
16         }
17 end
18
19 function Instance.to_string( self, c )
20         return string.format(
21                 "%02x%02x%02x",
22                 math.floor(c[1]),
23                 math.floor(c[2]),
24                 math.floor(c[3])
25         )
26 end
27
28 function Instance.defined( self, i )
29         local t = {
30                 {230, 25, 75},
31                 {245, 130, 48},
32                 {255, 225, 25},
33                 {60, 180, 75},
34                 {70, 240, 240},
35                 {0, 130, 200},
36                 {0, 0, 128},
37                 {170, 110, 40}
38         }
39         return string.format(
40                 "%02x%02x%02x",
41                 t[(i-1) % #t + 1][1], t[(i-1) % #t +1][2], t[(i-1) % #t + 1][3] )
42 end
43
44 function Instance.random( self )
45         local r   = math.random(255)
46         local g   = math.random(255)
47         local min = 0
48         local max = 255
49
50         if ( r + g ) < 255 then
51                 min = 255 - r - g
52         else
53                 max = 511 - r - g
54         end
55
56         local b = min + math.floor( math.random() * ( max - min ) )
57
58         return { r, g, b }
59 end
60
61 function Instance.faded( self, fg, opts )
62         opts = opts or {}
63         opts.background = opts.background or { 255, 255, 255 }
64         opts.alpha      = opts.alpha      or 0.25
65
66         if type(opts.background) == "string" then
67                 opts.background = _string_to_color(opts.background)
68         end
69
70         local bg = opts.background
71
72         return {
73                 ( opts.alpha * fg[1] ) + ( ( 1.0 - opts.alpha ) * bg[1] ),
74                 ( opts.alpha * fg[2] ) + ( ( 1.0 - opts.alpha ) * bg[2] ),
75                 ( opts.alpha * fg[3] ) + ( ( 1.0 - opts.alpha ) * bg[3] )
76         }
77 end