Color Module
Provides color manipulation across RGBA, HSLA, and hex spaces.
Includes dedicated classes for each color model, conversions, parsing,
luminance calculation, color adjustments, and gradient generation.
rgba
class rgba(self, red: int, green: int, blue: int, alpha: float | None = None, /, *, _validate: bool = True)An RGB/RGBA color object that includes a bunch of methods to manipulate the color.
red– The red channel in range [0, 255] inclusive.green– The green channel in range [0, 255] inclusive.blue– The blue channel in range [0, 255] inclusive.alpha– The alpha channel in range [0.0, 1.0] inclusive orNoneif the color has no alpha channel.
Example Usage
from xulbux import rgba
color = rgba(30, 144, 255) # Dodger Blue
# Adjust color properties:
lighter = color.lighten(0.2)
darker = color.darken(0.15)
blended = color.blend(rgba(255, 0, 0), ratio=0.5)
# Convert to other color spaces:
hex_str = color.as_hexa() # #1E90FF
hsl_obj = color.as_hsla() # hsla(210°, 100%, 56%).has_alpha()
def has_alpha(self) -> boolReturns True if the color has an alpha channel and False otherwise.
.as_tuple()
def as_tuple(self) -> tuple[int, int, int, float | None]Returns the color components as a tuple (red, green, blue, alpha).
.as_dict()
def as_dict(self) -> RgbaDictReturns the color components as a dictionary with keys "red", "green", "blue" and optionally "alpha".
.lighten()
def lighten(self, amount: float, /) -> rgbaIncreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.darken()
def darken(self, amount: float, /) -> rgbaDecreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.saturate()
def saturate(self, amount: float, /) -> rgbaIncreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.desaturate()
def desaturate(self, amount: float, /) -> rgbaDecreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.rotate()
def rotate(self, degrees: int, /) -> rgbaRotates the colors hue by the specified number of degrees.
.invert()
def invert(self, *, invert_alpha: bool = False) -> rgbaInverts the color by rotating hue by 180 degrees and inverting lightness.
.grayscale()
def grayscale(self, *, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> rgbaConverts the color to grayscale using the luminance formula.
method– The luminance calculation method to use:"wcag2"WCAG 2.0 standard (default and most accurate for perception)"wcag3"draft WCAG 3.0 standard with improved coefficients"simple"simple arithmetic mean (less accurate)"bt601"ITU-R BT.601 standard (older TV standard)
.blend()
Blends the current color with another color using the specified ratio in range [0.0, 1.0] inclusive.
other– The other RGBA color to blend with.ratio– The blend ratio between the two colors:- If
ratiois0.0it means 100% of the current color and 0% of theothercolor (2:0 mixture). - If
ratiois0.5it means 50% of both colors (1:1 mixture). - If
ratiois1.0it means 0% of the current color and 100% of theothercolor (0:2 mixture).
- If
additive_alpha– Whether to blend the alpha channels additively or not.
.is_dark()
def is_dark(self) -> boolReturns True if the color is considered dark (lightness < 50%).
.is_light()
def is_light(self) -> boolReturns True if the color is considered light (lightness >= 50%).
.with_alpha()
def with_alpha(self, alpha: float | None, /) -> rgbaReturns a new color with the specified alpha value, or None to clear alpha.
.complementary()
def complementary(self) -> rgbaReturns the complementary color (180 degrees on the color wheel).
hsla
class hsla(self, hue: int, sat: int, light: int, alpha: float | None = None, /, *, _validate: bool = True)A HSL/HSLA color object that includes a bunch of methods to manipulate the color.
hue– The hue channel in range [0, 360] inclusive.sat– The saturation channel in range [0, 100] inclusive.light– The lightness channel in range [0, 100] inclusive.alpha– The alpha channel in range [0.0, 1.0] inclusive orNoneif the color has no alpha channel.
Example Usage
from xulbux import hsla
color = hsla(210, 100, 56) # Dodger Blue
# Rotate hue and adjust saturation:
complementary = color.complementary() # hsla(30°, 100%, 56%)
saturated = color.saturate(0.2)
desaturated = color.desaturate(0.3)
# Convert to other color spaces:
rgb_obj = color.as_rgba() # rgba(30, 144, 255)
hex_obj = color.as_hexa() # #1E90FF.has_alpha()
def has_alpha(self) -> boolReturns True if the color has an alpha channel and False otherwise.
.as_tuple()
def as_tuple(self) -> tuple[int, int, int, float | None]Returns the color components as a tuple (hue, sat, light, alpha).
.as_dict()
def as_dict(self) -> HslaDictReturns the color components as a dictionary with keys "hue", "sat", "light" and optionally "alpha".
.lighten()
def lighten(self, amount: float, /) -> hslaIncreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.darken()
def darken(self, amount: float, /) -> hslaDecreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.saturate()
def saturate(self, amount: float, /) -> hslaIncreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.desaturate()
def desaturate(self, amount: float, /) -> hslaDecreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.rotate()
def rotate(self, degrees: int, /) -> hslaRotates the colors hue by the specified number of degrees.
.invert()
def invert(self, *, invert_alpha: bool = False) -> hslaInverts the color by rotating hue by 180 degrees and inverting lightness.
.grayscale()
def grayscale(self, *, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> hslaConverts the color to grayscale using the luminance formula.
method– The luminance calculation method to use:"wcag2"WCAG 2.0 standard (default and most accurate for perception)"wcag3"draft WCAG 3.0 standard with improved coefficients"simple"simple arithmetic mean (less accurate)"bt601"ITU-R BT.601 standard (older TV standard)
.blend()
Blends the current color with another color using the specified ratio in range [0.0, 1.0] inclusive.
other– The other HSLA color to blend with.ratio– The blend ratio between the two colors:- If
ratiois0.0it means 100% of the current color and 0% of theothercolor (2:0 mixture). - If
ratiois0.5it means 50% of both colors (1:1 mixture). - If
ratiois1.0it means 0% of the current color and 100% of theothercolor (0:2 mixture).
- If
additive_alpha– whether to blend the alpha channels additively or not.
.is_dark()
def is_dark(self) -> boolReturns True if the color is considered dark (lightness < 50%).
.is_light()
def is_light(self) -> boolReturns True if the color is considered light (lightness >= 50%).
.is_grayscale()
def is_grayscale(self) -> boolReturns True if the color is considered grayscale.
.with_alpha()
def with_alpha(self, alpha: float | None, /) -> hslaReturns a new color with the specified alpha value, or None to clear alpha.
.complementary()
def complementary(self) -> hslaReturns the complementary color (180 degrees on the color wheel).
hexa
class hexa(
self,
color: Hexa | None = None,
/,
*,
_red: int | None = None,
_green: int | None = None,
_blue: int | None = None,
_alpha: float | None = None,
)A hex color object that includes a bunch of methods to manipulate the color.
color– The hex color string (prefix optional) or hex integer, that can be in formats:RGBshort format without alpha (only for strings)RGBAshort format with alpha (only for strings)RRGGBBlong format without alpha (for strings and hex integers)RRGGBBAAlong format with alpha (for strings and hex integers)
Example Usage
from xulbux import hexa
color = hexa("#1E90FF") # Dodger Blue
# Manipulate hex color:
lighter = color.lighten(0.2)
inverted = color.invert()
# Convert to other representations:
rgb_obj = color.as_rgba() # rgba(30, 144, 255)
hsl_obj = color.as_hsla() # hsla(210°, 100%, 56%).has_alpha()
def has_alpha(self) -> boolReturns True if the color has an alpha channel and False otherwise.
.as_tuple()
def as_tuple(self, *, round_alpha: bool = True) -> tuple[int, int, int, float | None]Returns the color components as a tuple (red, green, blue, alpha).
.as_dict()
def as_dict(self) -> HexaDictReturns the color components as a dictionary with hex string values
for keys "red", "green", "blue" and optionally "alpha".
.as_rgba()
def as_rgba(self, *, round_alpha: bool = True) -> rgbaReturns the color as rgba() color object.
.as_hsla()
def as_hsla(self, *, round_alpha: bool = True) -> hslaReturns the color as hsla() color object.
.lighten()
def lighten(self, amount: float, /) -> hexaIncreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.darken()
def darken(self, amount: float, /) -> hexaDecreases the colors lightness by the specified amount in range [0.0, 1.0] inclusive.
.saturate()
def saturate(self, amount: float, /) -> hexaIncreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.desaturate()
def desaturate(self, amount: float, /) -> hexaDecreases the colors saturation by the specified amount in range [0.0, 1.0] inclusive.
.rotate()
def rotate(self, degrees: int, /) -> hexaRotates the colors hue by the specified number of degrees.
.invert()
def invert(self, *, invert_alpha: bool = False) -> hexaInverts the color by rotating hue by 180 degrees and inverting lightness.
.grayscale()
def grayscale(self, *, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> hexaConverts the color to grayscale using the luminance formula.
method– The luminance calculation method to use:"wcag2"WCAG 2.0 standard (default and most accurate for perception)"wcag3"draft WCAG 3.0 standard with improved coefficients"simple"simple arithmetic mean (less accurate)"bt601"ITU-R BT.601 standard (older TV standard)
.blend()
Blends the current color with another color using the specified ratio in range [0.0, 1.0] inclusive.
other– The other hex color to blend with.ratio– The blend ratio between the two colors:- If
ratiois0.0it means 100% of the current color and 0% of theothercolor (2:0 mixture). - If
ratiois0.5it means 50% of both colors (1:1 mixture). - If
ratiois1.0it means 0% of the current color and 100% of theothercolor (0:2 mixture).
- If
additive_alpha– Whether to blend the alpha channels additively or not.
.is_dark()
def is_dark(self) -> boolReturns True if the color is considered dark (lightness < 50%).
.is_light()
def is_light(self) -> boolReturns True if the color is considered light (lightness >= 50%).
.is_grayscale()
def is_grayscale(self) -> boolReturns True if the color is grayscale (saturation == 0).
.with_alpha()
def with_alpha(self, alpha: float | None, /) -> hexaReturns a new color with the specified alpha value, or None to clear alpha.
.complementary()
def complementary(self) -> hexaReturns the complementary color (180 degrees on the color wheel).
is_valid_rgba()
Check if the given color is a valid RGBA color.
color– The color to check (can be in any supported format).allow_alpha– Whether to allow alpha channel in the color.allow_strings– Whether to allow color strings.
is_valid_hsla()
Check if the given color is a valid HSLA color.
color– The color to check (can be in any supported format).allow_alpha– Whether to allow alpha channel in the color.allow_strings– Whether to allow color strings.
is_valid_hexa()
def is_valid_hexa(
color: object, /, *, allow_alpha: bool = True, get_prefix: bool = False
) -> TypeGuard[Hexa] | tuple[bool, Literal["#", "0x"] | None]Check if the given color is a valid hex color.
color– The color to check (can be in any supported format).allow_alpha– Whether to allow alpha channel in the color.get_prefix– If true, the prefix used in the color (if any)- is returned along with validity.
is_valid()
Check if the given color is a valid RGBA, HSLA or hex color.
color– The color to check (can be in any supported format).allow_alpha– Whether to allow alpha channel in the color.
has_alpha()
Check if the given color has an alpha channel.
color– The color to check (can be in any supported format).
to_rgba()
Will try to convert any color type to a color of type RGBA.
color– The color to convert (can be in any supported format).
to_hsla()
Will try to convert any color type to a color of type HSLA.
color– The color to convert (can be in any supported format).
to_hexa()
Will try to convert any color type to a color of type hex.
color– The color to convert (can be in any supported format).
extract_rgba()
Will try to recognize RGBA colors inside a string and output the found ones as RGBA objects.
string– The string to search for RGBA colors.only_first– If true, only the first found color will be returned, otherwise a list of all found colors.
extract_hsla()
Will try to recognize HSLA colors inside a string and output the found ones as HSLA objects.
string– The string to search for HSLA colors.only_first– If true, only the first found color will be returned, otherwise a list of all found colors.
rgba_to_hex_int()
def rgba_to_hex_int(red: int, green: int, blue: int, alpha: float | None = None, /, *, preserve_original: bool = False) -> intConvert RGBA channels to a hex integer (alpha is optional).
red,green,blue– The red, green, and blue channels in range [0, 255] inclusive.alpha– The alpha channel in range [0.0, 1.0] inclusive orNoneif not set.preserve_original– Whether to preserve the original color exactly (explained below).
To preserve leading zeros, the function will add a 1 at the beginning,
if the hex integer would start with a 0.
This could affect the color a little bit, but will make sure, that it won't be interpreted
as a completely different color, when initializing it as a hexa() color or changing it
back to RGBA using hex_int_to_rgba().
hex_int_to_rgba()
def hex_int_to_rgba(hex_int: int, /, *, preserve_original: bool = False) -> rgbaConvert a hex integer to RGBA channels.
hex_int– The hex integer to convert.preserve_original– Whether to preserve the original color exactly (explained below).
If the red channel is 1 after conversion, it will be set to 0, because when converting
from RGBA to a hex integer, the first 0 will be set to 1 to preserve leading zeros.
This is the correction, so the color doesn't even look slightly different.
get_luminance()
def get_luminance(
red: int,
green: int,
blue: int,
/,
*,
output_type: type[int | float] | None = None,
method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2",
) -> int | floatCalculates the relative luminance of a color according to various standards.
red,green,blue– The red, green and blue channels in range [0, 255] inclusive.output_type– The range of the returned luminance value:intreturns integer in range [0, 100] inclusive.floatreturns float in range [0.0, 1.0] inclusive.Nonereturns integer in range [0, 255] inclusive.
method– The luminance calculation method to use:"wcag2"WCAG 2.0 standard (default and most accurate for perception)"wcag3"draft WCAG 3.0 standard with improved coefficients"simple"simple arithmetic mean (less accurate)"bt601"ITU-R BT.601 standard (older TV standard)
get_text_fg()
Returns either black or white text color for optimal contrast on the given background color.
text_bg_color– The background color (can be in RGBA or hex format).
adjust_lightness()
In- or decrease the lightness of the input color.
color– The color to adjust (can be in RGBA or hex format).light_change– The amount to change the lightness by, in range-1.0(darken by 100%) and1.0(lighten by 100%).
adjust_saturation()
In- or decrease the saturation of the input color.
color– The color to adjust (can be in RGBA or hex format).sat_change– The amount to change the saturation by, in range-1.0(saturate by 100%) and1.0(desaturate by 100%).
ColorStop
A color stop for gradient generation: a color value or a (color, position) tuple.
interpolate_color()
Linearly interpolates between two colors in the specified color space.
color1– The starting color (RGBA, HSLA, hex, or tuple).color2– The ending color (RGBA, HSLA, hex, or tuple).ratio– The blend ratio between0.0(100%color1) and1.0(100%color2).space– The color space to interpolate in ("rgb","hsl","hsl_long","linear_rgb", or"oklab"). Default is"hsl".
Raises ValueError if space is invalid.
create_gradient()
Creates a tuple of steps smoothly interpolated colors across the given color stops.
colors– Sequence of colors or(color, position)tuples defining the gradient stops.steps– Total number of color steps to generate. Default is10.space– The color space to interpolate in ("rgb","hsl","hsl_long","linear_rgb", or"oklab"). Default is"hsl".
Raises ValueError if steps < 1, colors is empty, or space is invalid.