annotate color_convert.py @ 26:33b3eb24506e

wled (single bulb) support. more lights
author drewp@bigasterisk.com
date Sat, 03 Feb 2024 20:56:27 -0800
parents ecbbf76318bb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
1 import colorsys
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
2 import logging
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
3 from dataclasses import dataclass
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
4
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
5 from color import Color
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
6
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
7 log = logging.getLogger('conv')
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
8
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
9
24
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
10 def inverseLerp(a, b, val) -> float:
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
11 return (val - a) / (b - a)
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
12
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
13
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
14 def clamped(x) -> float:
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
15 return min(1, max(0, x))
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
16
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
17
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
18 def simpleWhiteVal(r, g, b) -> float:
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
19 h, s, v = colorsys.rgb_to_hsv(r, g, b)
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
20 return (1 - s)**2 * clamped(inverseLerp(.5, 1, v))**2
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
21
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
22
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
23 @dataclass(frozen=True)
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
24 class DeviceColor:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
25 """neutral representation of the adjusted color that we send to a device"""
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
26 r: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
27 g: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
28 b: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
29 w: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
30 x: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
31 y: float = 0
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
32 cw: float = 0
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
33 ww: float = 0
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
34 brightness: float = 0
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
35 temp: float = 0
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
36
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
37 def summary(self) -> dict:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
38 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0])
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
39
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
40
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
41 # fix this to send what z2m likes
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
42 def zbConv(c: Color) -> DeviceColor:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
43 return DeviceColor(r=c.r, g=c.g, b=c.b, brightness=max(c.r, c.g, c.b))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
44
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
45
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
46 def ikeaWhiteConv(c: Color) -> DeviceColor:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
47 return DeviceColor(brightness=max(c.r, c.g, c.b), temp=454)
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
48
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
49
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
50 def oneWhiteConv(c: Color) -> DeviceColor:
24
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
51 return DeviceColor(r=c.r, g=c.g, b=c.b, w=simpleWhiteVal(c.r, c.g, c.b))
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
52
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
53
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
54 def brightnessConv(c: Color) -> DeviceColor:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
55 return DeviceColor(brightness=max(c.r, c.g, c.b))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
56
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
57
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
58 def twoWhitesConv(c: Color) -> DeviceColor:
24
ecbbf76318bb simpleWhiteVal
drewp@bigasterisk.com
parents: 21
diff changeset
59 return DeviceColor(r=c.r, g=c.g, b=c.b, cw=simpleWhiteVal(c.r, c.g, c.b), ww=simpleWhiteVal(c.r, c.g, c.b))
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
60
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
61
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
62 def relayConv(c: Color) -> DeviceColor:
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
63 return DeviceColor(brightness=1 if (max(c.r, c.g, c.b) > 0) else 0)