| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import ast |
| 4 |
import colorsys |
| 5 |
import contextlib |
| 6 |
import datetime |
| 7 |
from collections.abc import Iterable |
| 8 |
from typing import NamedTuple, cast |
| 9 |
|
| 10 |
import webcolors |
| 11 |
|
| 12 |
from .const import MAX_TEMP, MIN_TEMP |
| 13 |
|
| 14 |
MAX_MIN_TEMP_DIFF = MAX_TEMP - MIN_TEMP |
| 15 |
|
| 16 |
|
| 17 |
class WhiteLevels(NamedTuple): |
| 18 |
"""White level for a color.""" |
| 19 |
|
| 20 |
warm_white: int |
| 21 |
cool_white: int |
| 22 |
|
| 23 |
|
| 24 |
class TemperatureBrightness(NamedTuple): |
| 25 |
"""Temperature and brightness for a color.""" |
| 26 |
|
| 27 |
temperature: int |
| 28 |
brightness: int |
| 29 |
|
| 30 |
|
| 31 |
class utils: |
| 32 |
@staticmethod |
| 33 |
def color_object_to_tuple( |
| 34 |
color: tuple[int, ...] | str, |
| 35 |
) -> tuple[int, ...] | None: |
| 36 |
|
| 37 |
if isinstance(color, tuple) and len(color) in [3, 4, 5]: |
| 38 |
return color |
| 39 |
|
| 40 |
|
| 41 |
if not isinstance(color, str): |
| 42 |
return None |
| 43 |
color = color.strip() |
| 44 |
|
| 45 |
|
| 46 |
with contextlib.suppress(Exception): |
| 47 |
return cast("tuple[int, int, int]", webcolors.name_to_rgb(color)) |
| 48 |
|
| 49 |
|
| 50 |
with contextlib.suppress(Exception): |
| 51 |
return cast( |
| 52 |
"tuple[int, int, int]", |
| 53 |
webcolors.hex_to_rgb(webcolors.normalize_hex(color)), |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
with contextlib.suppress(Exception): |
| 58 |
val = ast.literal_eval(color) |
| 59 |
if type(val) is not tuple or len(val) not in [3, 4, 5]: |
| 60 |
raise Exception |
| 61 |
return val |
| 62 |
|
| 63 |
return None |
| 64 |
|
| 65 |
@staticmethod |
| 66 |
def color_tuple_to_string(rgb: tuple[int, int, int]) -> str: |
| 67 |
|
| 68 |
with contextlib.suppress(Exception): |
| 69 |
return cast("str", webcolors.rgb_to_name(rgb)) |
| 70 |
return str(rgb) |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
def get_color_names_list() -> list[str]: |
| 74 |
return sorted( |
| 75 |
{ |
| 76 |
*webcolors.names(webcolors.CSS2), |
| 77 |
*webcolors.names(webcolors.CSS21), |
| 78 |
*webcolors.names(webcolors.CSS3), |
| 79 |
*webcolors.names(webcolors.HTML4), |
| 80 |
} |
| 81 |
) |
| 82 |
|
| 83 |
@staticmethod |
| 84 |
def date_has_passed(dt: datetime.datetime) -> bool: |
| 85 |
return (dt - datetime.datetime.now()).total_seconds() < 0 |
| 86 |
|
| 87 |
@staticmethod |
| 88 |
def raw_state_to_dec(rx: Iterable[int]) -> str: |
| 89 |
raw_state_str = "" |
| 90 |
for _r in rx: |
| 91 |
raw_state_str += str(_r) + "," |
| 92 |
return raw_state_str |
| 93 |
|
| 94 |
max_delay = 0x1F |
| 95 |
|
| 96 |
@staticmethod |
| 97 |
def delayToSpeed(delay: int) -> int: |
| 98 |
|
| 99 |
|
| 100 |
delay = delay - 1 |
| 101 |
delay = max(0, min(utils.max_delay - 1, delay)) |
| 102 |
inv_speed = int((delay * 100) / (utils.max_delay - 1)) |
| 103 |
speed = 100 - inv_speed |
| 104 |
return speed |
| 105 |
|
| 106 |
@staticmethod |
| 107 |
def speedToDelay(speed: int) -> int: |
| 108 |
|
| 109 |
speed = max(0, min(100, speed)) |
| 110 |
inv_speed = 100 - speed |
| 111 |
delay = int((inv_speed * (utils.max_delay - 1)) / 100) |
| 112 |
|
| 113 |
delay = delay + 1 |
| 114 |
return delay |
| 115 |
|
| 116 |
@staticmethod |
| 117 |
def byteToPercent(byte: int) -> int: |
| 118 |
return int((max(0, min(255, byte)) * 100) / 255) |
| 119 |
|
| 120 |
@staticmethod |
| 121 |
def percentToByte(percent: int) -> int: |
| 122 |
return int((max(0, min(100, percent)) * 255) / 100) |
| 123 |
|
| 124 |
@staticmethod |
| 125 |
def A3WarmWhiteToByte(val: int) -> int: |
| 126 |
return round(((min(228, max(128, val)) - 128) * 255) / 100) |
| 127 |
|
| 128 |
|
| 129 |
def rgbwc_to_rgbcw( |
| 130 |
rgbwc_data: tuple[int, int, int, int, int], |
| 131 |
) -> tuple[int, int, int, int, int]: |
| 132 |
r, g, b, w, c = rgbwc_data |
| 133 |
return r, g, b, c, w |
| 134 |
|
| 135 |
|
| 136 |
def rgbcw_to_rgbwc( |
| 137 |
rgbcw_data: tuple[int, int, int, int, int], |
| 138 |
) -> tuple[int, int, int, int, int]: |
| 139 |
r, g, b, c, w = rgbcw_data |
| 140 |
return r, g, b, w, c |
| 141 |
|
| 142 |
|
| 143 |
def _adjust_brightness( |
| 144 |
current_brightness: int, |
| 145 |
new_brightness: int, |
| 146 |
color_brightness: int, |
| 147 |
cw_brightness: int, |
| 148 |
ww_brightness: int, |
| 149 |
) -> tuple[int, int, int]: |
| 150 |
if new_brightness < current_brightness: |
| 151 |
change_brightness_pct = ( |
| 152 |
current_brightness - new_brightness |
| 153 |
) / current_brightness |
| 154 |
ww_brightness = round(ww_brightness * (1 - change_brightness_pct)) |
| 155 |
color_brightness = round(color_brightness * (1 - change_brightness_pct)) |
| 156 |
cw_brightness = round(cw_brightness * (1 - change_brightness_pct)) |
| 157 |
else: |
| 158 |
change_brightness_pct = (new_brightness - current_brightness) / ( |
| 159 |
255 - current_brightness |
| 160 |
) |
| 161 |
ww_brightness = round( |
| 162 |
(255 - ww_brightness) * change_brightness_pct + ww_brightness |
| 163 |
) |
| 164 |
color_brightness = round( |
| 165 |
(255 - color_brightness) * change_brightness_pct + color_brightness |
| 166 |
) |
| 167 |
cw_brightness = round( |
| 168 |
(255 - cw_brightness) * change_brightness_pct + cw_brightness |
| 169 |
) |
| 170 |
|
| 171 |
return color_brightness, cw_brightness, ww_brightness |
| 172 |
|
| 173 |
|
| 174 |
def rgbw_brightness( |
| 175 |
rgbw_data: tuple[int, int, int, int], |
| 176 |
brightness: int | None = None, |
| 177 |
) -> tuple[int, int, int, int]: |
| 178 |
"""Convert rgbw to brightness.""" |
| 179 |
original_r, original_g, original_b = rgbw_data[0:3] |
| 180 |
h, s, v = colorsys.rgb_to_hsv(original_r / 255, original_g / 255, original_b / 255) |
| 181 |
color_brightness = round(v * 255) |
| 182 |
ww_brightness = rgbw_data[3] |
| 183 |
current_brightness = round((color_brightness + ww_brightness) / 2) |
| 184 |
|
| 185 |
if not brightness or brightness == current_brightness: |
| 186 |
return rgbw_data |
| 187 |
|
| 188 |
if brightness < current_brightness: |
| 189 |
change_brightness_pct = (current_brightness - brightness) / current_brightness |
| 190 |
ww_brightness = round(ww_brightness * (1 - change_brightness_pct)) |
| 191 |
color_brightness = round(color_brightness * (1 - change_brightness_pct)) |
| 192 |
|
| 193 |
else: |
| 194 |
change_brightness_pct = (brightness - current_brightness) / ( |
| 195 |
255 - current_brightness |
| 196 |
) |
| 197 |
ww_brightness = round( |
| 198 |
(255 - ww_brightness) * change_brightness_pct + ww_brightness |
| 199 |
) |
| 200 |
color_brightness = round( |
| 201 |
(255 - color_brightness) * change_brightness_pct + color_brightness |
| 202 |
) |
| 203 |
|
| 204 |
r, g, b = colorsys.hsv_to_rgb(h, s, color_brightness / 255) |
| 205 |
return (round(r * 255), round(g * 255), round(b * 255), ww_brightness) |
| 206 |
|
| 207 |
|
| 208 |
def rgbww_brightness( |
| 209 |
rgbww_data: tuple[int, int, int, int, int], |
| 210 |
brightness: int | None = None, |
| 211 |
) -> tuple[int, int, int, int, int]: |
| 212 |
"""Convert rgbww to brightness.""" |
| 213 |
original_r, original_g, original_b = rgbww_data[0:3] |
| 214 |
h, s, v = colorsys.rgb_to_hsv(original_r / 255, original_g / 255, original_b / 255) |
| 215 |
color_brightness = round(v * 255) |
| 216 |
ww_brightness = rgbww_data[3] |
| 217 |
cw_brightness = rgbww_data[4] |
| 218 |
current_brightness = round((color_brightness + ww_brightness + cw_brightness) / 3) |
| 219 |
|
| 220 |
if not brightness or brightness == current_brightness: |
| 221 |
return rgbww_data |
| 222 |
|
| 223 |
color_brightness, cw_brightness, ww_brightness = _adjust_brightness( |
| 224 |
current_brightness, brightness, color_brightness, cw_brightness, ww_brightness |
| 225 |
) |
| 226 |
r, g, b = colorsys.hsv_to_rgb(h, s, color_brightness / 255) |
| 227 |
return ( |
| 228 |
round(r * 255), |
| 229 |
round(g * 255), |
| 230 |
round(b * 255), |
| 231 |
ww_brightness, |
| 232 |
cw_brightness, |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
def rgbcw_brightness( |
| 237 |
rgbcw_data: tuple[int, int, int, int, int], |
| 238 |
brightness: int | None = None, |
| 239 |
) -> tuple[int, int, int, int, int]: |
| 240 |
"""Convert rgbww to brightness.""" |
| 241 |
original_r, original_g, original_b = rgbcw_data[0:3] |
| 242 |
h, s, v = colorsys.rgb_to_hsv(original_r / 255, original_g / 255, original_b / 255) |
| 243 |
color_brightness = round(v * 255) |
| 244 |
cw_brightness = rgbcw_data[3] |
| 245 |
ww_brightness = rgbcw_data[4] |
| 246 |
current_brightness = round((color_brightness + ww_brightness + cw_brightness) / 3) |
| 247 |
|
| 248 |
if not brightness or brightness == current_brightness: |
| 249 |
return rgbcw_data |
| 250 |
|
| 251 |
color_brightness, cw_brightness, ww_brightness = _adjust_brightness( |
| 252 |
current_brightness, brightness, color_brightness, cw_brightness, ww_brightness |
| 253 |
) |
| 254 |
r, g, b = colorsys.hsv_to_rgb(h, s, color_brightness / 255) |
| 255 |
return ( |
| 256 |
round(r * 255), |
| 257 |
round(g * 255), |
| 258 |
round(b * 255), |
| 259 |
cw_brightness, |
| 260 |
ww_brightness, |
| 261 |
) |
| 262 |
|
| 263 |
|
| 264 |
def color_temp_to_white_levels( |
| 265 |
temperature: int, |
| 266 |
brightness: float, |
| 267 |
min_temp: int = MIN_TEMP, |
| 268 |
max_temp: int = MAX_TEMP, |
| 269 |
) -> WhiteLevels: |
| 270 |
|
| 271 |
|
| 272 |
temperature = min(max(min_temp, temperature), max_temp) |
| 273 |
if not (0 <= brightness <= 255): |
| 274 |
raise ValueError( |
| 275 |
f"Brightness of {brightness} is not valid and must be between 0 and 255" |
| 276 |
) |
| 277 |
brightness = round(brightness / 255, 2) |
| 278 |
warm = ((max_temp - temperature) / (max_temp - min_temp)) * brightness |
| 279 |
cold = brightness - warm |
| 280 |
return WhiteLevels(round(255 * warm), round(255 * cold)) |
| 281 |
|
| 282 |
|
| 283 |
def scaled_color_temp_to_white_levels( |
| 284 |
temperature: int, brightness: float |
| 285 |
) -> WhiteLevels: |
| 286 |
|
| 287 |
|
| 288 |
if not (0 <= temperature <= 100): |
| 289 |
raise ValueError( |
| 290 |
f"Temperature of {temperature} is not valid and must be between {0} and {100}" |
| 291 |
) |
| 292 |
if not (0 <= brightness <= 100): |
| 293 |
raise ValueError( |
| 294 |
f"Brightness of {brightness} is not valid and must be between 0 and 100" |
| 295 |
) |
| 296 |
brightness = round(brightness / 100, 2) |
| 297 |
warm = ((100 - temperature) / 100) * brightness |
| 298 |
cold = brightness - warm |
| 299 |
return WhiteLevels(round(255 * warm), round(255 * cold)) |
| 300 |
|
| 301 |
|
| 302 |
def white_levels_to_color_temp( |
| 303 |
warm_white: int, cool_white: int, min_temp: int = MIN_TEMP, max_temp: int = MAX_TEMP |
| 304 |
) -> TemperatureBrightness: |
| 305 |
if not (0 <= warm_white <= 255): |
| 306 |
raise ValueError( |
| 307 |
f"Warm White of {warm_white} is not valid and must be between 0 and 255" |
| 308 |
) |
| 309 |
if not (0 <= cool_white <= 255): |
| 310 |
raise ValueError( |
| 311 |
f"Cool White of {cool_white} is not valid and must be between 0 and 255" |
| 312 |
) |
| 313 |
warm = warm_white / 255 |
| 314 |
cold = cool_white / 255 |
| 315 |
brightness = warm + cold |
| 316 |
if brightness == 0: |
| 317 |
temperature: float = min_temp |
| 318 |
else: |
| 319 |
temperature = ((cold / brightness) * (max_temp - min_temp)) + min_temp |
| 320 |
return TemperatureBrightness(round(temperature), min(255, round(brightness * 255))) |
| 321 |
|
| 322 |
|
| 323 |
def white_levels_to_scaled_color_temp( |
| 324 |
warm_white: int, cool_white: int |
| 325 |
) -> TemperatureBrightness: |
| 326 |
if not (0 <= warm_white <= 255): |
| 327 |
raise ValueError( |
| 328 |
f"Warm White of {warm_white} is not valid and must be between 0 and 255" |
| 329 |
) |
| 330 |
if not (0 <= cool_white <= 255): |
| 331 |
raise ValueError( |
| 332 |
f"Cool White of {cool_white} is not valid and must be between 0 and 255" |
| 333 |
) |
| 334 |
warm = warm_white / 255 |
| 335 |
cold = cool_white / 255 |
| 336 |
brightness = warm + cold |
| 337 |
if brightness == 0: |
| 338 |
temperature: float = 0 |
| 339 |
else: |
| 340 |
temperature = (cold / brightness) * 100 |
| 341 |
return TemperatureBrightness(round(temperature), min(100, round(brightness * 100))) |