v1.0.0
Color Functions API
This document covers all factory functions and conversion functions available in Colorium.
Factory Functions
from_string
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:
| Parameter | Type | Description |
|---|---|---|
| color_str | str | String representation of a color |
Returns: Color object or None if parsing fails
Example:
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
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:
| Parameter | Type | Description |
|---|---|---|
| hex_str | str | Hex string with or without # prefix |
Returns: Color object or None if invalid
Example:
from colorium import from_hex
red = from_hex("#FF0000")
green = from_hex("00FF00")
blue = from_hex("#00F") # shorthandfrom_rgb
def from_rgb(r: int, g: int, b: int, opacity: float = 1.0) -> ColorCreate a Color from RGB values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
from colorium import from_rgb
red = from_rgb(255, 0, 0)
semi_transparent = from_rgb(255, 0, 0, 0.5)from_hsl
def from_hsl(hue: float, sat: float, lightness: float, opacity: float = 1.0) -> ColorCreate a Color from HSL values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
| sat | float | 0-1 | Saturation |
| lightness | float | 0-1 | Lightness |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
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
def from_hwb(hue: float, white: float, black: float, opacity: float = 1.0) -> ColorCreate a Color from HWB values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
| white | float | 0-1 | Whiteness |
| black | float | 0-1 | Blackness |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
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
def from_cmyk(c: float, m: float, y: float, k: float, opacity: float = 1.0) -> ColorCreate a Color from CMYK values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| c | float | 0-1 | Cyan |
| m | float | 0-1 | Magenta |
| y | float | 0-1 | Yellow |
| k | float | 0-1 | Black |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
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
def from_oklch(l: float, c: float, h: float, opacity: float = 1.0) -> ColorCreate a Color from OKLCH values (CSS Color Level 4).
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-1 | Lightness |
| c | float | 0-0.4+ | Chroma |
| h | float | 0-360 | Hue |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
from colorium import from_oklch
color = from_oklch(0.5, 0.2, 180)from_lab
def from_lab(l: float, a: float, b: float, opacity: float = 1.0) -> ColorCreate a Color from CIE Lab* values (CSS Color Level 4).
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-100 | Lightness |
| a | float | -128 to 128 | a* axis |
| b | float | -128 to 128 | b* axis |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
from colorium import from_lab
color = from_lab(50, 20, -30)from_lch
def from_lch(l: float, c: float, h: float, opacity: float = 1.0) -> ColorCreate a Color from CIE LCH values (CSS Color Level 4).
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-100 | Lightness |
| c | float | 0-100+ | Chroma |
| h | float | 0-360 | Hue |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
from colorium import from_lch
color = from_lch(50, 30, 180)from_p3
def from_p3(r: float, g: float, b: float, opacity: float = 1.0) -> ColorCreate a Color from Display P3 values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | float | 0-1+ | Red in P3 space |
| g | float | 0-1+ | Green in P3 space |
| b | float | 0-1+ | Blue in P3 space |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Returns: Color object
Example:
from colorium import from_p3
color = from_p3(0.8, 0.3, 0.5)Color Space Conversion Functions
rgb_to_hsl
def rgb_to_hsl(r: int, g: int, b: int) -> Dict[str, float]Convert RGB values to HSL.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'h': 0-360, # Hue
's': 0-1, # Saturation
'l': 0-1 # Lightness
}Example:
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
def hsl_to_rgb(hue: float, sat: float, lightness: float) -> Dict[str, int]Convert HSL values to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
| sat | float | 0-1 | Saturation |
| lightness | float | 0-1 | Lightness |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_hwb(r: int, g: int, b: int) -> Dict[str, float]Convert RGB values to HWB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'h': 0-360, # Hue
'w': 0-1, # Whiteness
'b': 0-1 # Blackness
}Example:
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
def hwb_to_rgb(hue: float, white: float, black: float) -> Dict[str, int]Convert HWB values to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
| white | float | 0-1 | Whiteness |
| black | float | 0-1 | Blackness |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_cmyk(r: int, g: int, b: int) -> Dict[str, float]Convert RGB values to CMYK.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'c': 0-1, # Cyan
'm': 0-1, # Magenta
'y': 0-1, # Yellow
'k': 0-1 # Black
}Example:
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
def cmyk_to_rgb(c: float, m: float, y: float, k: float) -> Dict[str, int]Convert CMYK values to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| c | float | 0-1 | Cyan |
| m | float | 0-1 | Magenta |
| y | float | 0-1 | Yellow |
| k | float | 0-1 | Black |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_oklch(r: int, g: int, b: int) -> Dict[str, float]Convert RGB to OKLCH color space.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'l': 0-1, # Lightness
'c': 0-0.4+, # Chroma
'h': 0-360 # Hue
}Example:
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
def oklch_to_rgb(l: float, c: float, h: float) -> Dict[str, int]Convert OKLCH to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-1 | Lightness |
| c | float | 0-0.4+ | Chroma |
| h | float | 0-360 | Hue |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_lab(r: int, g: int, b: int) -> Dict[str, float]Convert RGB to CIE Lab* color space.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'l': 0-100, # Lightness
'a': -128 to 128, # a* axis
'b': -128 to 128 # b* axis
}Example:
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
def lab_to_rgb(l: float, a: float, b: float) -> Dict[str, int]Convert CIE Lab* to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-100 | Lightness |
| a | float | -128 to 128 | a* axis |
| b | float | -128 to 128 | b* axis |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_lch(r: int, g: int, b: int) -> Dict[str, float]Convert RGB to CIE LCH color space.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'l': 0-100, # Lightness
'c': 0-100+, # Chroma
'h': 0-360 # Hue
}Example:
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
def lch_to_rgb(l: float, c: float, h: float) -> Dict[str, int]Convert CIE LCH to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| l | float | 0-100 | Lightness |
| c | float | 0-100+ | Chroma |
| h | float | 0-360 | Hue |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def rgb_to_p3(r: int, g: int, b: int) -> Dict[str, float]Convert sRGB to Display P3.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | int | 0-255 | Red component |
| g | int | 0-255 | Green component |
| b | int | 0-255 | Blue component |
Returns:
{
'r': 0-1, # Red in P3
'g': 0-1, # Green in P3
'b': 0-1 # Blue in P3
}Example:
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
def p3_to_rgb(r: float, g: float, b: float) -> Dict[str, int]Convert Display P3 to sRGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| r | float | 0-1+ | Red in P3 |
| g | float | 0-1+ | Green in P3 |
| b | float | 0-1+ | Blue in P3 |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
def hue_to_ncol(hue: float) -> strConvert hue to NCOL notation.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
Returns: NCOL string (e.g., "R0", "Y50", "G100")
Example:
from colorium import hue_to_ncol
print(hue_to_ncol(0)) # R0
print(hue_to_ncol(30)) # R50
print(hue_to_ncol(90)) # Y50ncol_to_rgb
def ncol_to_rgb(ncol: str, white: float, black: float) -> Dict[str, int]Convert NCOL notation to RGB.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| ncol | str | - | NCOL string (e.g., "R50", "Y20") |
| white | float | 0-1 | Whiteness |
| black | float | 0-1 | Blackness |
Returns:
{
'r': 0-255, # Red
'g': 0-255, # Green
'b': 0-255 # Blue
}Example:
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
- Filter Classes — Filter API
- Utility Functions — Utility API
- Color Class — Color class API
Previous: Color Class Next: Filter Classes →