Color Functions API

This document covers all factory functions and conversion functions available in Colorium.

Factory Functions

from_string

1
def from_string(color_str: str) -> Optional[Color]

Create a Color from any valid string representation.

Supported formats:

  • Named colors: "red", "blue", "white"
  • Hex colors: "#FF0000", "#F00", "FF0000"
  • RGB: "rgb(255, 0, 0)", "rgba(255, 0, 0, 0.5)"
  • HSL: "hsl(0, 100%, 50%)", "hsla(0, 100%, 50%, 0.5)"
  • HWB: "hwb(0, 0%, 0%)", "hwba(0, 0%, 0%, 0.5)"
  • CMYK: "cmyk(0%, 100%, 100%, 0%)"
  • NCOL: "ncol(R50, 0%, 0%)"

Parameters:

ParameterTypeDescription
color_strstrString representation of a color

Returns: Color object or None if parsing fails

Example:

1
2
3
4
5
6
7
from colorium import from_string

red = from_string("red")
hex_color = from_string("#FF0000")
rgb_color = from_string("rgb(255, 0, 0)")
hsl_color = from_string("hsl(0, 100%, 50%)")
cmyk_color = from_string("cmyk(0%, 100%, 100%, 0%)")

from_hex

1
def from_hex(hex_str: str) -> Optional[Color]

Create a Color from a hexadecimal color code.

Supported formats:

  • 6-digit: "FF0000", "#FF0000"
  • 3-digit shorthand: "#F00" (expands to "#FF0000")

Parameters:

ParameterTypeDescription
hex_strstrHex string with or without # prefix

Returns: Color object or None if invalid

Example:

1
2
3
4
5
from colorium import from_hex

red = from_hex("#FF0000")
green = from_hex("00FF00")
blue = from_hex("#00F")  # shorthand

from_rgb

1
def from_rgb(r: int, g: int, b: int, opacity: float = 1.0) -> Color

Create a Color from RGB values.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
4
from colorium import from_rgb

red = from_rgb(255, 0, 0)
semi_transparent = from_rgb(255, 0, 0, 0.5)

from_hsl

1
def from_hsl(hue: float, sat: float, lightness: float, opacity: float = 1.0) -> Color

Create a Color from HSL values.

Parameters:

ParameterTypeRangeDescription
huefloat0-360Hue angle
satfloat0-1Saturation
lightnessfloat0-1Lightness
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
4
5
from colorium import from_hsl

red = from_hsl(0, 1.0, 0.5)
pastel = from_hsl(200, 0.3, 0.7)
dark = from_hsl(120, 0.8, 0.2)

from_hwb

1
def from_hwb(hue: float, white: float, black: float, opacity: float = 1.0) -> Color

Create a Color from HWB values.

Parameters:

ParameterTypeRangeDescription
huefloat0-360Hue angle
whitefloat0-1Whiteness
blackfloat0-1Blackness
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
4
5
from colorium import from_hwb

red = from_hwb(0, 0, 0)
pink = from_hwb(0, 0.5, 0)
dark_red = from_hwb(0, 0, 0.5)

from_cmyk

1
def from_cmyk(c: float, m: float, y: float, k: float, opacity: float = 1.0) -> Color

Create a Color from CMYK values.

Parameters:

ParameterTypeRangeDescription
cfloat0-1Cyan
mfloat0-1Magenta
yfloat0-1Yellow
kfloat0-1Black
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
4
5
from colorium import from_cmyk

red = from_cmyk(0, 1.0, 1.0, 0)
black = from_cmyk(0, 0, 0, 1.0)
rich_black = from_cmyk(0.5, 0.5, 0.5, 1.0)

from_oklch

1
def from_oklch(l: float, c: float, h: float, opacity: float = 1.0) -> Color

Create a Color from OKLCH values (CSS Color Level 4).

Parameters:

ParameterTypeRangeDescription
lfloat0-1Lightness
cfloat0-0.4+Chroma
hfloat0-360Hue
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
from colorium import from_oklch

color = from_oklch(0.5, 0.2, 180)

from_lab

1
def from_lab(l: float, a: float, b: float, opacity: float = 1.0) -> Color

Create a Color from CIE Lab* values (CSS Color Level 4).

Parameters:

ParameterTypeRangeDescription
lfloat0-100Lightness
afloat-128 to 128a* axis
bfloat-128 to 128b* axis
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
from colorium import from_lab

color = from_lab(50, 20, -30)

from_lch

1
def from_lch(l: float, c: float, h: float, opacity: float = 1.0) -> Color

Create a Color from CIE LCH values (CSS Color Level 4).

Parameters:

ParameterTypeRangeDescription
lfloat0-100Lightness
cfloat0-100+Chroma
hfloat0-360Hue
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
from colorium import from_lch

color = from_lch(50, 30, 180)

from_p3

1
def from_p3(r: float, g: float, b: float, opacity: float = 1.0) -> Color

Create a Color from Display P3 values.

Parameters:

ParameterTypeRangeDescription
rfloat0-1+Red in P3 space
gfloat0-1+Green in P3 space
bfloat0-1+Blue in P3 space
opacityfloat0-1Opacity (default: 1.0)

Returns: Color object

Example:

1
2
3
from colorium import from_p3

color = from_p3(0.8, 0.3, 0.5)

Color Space Conversion Functions

rgb_to_hsl

1
def rgb_to_hsl(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB values to HSL.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'h': 0-360,  # Hue
    's': 0-1,    # Saturation
    'l': 0-1     # Lightness
}

Example:

1
2
3
4
from colorium import rgb_to_hsl

hsl = rgb_to_hsl(255, 0, 0)
print(hsl)  # {'h': 0, 's': 1.0, 'l': 0.5}

hsl_to_rgb

1
def hsl_to_rgb(hue: float, sat: float, lightness: float) -> Dict[str, int]

Convert HSL values to RGB.

Parameters:

ParameterTypeRangeDescription
huefloat0-360Hue angle
satfloat0-1Saturation
lightnessfloat0-1Lightness

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import hsl_to_rgb

rgb = hsl_to_rgb(0, 1.0, 0.5)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_hwb

1
def rgb_to_hwb(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB values to HWB.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'h': 0-360,  # Hue
    'w': 0-1,    # Whiteness
    'b': 0-1     # Blackness
}

Example:

1
2
3
4
from colorium import rgb_to_hwb

hwb = rgb_to_hwb(255, 0, 0)
print(hwb)  # {'h': 0, 'w': 0.0, 'b': 0.0}

hwb_to_rgb

1
def hwb_to_rgb(hue: float, white: float, black: float) -> Dict[str, int]

Convert HWB values to RGB.

Parameters:

ParameterTypeRangeDescription
huefloat0-360Hue angle
whitefloat0-1Whiteness
blackfloat0-1Blackness

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import hwb_to_rgb

rgb = hwb_to_rgb(0, 0.0, 0.0)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_cmyk

1
def rgb_to_cmyk(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB values to CMYK.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
6
{
    'c': 0-1,  # Cyan
    'm': 0-1,  # Magenta
    'y': 0-1,  # Yellow
    'k': 0-1   # Black
}

Example:

1
2
3
4
from colorium import rgb_to_cmyk

cmyk = rgb_to_cmyk(255, 0, 0)
print(cmyk)  # {'c': 0.0, 'm': 1.0, 'y': 1.0, 'k': 0.0}

cmyk_to_rgb

1
def cmyk_to_rgb(c: float, m: float, y: float, k: float) -> Dict[str, int]

Convert CMYK values to RGB.

Parameters:

ParameterTypeRangeDescription
cfloat0-1Cyan
mfloat0-1Magenta
yfloat0-1Yellow
kfloat0-1Black

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import cmyk_to_rgb

rgb = cmyk_to_rgb(0.0, 1.0, 1.0, 0.0)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_oklch

1
def rgb_to_oklch(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB to OKLCH color space.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'l': 0-1,    # Lightness
    'c': 0-0.4+, # Chroma
    'h': 0-360   # Hue
}

Example:

1
2
3
4
from colorium import rgb_to_oklch

oklch = rgb_to_oklch(255, 0, 0)
print(oklch)  # {'l': 0.628, 'c': 0.258, 'h': 29.2}

oklch_to_rgb

1
def oklch_to_rgb(l: float, c: float, h: float) -> Dict[str, int]

Convert OKLCH to RGB.

Parameters:

ParameterTypeRangeDescription
lfloat0-1Lightness
cfloat0-0.4+Chroma
hfloat0-360Hue

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import oklch_to_rgb

rgb = oklch_to_rgb(0.628, 0.258, 29.2)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_lab

1
def rgb_to_lab(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB to CIE Lab* color space.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'l': 0-100,      # Lightness
    'a': -128 to 128, # a* axis
    'b': -128 to 128  # b* axis
}

Example:

1
2
3
4
from colorium import rgb_to_lab

lab = rgb_to_lab(255, 0, 0)
print(lab)  # {'l': 53.24, 'a': 80.09, 'b': 67.20}

lab_to_rgb

1
def lab_to_rgb(l: float, a: float, b: float) -> Dict[str, int]

Convert CIE Lab* to RGB.

Parameters:

ParameterTypeRangeDescription
lfloat0-100Lightness
afloat-128 to 128a* axis
bfloat-128 to 128b* axis

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import lab_to_rgb

rgb = lab_to_rgb(53.24, 80.09, 67.20)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_lch

1
def rgb_to_lch(r: int, g: int, b: int) -> Dict[str, float]

Convert RGB to CIE LCH color space.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'l': 0-100,    # Lightness
    'c': 0-100+,   # Chroma
    'h': 0-360     # Hue
}

Example:

1
2
3
4
from colorium import rgb_to_lch

lch = rgb_to_lch(255, 0, 0)
print(lch)  # {'l': 53.24, 'c': 104.55, 'h': 40.0}

lch_to_rgb

1
def lch_to_rgb(l: float, c: float, h: float) -> Dict[str, int]

Convert CIE LCH to RGB.

Parameters:

ParameterTypeRangeDescription
lfloat0-100Lightness
cfloat0-100+Chroma
hfloat0-360Hue

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import lch_to_rgb

rgb = lch_to_rgb(53.24, 104.55, 40.0)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

rgb_to_p3

1
def rgb_to_p3(r: int, g: int, b: int) -> Dict[str, float]

Convert sRGB to Display P3.

Parameters:

ParameterTypeRangeDescription
rint0-255Red component
gint0-255Green component
bint0-255Blue component

Returns:

1
2
3
4
5
{
    'r': 0-1,  # Red in P3
    'g': 0-1,  # Green in P3
    'b': 0-1   # Blue in P3
}

Example:

1
2
3
4
from colorium import rgb_to_p3

p3 = rgb_to_p3(255, 0, 0)
print(p3)  # {'r': 0.8226, 'g': 0.0331, 'b': 0.0171}

p3_to_rgb

1
def p3_to_rgb(r: float, g: float, b: float) -> Dict[str, int]

Convert Display P3 to sRGB.

Parameters:

ParameterTypeRangeDescription
rfloat0-1+Red in P3
gfloat0-1+Green in P3
bfloat0-1+Blue in P3

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import p3_to_rgb

rgb = p3_to_rgb(0.8226, 0.0331, 0.0171)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

NCOL Functions

hue_to_ncol

1
def hue_to_ncol(hue: float) -> str

Convert hue to NCOL notation.

Parameters:

ParameterTypeRangeDescription
huefloat0-360Hue angle

Returns: NCOL string (e.g., "R0", "Y50", "G100")

Example:

1
2
3
4
5
from colorium import hue_to_ncol

print(hue_to_ncol(0))    # R0
print(hue_to_ncol(30))   # R50
print(hue_to_ncol(90))   # Y50

ncol_to_rgb

1
def ncol_to_rgb(ncol: str, white: float, black: float) -> Dict[str, int]

Convert NCOL notation to RGB.

Parameters:

ParameterTypeRangeDescription
ncolstr-NCOL string (e.g., "R50", "Y20")
whitefloat0-1Whiteness
blackfloat0-1Blackness

Returns:

1
2
3
4
5
{
    'r': 0-255,  # Red
    'g': 0-255,  # Green
    'b': 0-255   # Blue
}

Example:

1
2
3
4
from colorium import ncol_to_rgb

rgb = ncol_to_rgb("R0", 0.0, 0.0)
print(rgb)  # {'r': 255, 'g': 0, 'b': 0}

Next Steps


Previous: Color Class Next: Filter Classes →

On this page
30 sections