libs/core: Remove deprecated luci.bits which got replaced by bitlib
[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
20
21 Instance = luci.util.class()
22
23 function Instance.from_string( self, s )
24 return {
25 tonumber(s:sub(1,2), 16),
26 tonumber(s:sub(3,4), 16),
27 tonumber(s:sub(5,6), 16)
28 }
29 end
30
31 function Instance.to_string( self, c )
32 return string.format(
33 "%02x%02x%02x",
34 math.floor(c[1]),
35 math.floor(c[2]),
36 math.floor(c[3])
37 )
38 end
39
40 function Instance.random( self )
41 local r = math.random(255)
42 local g = math.random(255)
43 local min = 0
44 local max = 255
45
46 if ( r + g ) < 255 then
47 min = 255 - r - g
48 else
49 max = 511 - r - g
50 end
51
52 local b = min + math.floor( math.random() * ( max - min ) )
53
54 return { r, g, b }
55 end
56
57 function Instance.faded( self, fg, opts )
58 opts = opts or {}
59 opts.background = opts.background or { 255, 255, 255 }
60 opts.alpha = opts.alpha or 0.25
61
62 if type(opts.background) == "string" then
63 opts.background = _string_to_color(opts.background)
64 end
65
66 local bg = opts.background
67
68 return {
69 ( opts.alpha * fg[1] ) + ( ( 1.0 - opts.alpha ) * bg[1] ),
70 ( opts.alpha * fg[2] ) + ( ( 1.0 - opts.alpha ) * bg[2] ),
71 ( opts.alpha * fg[3] ) + ( ( 1.0 - opts.alpha ) * bg[3] )
72 }
73 end