summaryrefslogtreecommitdiff
path: root/python/colours.py
blob: 5e66696ec9247d88fab6e04225e7221f19efb5e0 (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
# Colours - library for managing the colours on RGB LEDs
# Copyright 2025 Elena Grandi

# This is the (Circuit|Micro)Python version of the library.

# Colours is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 of 
# the License, or (at your option) any later version.
#
# Colours 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Colours.  If not, see <http://www.gnu.org/licenses/>.

def hsv2rgb(h, s, v):
    h = h % 360
    if s == 0:
        return (v, v, v)
    f = 256*(h%60)//60
    p = v*(256-s)//256
    q = v*(256-f*s//256)//256
    t = v*(256-s*(256-f)//256)//256
    hue_zone = (h//60) % 6
    if hue_zone == 0:
        return (v, t, p)
    elif hue_zone == 1:
        return (q, v, p)
    elif hue_zone == 2:
        return (p, v, t)
    elif hue_zone == 3:
        return (p, q, v)
    elif hue_zone == 4:
        return (t, p, v)
    elif hue_zone == 5:
        return (v, p, q)