Home Assistant Unofficial Reference 2024.12.1
homeassistant.util.color Namespace Reference

Classes

class  GamutType
 
class  RGBColor
 
class  XYPoint
 

Functions

float _clamp (float color_component, float minimum=0, float maximum=255)
 
float _get_blue (float temperature)
 
float _get_green (float temperature)
 
float _get_red (float temperature)
 
tuple[int, int] _white_levels_to_color_temperature (int cold, int warm, int min_kelvin, int max_kelvin)
 
float brightness_to_value (tuple[float, float] low_high_range, int brightness)
 
bool check_point_in_lamps_reach (tuple[float, float] p, GamutType Gamut)
 
bool check_valid_gamut (GamutType Gamut)
 
tuple[int, int, int] color_hs_to_RGB (float iH, float iS)
 
tuple[float, float] color_hs_to_xy (float iH, float iS, GamutType|None Gamut=None)
 
tuple[int, int, int] color_hsb_to_RGB (float fH, float fS, float fB)
 
tuple[int, int, int] color_hsv_to_RGB (float iH, float iS, float iV)
 
RGBColor color_name_to_rgb (str color_name)
 
str color_rgb_to_hex (int r, int g, int b)
 
tuple[float, float] color_RGB_to_hs (float iR, float iG, float iB)
 
tuple[float, float, float] color_RGB_to_hsv (float iR, float iG, float iB)
 
tuple[int, int, int, int] color_rgb_to_rgbw (int r, int g, int b)
 
tuple[int, int, int, int, int] color_rgb_to_rgbww (int r, int g, int b, int min_kelvin, int max_kelvin)
 
tuple[float, float] color_RGB_to_xy (int iR, int iG, int iB, GamutType|None Gamut=None)
 
tuple[float, float, int] color_RGB_to_xy_brightness (int iR, int iG, int iB, GamutType|None Gamut=None)
 
tuple[int, int, int] color_rgbw_to_rgb (int r, int g, int b, int w)
 
tuple[int, int, int] color_rgbww_to_rgb (int r, int g, int b, int cw, int ww, int min_kelvin, int max_kelvin)
 
int color_temperature_kelvin_to_mired (float kelvin_temperature)
 
int color_temperature_mired_to_kelvin (float mired_temperature)
 
tuple[float, float] color_temperature_to_hs (float color_temperature_kelvin)
 
tuple[float, float, float] color_temperature_to_rgb (float color_temperature_kelvin)
 
tuple[int, int, int, int, int] color_temperature_to_rgbww (int temperature, int brightness, int min_kelvin, int max_kelvin)
 
tuple[int, int, int] color_xy_brightness_to_RGB (float vX, float vY, int ibrightness, GamutType|None Gamut=None)
 
tuple[float, float] color_xy_to_hs (float vX, float vY, GamutType|None Gamut=None)
 
tuple[int, int, int] color_xy_to_RGB (float vX, float vY, GamutType|None Gamut=None)
 
int color_xy_to_temperature (float x, float y)
 
float cross_product (XYPoint p1, XYPoint p2)
 
XYPoint get_closest_point_to_line (XYPoint A, XYPoint B, XYPoint P)
 
tuple[float, float] get_closest_point_to_point (tuple[float, float] xy_tuple, GamutType Gamut)
 
float get_distance_between_two_points (XYPoint one, XYPoint two)
 
tuple[int,...] match_max_scale (tuple[int,...] input_colors, tuple[float,...] output_colors)
 
list[int] rgb_hex_to_rgb_list (str hex_string)
 
tuple[int, int] rgbww_to_color_temperature (tuple[int, int, int, int, int] rgbww, int min_kelvin, int max_kelvin)
 
int value_to_brightness (tuple[float, float] low_high_range, float value)
 

Variables

dictionary COLORS
 

Detailed Description

Color util methods.

Function Documentation

◆ _clamp()

float homeassistant.util.color._clamp ( float  color_component,
float   minimum = 0,
float   maximum = 255 
)
private
Clamp the given color component value between the given min and max values.

The range defined by the minimum and maximum values is inclusive, i.e. given a
color_component of 0 and a minimum of 10, the returned value is 10.

Definition at line 594 of file color.py.

◆ _get_blue()

float homeassistant.util.color._get_blue ( float  temperature)
private
Get the blue component of the given color temperature in RGB space.

Definition at line 621 of file color.py.

◆ _get_green()

float homeassistant.util.color._get_green ( float  temperature)
private
Get the green component of the given color temp in RGB space.

Definition at line 612 of file color.py.

◆ _get_red()

float homeassistant.util.color._get_red ( float  temperature)
private
Get the red component of the temperature in RGB space.

Definition at line 604 of file color.py.

◆ _white_levels_to_color_temperature()

tuple[int, int] homeassistant.util.color._white_levels_to_color_temperature ( int  cold,
int  warm,
int  min_kelvin,
int   max_kelvin 
)
private
Convert whites to color temperature in kelvin.

Returns a tuple (color_temperature, brightness).

Definition at line 562 of file color.py.

◆ brightness_to_value()

float homeassistant.util.color.brightness_to_value ( tuple[float, float]  low_high_range,
int  brightness 
)
Given a brightness_scale convert a brightness to a single value.

Do not include 0 if the light is off for value 0.

Given a brightness low_high_range of (1,100) this function
will return:

255: 100.0
127: ~49.8039
10: ~3.9216

Definition at line 752 of file color.py.

◆ check_point_in_lamps_reach()

bool homeassistant.util.color.check_point_in_lamps_reach ( tuple[float, float]  p,
GamutType  Gamut 
)
Check if the provided XYPoint can be recreated by a Hue lamp.

Definition at line 713 of file color.py.

◆ check_valid_gamut()

bool homeassistant.util.color.check_valid_gamut ( GamutType  Gamut)
Check if the supplied gamut is valid.

Definition at line 725 of file color.py.

◆ color_hs_to_RGB()

tuple[int, int, int] homeassistant.util.color.color_hs_to_RGB ( float  iH,
float  iS 
)
Convert an hsv color into its rgb representation.

Definition at line 383 of file color.py.

◆ color_hs_to_xy()

tuple[float, float] homeassistant.util.color.color_hs_to_xy ( float  iH,
float  iS,
GamutType | None   Gamut = None 
)
Convert an hs color to its xy representation.

Definition at line 396 of file color.py.

◆ color_hsb_to_RGB()

tuple[int, int, int] homeassistant.util.color.color_hsb_to_RGB ( float  fH,
float  fS,
float  fB 
)
Convert a hsb into its rgb representation.

Definition at line 315 of file color.py.

◆ color_hsv_to_RGB()

tuple[int, int, int] homeassistant.util.color.color_hsv_to_RGB ( float  iH,
float  iS,
float  iV 
)
Convert an hsv color into its rgb representation.

Hue is scaled 0-360
Sat is scaled 0-100
Val is scaled 0-100

Definition at line 372 of file color.py.

◆ color_name_to_rgb()

RGBColor homeassistant.util.color.color_name_to_rgb ( str  color_name)
Convert color name to RGB hex value.

Definition at line 200 of file color.py.

◆ color_rgb_to_hex()

str homeassistant.util.color.color_rgb_to_hex ( int  r,
int  g,
int  b 
)
Return a RGB color from a hex color string.

Definition at line 492 of file color.py.

◆ color_RGB_to_hs()

tuple[float, float] homeassistant.util.color.color_RGB_to_hs ( float  iR,
float  iG,
float  iB 
)
Convert an rgb color to its hs representation.

Definition at line 367 of file color.py.

◆ color_RGB_to_hsv()

tuple[float, float, float] homeassistant.util.color.color_RGB_to_hsv ( float  iR,
float  iG,
float  iB 
)
Convert an rgb color to its hsv representation.

Hue is scaled 0-360
Sat is scaled 0-100
Val is scaled 0-100

Definition at line 356 of file color.py.

◆ color_rgb_to_rgbw()

tuple[int, int, int, int] homeassistant.util.color.color_rgb_to_rgbw ( int  r,
int  g,
int  b 
)
Convert an rgb color to an rgbw representation.

Definition at line 416 of file color.py.

◆ color_rgb_to_rgbww()

tuple[int, int, int, int, int] homeassistant.util.color.color_rgb_to_rgbww ( int  r,
int  g,
int  b,
int  min_kelvin,
int   max_kelvin 
)
Convert an rgb color to an rgbww representation.

Definition at line 438 of file color.py.

◆ color_RGB_to_xy()

tuple[float, float] homeassistant.util.color.color_RGB_to_xy ( int  iR,
int  iG,
int  iB,
GamutType | None   Gamut = None 
)
Convert from RGB color to XY color.

Definition at line 211 of file color.py.

◆ color_RGB_to_xy_brightness()

tuple[float, float, int] homeassistant.util.color.color_RGB_to_xy_brightness ( int  iR,
int  iG,
int  iB,
GamutType | None   Gamut = None 
)
Convert from RGB color to XY color.

Definition at line 221 of file color.py.

◆ color_rgbw_to_rgb()

tuple[int, int, int] homeassistant.util.color.color_rgbw_to_rgb ( int  r,
int  g,
int  b,
int  w 
)
Convert an rgbw color to an rgb representation.

Definition at line 428 of file color.py.

◆ color_rgbww_to_rgb()

tuple[int, int, int] homeassistant.util.color.color_rgbww_to_rgb ( int  r,
int  g,
int  b,
int  cw,
int  ww,
int  min_kelvin,
int   max_kelvin 
)
Convert an rgbww color to an rgb representation.

Definition at line 464 of file color.py.

◆ color_temperature_kelvin_to_mired()

int homeassistant.util.color.color_temperature_kelvin_to_mired ( float  kelvin_temperature)
Convert degrees kelvin to mired shift.

Definition at line 636 of file color.py.

◆ color_temperature_mired_to_kelvin()

int homeassistant.util.color.color_temperature_mired_to_kelvin ( float  mired_temperature)
Convert absolute mired shift to degrees kelvin.

Definition at line 631 of file color.py.

◆ color_temperature_to_hs()

tuple[float, float] homeassistant.util.color.color_temperature_to_hs ( float  color_temperature_kelvin)
Return an hs color from a color temperature in Kelvin.

Definition at line 505 of file color.py.

◆ color_temperature_to_rgb()

tuple[float, float, float] homeassistant.util.color.color_temperature_to_rgb ( float  color_temperature_kelvin)
Return an RGB color from a color temperature in Kelvin.

This is a rough approximation based on the formula provided by T. Helland
http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/

Definition at line 510 of file color.py.

◆ color_temperature_to_rgbww()

tuple[int, int, int, int, int] homeassistant.util.color.color_temperature_to_rgbww ( int  temperature,
int  brightness,
int  min_kelvin,
int   max_kelvin 
)
Convert color temperature in kelvin to rgbcw.

Returns a (r, g, b, cw, ww) tuple.

Definition at line 535 of file color.py.

◆ color_xy_brightness_to_RGB()

tuple[int, int, int] homeassistant.util.color.color_xy_brightness_to_RGB ( float  vX,
float  vY,
int  ibrightness,
GamutType | None   Gamut = None 
)
Convert from XYZ to RGB.

Definition at line 270 of file color.py.

◆ color_xy_to_hs()

tuple[float, float] homeassistant.util.color.color_xy_to_hs ( float  vX,
float  vY,
GamutType | None   Gamut = None 
)
Convert an xy color to its hs representation.

Definition at line 388 of file color.py.

◆ color_xy_to_RGB()

tuple[int, int, int] homeassistant.util.color.color_xy_to_RGB ( float  vX,
float  vY,
GamutType | None   Gamut = None 
)
Convert from XY to a normalized RGB.

Definition at line 261 of file color.py.

◆ color_xy_to_temperature()

int homeassistant.util.color.color_xy_to_temperature ( float  x,
float  y 
)
Convert an xy color to a color temperature in Kelvin.

Uses McCamy's approximation (https://doi.org/10.1002/col.5080170211),
close enough for uses between 2000 K and 10000 K.

Definition at line 582 of file color.py.

◆ cross_product()

float homeassistant.util.color.cross_product ( XYPoint  p1,
XYPoint  p2 
)
Calculate the cross product of two XYPoints.

Definition at line 644 of file color.py.

◆ get_closest_point_to_line()

XYPoint homeassistant.util.color.get_closest_point_to_line ( XYPoint  A,
XYPoint  B,
XYPoint  P 
)
Find the closest point from P to a line defined by A and B.

This point will be reproducible by the lamp
as it is on the edge of the gamut.

Definition at line 656 of file color.py.

◆ get_closest_point_to_point()

tuple[float, float] homeassistant.util.color.get_closest_point_to_point ( tuple[float, float]  xy_tuple,
GamutType   Gamut 
)
Get the closest matching color within the gamut of the light.

Should only be used if the supplied color is outside of the color gamut.

Definition at line 676 of file color.py.

◆ get_distance_between_two_points()

float homeassistant.util.color.get_distance_between_two_points ( XYPoint  one,
XYPoint  two 
)
Calculate the distance between two XYPoints.

Definition at line 649 of file color.py.

◆ match_max_scale()

tuple[int, ...] homeassistant.util.color.match_max_scale ( tuple[int, ...]  input_colors,
tuple[float, ...]   output_colors 
)
Match the maximum value of the output to the input.

Definition at line 403 of file color.py.

◆ rgb_hex_to_rgb_list()

list[int] homeassistant.util.color.rgb_hex_to_rgb_list ( str  hex_string)
Return an RGB color value list from a hex color string.

Definition at line 497 of file color.py.

◆ rgbww_to_color_temperature()

tuple[int, int] homeassistant.util.color.rgbww_to_color_temperature ( tuple[int, int, int, int, int]  rgbww,
int  min_kelvin,
int   max_kelvin 
)
Convert rgbcw to color temperature in kelvin.

Returns a tuple (color_temperature, brightness).

Definition at line 551 of file color.py.

◆ value_to_brightness()

int homeassistant.util.color.value_to_brightness ( tuple[float, float]  low_high_range,
float  value 
)
Given a brightness_scale convert a single value to a brightness.

Do not include 0 if the light is off for value 0.

Given a brightness low_high_range of (1,100) this function
will return:

100: 255
50: 128
4: 10

The value will be clamped between 1..255 to ensure valid value.

Definition at line 767 of file color.py.

Variable Documentation

◆ COLORS

dictionary homeassistant.util.color.COLORS

Definition at line 28 of file color.py.