aboutsummaryrefslogtreecommitdiffstats
path: root/remote/libsimplepwm/gui/coloradjwidget.py
blob: 7be223b7e438b9f9d105e4057ccb594b1d77e6ad (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
107
108
109
110
#
# Simple PWM controller
# Remote control tool
#
# Copyright (c) 2018-2021 Michael Buesch <m@bues.ch>
#
# 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 program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

__all__ = [
    "ColorAdjWidget",
]

from libsimplepwm.gui.blocker import *
from libsimplepwm.gui.gtk_import import *
import colorsys

def rgb2hls(r, g, b):
    h, l, s = colorsys.rgb_to_hls(r / 100.0, g / 100.0, b / 100.0)
    return h * 360.0, l * 100.0, s * 100.0

def hls2rgb(h, l, s):
    r, g, b = colorsys.hls_to_rgb(h / 360.0, l / 100.0, s / 100.0)
    return round(r * 100.0), round(g * 100.0), round(b * 100.0)

class ColorAdjWidget(Gtk.Grid):
    @GObject.Signal
    def colorChanged(self,
                     mode: str,
                     r: int, g: int, b: int,
                     h: int, l: int, s: int):
        pass

    def __init__(self):
        super().__init__()

        self.__changeBlocked = Blocker()

        self.set_column_homogeneous(True)

        label = Gtk.Label("RGB:")
        self.__scaleR = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 1)
        self.__scaleG = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 1)
        self.__scaleB = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 1)
        self.attach(label, 0, 0, 1, 1)
        self.attach(self.__scaleR, 1, 0, 1, 1)
        self.attach(self.__scaleG, 2, 0, 1, 1)
        self.attach(self.__scaleB, 3, 0, 1, 1)

        label = Gtk.Label("HLS:")
        self.__scaleH = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 360, 1)
        self.__scaleL = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 1)
        self.__scaleS = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 0, 100, 1)
        self.attach(label, 0, 1, 1, 1)
        self.attach(self.__scaleH, 1, 1, 1, 1)
        self.attach(self.__scaleL, 2, 1, 1, 1)
        self.attach(self.__scaleS, 3, 1, 1, 1)

        self.__scaleR.connect("value_changed", self.__on_scaleRGB_changed)
        self.__scaleG.connect("value_changed", self.__on_scaleRGB_changed)
        self.__scaleB.connect("value_changed", self.__on_scaleRGB_changed)

        self.__scaleH.connect("value_changed", self.__on_scaleHLS_changed)
        self.__scaleL.connect("value_changed", self.__on_scaleHLS_changed)
        self.__scaleS.connect("value_changed", self.__on_scaleHLS_changed)

    def __on_scaleRGB_changed(self, _):
        if self.__changeBlocked:
            return
        with self.__changeBlocked:
            r = self.__scaleR.get_value()
            g = self.__scaleG.get_value()
            b = self.__scaleB.get_value()
            h, l, s = rgb2hls(r, g, b)
            self.__scaleH.set_value(h)
            self.__scaleL.set_value(l)
            self.__scaleS.set_value(s)
            self.emit("colorChanged", "RGB", r, g, b, h, l, s)

    def __on_scaleHLS_changed(self, _):
        if self.__changeBlocked:
            return
        with self.__changeBlocked:
            h = self.__scaleH.get_value()
            l = self.__scaleL.get_value()
            s = self.__scaleS.get_value()
            r, g, b = hls2rgb(h, l, s)
            self.__scaleR.set_value(r)
            self.__scaleG.set_value(g)
            self.__scaleB.set_value(b)
            self.emit("colorChanged", "HLS", r, g, b, h, l, s)

    def setRGB(self, r, g, b):
        self.__scaleR.set_value(r)
        self.__scaleG.set_value(g)
        self.__scaleB.set_value(b)

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface