summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/color.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/color.lua')
-rw-r--r--apps/plugins/lua/include_lua/color.lua117
1 files changed, 117 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/color.lua b/apps/plugins/lua/include_lua/color.lua
new file mode 100644
index 0000000000..ed2e4f865e
--- /dev/null
+++ b/apps/plugins/lua/include_lua/color.lua
@@ -0,0 +1,117 @@
1--[[ Lua Color functions
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23
24--[[ Exposed Functions
25
26 _clr.inc
27 _clr.set
28
29-- Exposed Constants
30 IS_COLOR_TARGET
31
32]]
33if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
34
35IS_COLOR_TARGET = false
36-- Only true when we're on a color target, i.e. when LCD_RGBPACK is available
37if rb.lcd_rgbpack ~= _NIL then
38 IS_COLOR_TARGET = true
39end
40
41local _clr = {} do
42
43 -- Internal Constants
44 local _NIL = nil -- _NIL placeholder
45
46 local maxstate = (bit.lshift(1, rb.LCD_DEPTH) - 1)
47
48 if rb.LCD_DEPTH > 24 then -- no alpha channels
49 maxstate = (bit.lshift(1, 24) - 1)
50 end
51
52 local function init(v)
53 return v or 0
54 end
55
56 -- clamps value to >= min and <= max rolls over to opposite
57 local function clamp_roll(val, min, max)
58 if min > max then
59 local swap = min
60 min, max = max, swap
61 end
62
63 if val < min then
64 val = max
65 elseif val > max then
66 val = min
67 end
68
69 return val
70 end
71
72 -- sets color -- monochrome / greyscale use 'set' -- color targets 'r,b,g'
73 -- on monochrome/ greyscale targets:
74 -- '-1' sets the highest 'color' state & 0 is the minimum 'color' state
75 local function clrset(set, r, g, b)
76 local color = set or 0
77
78 if IS_COLOR_TARGET then
79 if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then
80 r, g, b = init(r), init(g), init(b)
81 color = rb.lcd_rgbpack(r, g, b)
82 end
83 end
84
85 return clamp_roll(color, 0, maxstate)
86 end -- clrset
87
88 -- de/increments current color by 'inc' -- optionally color targets by 'r,g,b'
89 local function clrinc(current, inc, r, g, b)
90 local color = 0
91 current = current or color
92 inc = inc or 1
93
94 if IS_COLOR_TARGET then
95 local ru, gu, bu = rb.lcd_rgbunpack(current);
96 if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then
97 r, g, b = init(r), init(g), init(b)
98 ru = ru + r; gu = gu + g; bu = bu + b
99 color = rb.lcd_rgbpack(ru, gu, bu)
100 else
101 ru = ru + inc; gu = gu + inc; bu = bu + inc
102 color = rb.lcd_rgbpack(ru, gu, bu)
103 end
104 else
105 color = current + inc
106 end
107
108 return clamp_roll(color, 0, maxstate)
109 end -- clrinc
110
111 -- expose functions to the outside through _clr table
112 _clr.set = clrset
113 _clr.inc = clrinc
114end -- color functions
115
116return _clr
117