annotate color_convert.py @ 20:24a574108365

more protocols; bugs in setColor
author drewp@bigasterisk.com
date Mon, 29 Jan 2024 11:52:43 -0800
parents 61d4ccecfed8
children b8201490c731
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
1 import logging
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
2 from dataclasses import dataclass
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
3
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
4 from color import Color
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
5
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
6 log = logging.getLogger('conv')
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
7
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
8
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
9 @dataclass(frozen=True)
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
10 class DeviceColor:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
11 """neutral representation of the adjusted color that we send to a device"""
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
12 r: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
13 g: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
14 b: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
15 w: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
16 x: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
17 y: float = 0
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
18 cw: float = 0
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
19 ww: float = 0
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
20 brightness: float = 0
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
21
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
22 def summary(self) -> dict:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
23 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
24
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
25 # fix this to send what z2m likes
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
26 def zbConv(c: Color) -> DeviceColor:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
27 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
28
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
29 def oneWhiteConv(c: Color) -> DeviceColor:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
30 return DeviceColor(r=c.r, g=c.g, b=c.b, w=max(c.r, c.g, c.b))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
31
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
32 def twoWhitesConv(c: Color) -> DeviceColor:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
33 return DeviceColor(r=c.r, g=c.g, b=c.b, cw=max(c.r, c.g, c.b))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
34
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
35 def relayConv(c: Color) -> DeviceColor:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
36 return DeviceColor(brightness=1 if (max(c.r, c.g, c.b) > 0) else 0)