Mercurial > code > home > repos > light-bridge
changeset 24:ecbbf76318bb
simpleWhiteVal
author | drewp@bigasterisk.com |
---|---|
date | Fri, 02 Feb 2024 20:51:48 -0800 |
parents | 7d9a056e29fe |
children | cee43f550577 |
files | color_convert.py |
diffstat | 1 files changed, 17 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/color_convert.py Mon Jan 29 23:41:27 2024 -0800 +++ b/color_convert.py Fri Feb 02 20:51:48 2024 -0800 @@ -1,3 +1,4 @@ +import colorsys import logging from dataclasses import dataclass @@ -6,6 +7,19 @@ log = logging.getLogger('conv') +def inverseLerp(a, b, val) -> float: + return (val - a) / (b - a) + + +def clamped(x) -> float: + return min(1, max(0, x)) + + +def simpleWhiteVal(r, g, b) -> float: + h, s, v = colorsys.rgb_to_hsv(r, g, b) + return (1 - s)**2 * clamped(inverseLerp(.5, 1, v))**2 + + @dataclass(frozen=True) class DeviceColor: """neutral representation of the adjusted color that we send to a device""" @@ -34,14 +48,15 @@ def oneWhiteConv(c: Color) -> DeviceColor: - return DeviceColor(r=c.r, g=c.g, b=c.b, w=max(c.r, c.g, c.b)) + return DeviceColor(r=c.r, g=c.g, b=c.b, w=simpleWhiteVal(c.r, c.g, c.b)) + def brightnessConv(c: Color) -> DeviceColor: return DeviceColor(brightness=max(c.r, c.g, c.b)) def twoWhitesConv(c: Color) -> DeviceColor: - return DeviceColor(r=c.r, g=c.g, b=c.b, cw=max(c.r, c.g, c.b), ww=0) + 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)) def relayConv(c: Color) -> DeviceColor: