summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua/include_lua/color.lua
blob: 7266c090f3ed46f6c509b62d21f395d88b80f298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--[[ Lua Color functions
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2017 William Wilgus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
]]

--[[ Exposed Functions

    _clr.inc
    _clr.set

-- Exposed Constants
    IS_COLOR_TARGET

]]
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end

IS_COLOR_TARGET = false
-- Only true when we're on a color target, i.e. when LCD_RGBPACK is available
if rb.lcd_rgbpack ~= _NIL then
    IS_COLOR_TARGET = true
end

local _clr = {} do

    -- Internal Constants
    local _NIL = nil -- _NIL placeholder

    local maxstate = (bit.lshift(1, rb.LCD_DEPTH) - 1)

    if rb.LCD_DEPTH > 24 then -- no alpha channels
        maxstate = (bit.lshift(1, 24) - 1)
    end

   -- clamps value to >= min and <= max rolls over to opposite
    local function clamp_roll(val, min, max)
        -- Warning doesn't check if min < max
        if val < min then
            val = max
        elseif val > max then
            val = min
        end

        return val
    end

    -- sets color -- monochrome / greyscale use 'set' -- color targets 'r,b,g'
    -- on monochrome/ greyscale targets:
    -- '-1' sets the highest 'color' state & 0 is the minimum 'color' state
    _clr.set = function(set, r, g, b)
        local color = set or 0

        if IS_COLOR_TARGET then
            if (r or g or b) then
                r, g, b = (r or 0), (g or 0), (b or 0)
                color = rb.lcd_rgbpack(r, g, b)
            end
        end

        return clamp_roll(color, 0, maxstate)
    end -- clrset

    -- de/increments current color by 'inc' -- optionally color targets by 'r,g,b'
    _clr.inc = function(current, inc, r, g, b)
        local color = 0
        current = current or color
        inc = inc or 1

        if IS_COLOR_TARGET then
            local ru, gu, bu = rb.lcd_rgbunpack(current);
            if (r or g or b) then
                r, g, b = (r or 0), (g or 0), (b or 0)
                ru = ru + r; gu = gu + g; bu = bu + b
            else
                ru = ru + inc; gu = gu + inc; bu = bu + inc
            end

            color = rb.lcd_rgbpack(ru, gu, bu)
        else
            color = current + inc
        end

        return clamp_roll(color, 0, maxstate)
    end -- clrinc

end -- color functions

return _clr