Skip to content

Commit

Permalink
Add brand colours to tidal.py
Browse files Browse the repository at this point in the history
This adds all the brand colours to the tidal interface, including a utility to adjust RGB to a color565 entry that looks a closer match perceptually. This also moves the HSV utilities out of the torch and into a utility module mimicing colorsys.
  • Loading branch information
MatthewWilkes committed May 30, 2022
1 parent 6b46f54 commit 8836962
Showing 4 changed files with 69 additions and 50 deletions.
47 changes: 47 additions & 0 deletions modules/colorsys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

# Ported from https://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
def hsv_to_rgb(h, s, v):
i = int(h * 6)
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
mod = i % 6
if mod == 0:
r = v; g = t; b = p
elif mod == 1:
r = q; g = v; b = p
elif mod == 2:
r = p; g = v; b = t
elif mod == 3:
r = p; g = q; b = v
elif mod == 4:
r = t; g = p; b = v
elif mod == 5:
r = v; g = p; b = q

return (int(r * 255), int(g * 255), int(b * 255))

def rgb_to_hsv(r, g, b):
r = r / 255
g = g / 255
b = b / 255
max_val = max(r, g, b)
min_val = min(r, g, b)
v = max_val
d = max_val - min_val
s = 0 if max_val == 0 else d / max_val

if max_val == min_val:
h = 0 # achromatic
else:
if max_val == r:
h = (g - b) / d + (6 if g < b else 0)
elif max_val == g:
h = (b - r) / d + 2
elif max_val == b:
h = (r - g) / d + 4
h /= 6

return (h, s, v)

4 changes: 2 additions & 2 deletions modules/textwindow.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class TextWindow:
RIGHT_ARROW = '\x1A'
LEFT_ARROW = '\x1B'

DEFAULT_BG = tidal.color565(0, 0, 0x60)
DEFAULT_BG = tidal.BRAND_NAVY
DEFAULT_FG = tidal.WHITE

def __init__(self, bg=None, fg=None, title=None, font=None, buttons=None):
@@ -163,7 +163,7 @@ def flow_lines(self, text, font=None):
class Menu(TextWindow):

DEFAULT_FOCUS_FG = tidal.BLACK
DEFAULT_FOCUS_BG = tidal.CYAN
DEFAULT_FOCUS_BG = tidal.BRAND_CYAN

def __init__(self, bg, fg, focus_bg, focus_fg, title, choices, font=None, buttons=None):
super().__init__(bg, fg, title, font, buttons)
18 changes: 18 additions & 0 deletions modules/tidal.py
Original file line number Diff line number Diff line change
@@ -3,12 +3,30 @@
from machine import Pin
from machine import SPI
from neopixel import NeoPixel
from colorsys import hsv_to_rgb, rgb_to_hsv
import st7789
from st7789 import BLACK, BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW, WHITE, color565

import _tidal_usb as usb
import tidal_helpers

# Boost the saturation massively to match better on the LCD
def perceptual_adjust(r, g, b):
h, s, v = rgb_to_hsv(r, g, b)
s *= 1.6
s = min(s, 1.0)
r, g, b = hsv_to_rgb(h, s, v)
return color565(r, g, b)

BRAND_NAVY = perceptual_adjust(21, 23, 53)
BRAND_MID_BLUE = perceptual_adjust(18, 63, 139)
BRAND_CYAN = perceptual_adjust(144, 204, 214)
BRAND_YELLOW = perceptual_adjust(242, 222, 27)
BRAND_ORANGE = perceptual_adjust(246, 163, 24)
BRAND_PINK = perceptual_adjust(228, 20, 126)
ADDITIONAL_PURPLE = perceptual_adjust(118, 34, 114)
ADDITIONAL_RED = perceptual_adjust(230, 43, 39)
ADDITIONAL_DEEP_ORANGE = perceptual_adjust(235, 107, 16)

"""
NOTE: If you are using the automatic lightsleep (on by default) you should never
50 changes: 2 additions & 48 deletions modules/torch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from tidal import *
from colorsys import hsv_to_rgb
from app import TextApp

BRIGHTNESS_STEP = 0.8
@@ -27,53 +28,6 @@
(MORSE_WORD, HUE_WHITE, 0)])
]

# Ported from https://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
def hsvToRgb(h, s, v):
i = int(h * 6)
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
mod = i % 6
if mod == 0:
r = v; g = t; b = p
elif mod == 1:
r = q; g = v; b = p
elif mod == 2:
r = p; g = v; b = t
elif mod == 3:
r = p; g = q; b = v
elif mod == 4:
r = t; g = p; b = v
elif mod == 5:
r = v; g = p; b = q

return (int(r * 255), int(g * 255), int(b * 255))

def rgbToHsv(r, g, b):
r = r / 255
g = g / 255
b = b / 255
max_val = max(r, g, b)
min_val = min(r, g, b)
v = max_val
d = max_val - min_val
s = 0 if max_val == 0 else d / max_val

if max_val == min_val:
h = 0 # achromatic
else:
if max_val == r:
h = (g - b) / d + (6 if g < b else 0)
elif max_val == g:
h = (b - r) / d + 2
elif max_val == b:
h = (r - g) / d + 4
h /= 6

return (h, s, v)


class Torch(TextApp):

TITLE = "Torch"
@@ -133,7 +87,7 @@ def update_led(self):
# print("LED h={} s={} v={}".format(hue, saturation, led_v))
led_power_on(self.state)
if self.state:
self.led[0] = hsvToRgb(hue, saturation, self.led_v)
self.led[0] = hsv_to_rgb(hue, saturation, self.led_v)
else:
self.led[0] = (0, 0, 0)
self.led.write()

0 comments on commit 8836962

Please sign in to comment.