Color Utilities¶
lifx-async provides comprehensive color utilities for working with LIFX's HSBK color model and converting to/from RGB.
HSBK Class¶
The HSBK class represents colors in the Hue, Saturation, Brightness, Kelvin color model used by
LIFX devices.
HSBK
¶
User-friendly HSBK color representation.
LIFX devices use HSBK (Hue, Saturation, Brightness, Kelvin) color space. This class provides a convenient interface with normalized values and conversion to/from RGB.
| ATTRIBUTE | DESCRIPTION |
|---|---|
hue |
Hue value in degrees (0-360)
TYPE:
|
saturation |
Saturation (0.0-1.0, where 0 is white and 1 is fully saturated)
TYPE:
|
brightness |
Brightness (0.0-1.0, where 0 is off and 1 is full brightness)
TYPE:
|
kelvin |
Color temperature in Kelvin (1500-9000, typically 2500-9000 for LIFX). Devices also report 0 for a colour with no white component; that value is accepted and round-trips unchanged.
TYPE:
|
Example
| METHOD | DESCRIPTION |
|---|---|
__eq__ |
Two colors are equal if they share the same wire representation. |
__hash__ |
Return a hash consistent with uint16 equality. |
__str__ |
Return a string representation of the HSBK values for this color. |
__repr__ |
Return a string representation of the HSBK values for this color. |
from_rgb |
Create HSBK from RGB values. |
to_rgb |
Convert HSBK to RGB values. |
to_protocol |
Convert to protocol HSBK for packet serialization. |
from_protocol |
Create HSBK from protocol HSBK. |
with_hue |
Create a new HSBK with modified hue. |
with_saturation |
Create a new HSBK with modified saturation. |
with_brightness |
Create a new HSBK with modified brightness. |
with_kelvin |
Create a new HSBK with modified color temperature. |
replace |
Create a new HSBK, replacing only the components that were supplied. |
lerp_hsb |
Interpolate to another color via shortest-path HSB blending. |
lerp_oklab |
Interpolate to another color through Oklab perceptual color space. |
clone |
Create a copy of this color. |
as_tuple |
Return HSBK values as a tuple of protocol uint16 values. |
limit_distance_to |
Return a new color with hue limited to 90 degrees from another color. |
average |
Calculate the average color of a list of HSBK colors. |
Source code in src/lifx/color.py
Attributes¶
saturation_pct
property
¶
saturation_pct: float
Return saturation as a percentage (0.0-100.0).
Home Assistant expresses the saturation half of hs_color as a
percentage rather than this class's 0.0-1.0 fraction.
brightness_pct
property
¶
brightness_pct: float
Return brightness as a percentage (0.0-100.0).
Provided for symmetry with :attr:saturation_pct; Home Assistant
itself wants :attr:brightness_uint8 for a light's brightness.
brightness_uint8
property
¶
brightness_uint8: int
Return brightness as an 8-bit value (0-255).
Home Assistant and most RGB tooling express brightness as a uint8 rather than the protocol's uint16 or this class's 0.0-1.0 float.
Any non-zero brightness returns at least 1: rounding alone maps everything below 0.5/255 to 0, and a light reporting brightness 0 while powered on reads as off to Home Assistant, which then writes that 0 back and switches the light off.
kelvin
property
¶
kelvin: int
Return kelvin.
Usually 1500-9000, but 0 when the device reports a colour with no white component (a fully saturated zone).
as_dict
property
¶
Methods:¶
__eq__
¶
Two colors are equal if they share the same wire representation.
Equality is defined at uint16 (protocol) granularity rather than on the raw floats. This keeps colors that round-trip through the protocol stable under comparison despite sub-uint16 floating-point drift.
Source code in src/lifx/color.py
__str__
¶
__str__() -> str
Return a string representation of the HSBK values for this color.
Source code in src/lifx/color.py
from_rgb
classmethod
¶
Create HSBK from RGB values.
| PARAMETER | DESCRIPTION |
|---|---|
red
|
Red component (0.0-1.0)
TYPE:
|
green
|
Green component (0.0-1.0)
TYPE:
|
blue
|
Blue component (0.0-1.0)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
HSBK instance |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If RGB values are out of range (0.0-1.0) |
Example
Source code in src/lifx/color.py
to_rgb
¶
Convert HSBK to RGB values.
Color temperature (kelvin) is not considered in this conversion, as it only affects the white point of the device.
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float, float]
|
Tuple of (red, green, blue) with values 0.0-1.0 |
Example
Source code in src/lifx/color.py
to_protocol
¶
to_protocol() -> LightHsbk
Convert to protocol HSBK for packet serialization.
LIFX protocol uses uint16 values for all HSBK components:
- Hue: 0-65535 (represents 0-360 degrees)
- Saturation: 0-65535 (represents 0-100%)
- Brightness: 0-65535 (represents 0-100%)
- Kelvin: Direct value in Kelvin
| RETURNS | DESCRIPTION |
|---|---|
LightHsbk
|
LightHsbk instance for packet serialization |
Example
Source code in src/lifx/color.py
from_protocol
classmethod
¶
Create HSBK from protocol HSBK.
| PARAMETER | DESCRIPTION |
|---|---|
protocol
|
LightHsbk instance from packet deserialization
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
User-friendly HSBK instance |
Example
Source code in src/lifx/color.py
with_hue
¶
with_saturation
¶
with_brightness
¶
with_kelvin
¶
Create a new HSBK with modified color temperature.
| PARAMETER | DESCRIPTION |
|---|---|
kelvin
|
New kelvin value (1500-9000, or 0 for no white component)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance |
Source code in src/lifx/color.py
replace
¶
replace(
hue: float | None = None,
saturation: float | None = None,
brightness: float | None = None,
kelvin: int | None = None,
) -> HSBK
Create a new HSBK, replacing only the components that were supplied.
Each component defaults to the value held by this color, so any subset
can be overridden in a single call rather than chaining with_*
methods. Supplying nothing returns an equivalent color.
| PARAMETER | DESCRIPTION |
|---|---|
hue
|
New hue in degrees (0-360), or None to keep the current hue
TYPE:
|
saturation
|
New saturation (0.0-1.0), or None to keep the current saturation
TYPE:
|
brightness
|
New brightness (0.0-1.0), or None to keep the current brightness
TYPE:
|
kelvin
|
New color temperature (1500-9000, or 0 for no white component), or None to keep the current kelvin
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any supplied value is out of range |
Example
Source code in src/lifx/color.py
lerp_hsb
¶
Interpolate to another color via shortest-path HSB blending.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
Target color to interpolate towards.
TYPE:
|
blend
|
Blend factor from 0.0 (self) to 1.0 (other), clamped to range.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance with interpolated values. Kelvin is preserved from self. |
Source code in src/lifx/color.py
lerp_oklab
¶
Interpolate to another color through Oklab perceptual color space.
Oklab produces perceptually uniform transitions without brightness dips or muddy intermediates that occur with naive HSB blending.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
Target color to interpolate towards.
TYPE:
|
blend
|
Blend factor from 0.0 (self) to 1.0 (other), clamped to range.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance with interpolated values. Kelvin is preserved from self. |
Source code in src/lifx/color.py
clone
¶
clone() -> HSBK
Create a copy of this color.
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance with the same values |
as_tuple
¶
Return HSBK values as a tuple of protocol uint16 values.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Tuple of (hue_u16, saturation_u16, brightness_u16, kelvin) |
int
|
where u16 values are in range 0-65535 |
Example
Source code in src/lifx/color.py
limit_distance_to
¶
Return a new color with hue limited to 90 degrees from another color.
This is useful for preventing large hue jumps when interpolating between colors. If the hue difference is greater than 90 degrees, the hue is adjusted to be within 90 degrees of the target hue.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
Reference color to limit distance to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance with limited hue distance |
Example
Source code in src/lifx/color.py
average
classmethod
¶
Calculate the average color of a list of HSBK colors.
Uses circular mean for hue to correctly handle hue wraparound (e.g., average of 10° and 350° is 0°, not 180°).
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
List of HSBK colors to average (must not be empty) |
| RETURNS | DESCRIPTION |
|---|---|
HSBK
|
New HSBK instance with averaged values |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If colors list is empty |
Example
Source code in src/lifx/color.py
Colors Class¶
The Colors class provides convenient color presets for common colors.
Colors
¶
Common color presets for convenience.
Includes all 140 standard HTML/CSS named colors plus white temperature variants and pastel variations.
HTML color reference: https://www.w3.org/TR/css-color-3/#svg-color
Examples¶
Creating Colors¶
from lifx import HSBK, Colors
# Use built-in color presets
color = Colors.BLUE
# Create custom colors
custom = HSBK(hue=180.0, saturation=1.0, brightness=0.8, kelvin=3500)
# Create from RGB (0.0-1.0)
red = HSBK.from_rgb(1.0, 0.0, 0.0)
# Convert to RGB (returns 0.0-1.0)
r, g, b = Colors.BLUE.to_rgb()
print(f"RGB: ({r:.2f}, {g:.2f}, {b:.2f})")
Color Components¶
from lifx import HSBK
color = HSBK(hue=240.0, saturation=1.0, brightness=0.5, kelvin=3500)
# Hue: 0-360 degrees (0=red, 120=green, 240=blue)
print(f"Hue: {color.hue}°")
# Saturation: 0.0-1.0 (0=white, 1=full color)
print(f"Saturation: {color.saturation_pct}%")
# Brightness: 0.0-1.0 (0=off, 1=full brightness)
print(f"Brightness: {color.brightness_pct}%")
# Kelvin: 1500-9000 (warm white to cool white)
print(f"Temperature: {color.kelvin}K")
Kelvin 0 on device reads
1500-9000 is the white-mode range of the product, not the range of the
protocol field. Devices report kelvin 0 for a colour with no white
component — commonly seen on saturated zones of a Tile, Beam or Z. That
value is accepted by HSBK, survives replace() and the with_* helpers,
and is written back to the device unchanged rather than clamped to 1500.
Scaled Components¶
Saturation and brightness are also available pre-scaled, for consumers that expect percentages or 8-bit values rather than the 0.0-1.0 fractions:
from lifx import HSBK
color = HSBK(hue=240.0, saturation=1.0, brightness=0.5, kelvin=3500)
color.saturation_pct # 100.0 (0.0-100.0)
color.brightness_pct # 50.0 (0.0-100.0)
color.brightness_uint8 # 128 (0-255)
Home Assistant wants (color.hue, color.saturation_pct) for a light's
hs_color and color.brightness_uint8 for its brightness.
Color Manipulation¶
from lifx import HSBK, Light
async def cycle_hue(light: Light):
"""Cycle through the color spectrum"""
for hue in range(0, 360, 10):
color = HSBK(hue=float(hue), saturation=1.0, brightness=0.8, kelvin=3500)
await light.set_color(color, duration=0.1)
Use replace() to override some components while keeping the rest, instead
of chaining the single-component with_* methods:
color = HSBK(hue=180.0, saturation=0.5, brightness=0.75, kelvin=3500)
# Shift the hue and dim it; saturation and kelvin are carried over
dimmed = color.replace(hue=200.0, brightness=0.2)
White Balance¶
from lifx import HSBK
# Warm white (sunset, candlelight)
warm = HSBK(hue=0, saturation=0, brightness=1.0, kelvin=2500)
# Neutral white (daylight)
neutral = HSBK(hue=0, saturation=0, brightness=1.0, kelvin=4000)
# Cool white (overcast, shade)
cool = HSBK(hue=0, saturation=0, brightness=1.0, kelvin=6500)
Available Color Presets¶
The Colors class provides these preset colors:
Colors.WHITE- Pure white (3500K)Colors.RED- RedColors.ORANGE- OrangeColors.YELLOW- YellowColors.GREEN- GreenColors.CYAN- CyanColors.BLUE- BlueColors.PURPLE- PurpleColors.PINK- PinkColors.WARM- Warm white (2500K)Colors.COOL- Cool white (6500K)
Color Conversion Notes¶
RGB to HSBK¶
When converting from RGB to HSBK, note that:
- RGB values are floats in the range 0.0-1.0
- Kelvin defaults to 3500K (neutral)
- Conversion uses standard HSV formulas via Python's
colorsys
HSBK to RGB¶
When converting from HSBK to RGB:
- Returns tuple of (r, g, b) with float values 0.0-1.0
- Kelvin temperature is not represented in RGB
- White colors (saturation=0) will be pure gray values
- Round-trip conversion (
from_rgb->to_rgb) preserves values within floating-point epsilon