diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/color.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/color.lua | 112 |
1 files changed, 112 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..267728e072 --- /dev/null +++ b/apps/plugins/lua/include_lua/color.lua | |||
@@ -0,0 +1,112 @@ | |||
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 | ]] | ||
33 | if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end | ||
34 | |||
35 | IS_COLOR_TARGET = false | ||
36 | -- Only true when we're on a color target, i.e. when LCD_RGBPACK is available | ||
37 | if rb.lcd_rgbpack ~= _NIL then | ||
38 | IS_COLOR_TARGET = true | ||
39 | end | ||
40 | |||
41 | local _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 | local function init(v) | ||
49 | return v or 0 | ||
50 | end | ||
51 | |||
52 | -- clamps value to >= min and <= max rolls over to opposite | ||
53 | local function clamp_roll(val, min, max) | ||
54 | if min > max then | ||
55 | local swap = min | ||
56 | min, max = max, swap | ||
57 | end | ||
58 | |||
59 | if val < min then | ||
60 | val = max | ||
61 | elseif val > max then | ||
62 | val = min | ||
63 | end | ||
64 | |||
65 | return val | ||
66 | end | ||
67 | |||
68 | -- sets color -- monochrome / greyscale use 'set' -- color targets 'r,b,g' | ||
69 | -- on monochrome/ greyscale targets: | ||
70 | -- '-1' sets the highest 'color' state & 0 is the minimum 'color' state | ||
71 | local function clrset(set, r, g, b) | ||
72 | local color = set or 0 | ||
73 | |||
74 | if IS_COLOR_TARGET then | ||
75 | if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then | ||
76 | r, g, b = init(r), init(g), init(b) | ||
77 | color = rb.lcd_rgbpack(r, g, b) | ||
78 | end | ||
79 | end | ||
80 | |||
81 | return clamp_roll(color, 0, maxstate) | ||
82 | end -- clrset | ||
83 | |||
84 | -- de/increments current color by 'inc' -- optionally color targets by 'r,g,b' | ||
85 | local function clrinc(current, inc, r, g, b) | ||
86 | local color = 0 | ||
87 | current = current or color | ||
88 | inc = inc or 1 | ||
89 | |||
90 | if IS_COLOR_TARGET then | ||
91 | local ru, gu, bu = rb.lcd_rgbunpack(current); | ||
92 | if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then | ||
93 | r, g, b = init(r), init(g), init(b) | ||
94 | ru = ru + r; gu = gu + g; bu = bu + b | ||
95 | color = rb.lcd_rgbpack(ru, gu, bu) | ||
96 | else | ||
97 | ru = ru + inc; gu = gu + inc; bu = bu + inc | ||
98 | color = rb.lcd_rgbpack(ru, gu, bu) | ||
99 | end | ||
100 | else | ||
101 | color = current + inc | ||
102 | end | ||
103 | |||
104 | return clamp_roll(color, 0, maxstate) | ||
105 | end -- clrinc | ||
106 | |||
107 | -- expose functions to the outside through _clr table | ||
108 | _clr.set = clrset | ||
109 | _clr.inc = clrinc | ||
110 | end -- color functions | ||
111 | |||
112 | return _clr | ||