v1.0.0
Color Class API
The Color class is the core of Colorium. This document provides complete API reference for all methods and properties.
Constructor
Color(red: int, green: int, blue: int, opacity: float = 1.0)Creates a new Color instance with RGB values.
Parameters:
| Parameter | Type | Range | Description |
|---|---|---|---|
| red | int | 0-255 | Red component |
| green | int | 0-255 | Green component |
| blue | int | 0-255 | Blue component |
| opacity | float | 0-1 | Opacity (default: 1.0) |
Example:
from colorium import Color
red = Color(255, 0, 0)
semi_transparent = Color(255, 0, 0, 0.5)Properties
RGB Properties
color.red: int
color.green: int
color.blue: int
color.opacity: floatGet or set RGB components.
| Property | Type | Range | Description |
|---|---|---|---|
| red | int | 0-255 | Red component |
| green | int | 0-255 | Green component |
| blue | int | 0-255 | Blue component |
| opacity | float | 0-1 | Opacity |
Example:
from colorium import Color
color = Color(100, 150, 200)
color.red = 255
color.green = 0
color.blue = 0
print(color.opacity) # 1.0HSL Properties
color.hue: float
color.sat: float
color.lightness: floatGet derived HSL values (read-only).
| Property | Type | Range | Description |
|---|---|---|---|
| hue | float | 0-360 | Hue angle |
| sat | float | 0-1 | Saturation |
| lightness | float | 0-1 | Lightness |
Example:
from colorium import Color
color = Color(255, 0, 0)
print(color.hue) # 0
print(color.sat) # 1.0
print(color.lightness) # 0.5HWB Properties
color.whiteness: float
color.blackness: floatGet derived HWB values (read-only).
| Property | Type | Range | Description |
|---|---|---|---|
| whiteness | float | 0-1 | Whiteness |
| blackness | float | 0-1 | Blackness |
Example:
from colorium import Color
color = Color(255, 0, 0)
print(color.whiteness) # 0.0
print(color.blackness) # 0.0CMYK Properties
color.cyan: float
color.magenta: float
color.yellow: float
color.black: floatGet derived CMYK values (read-only).
| Property | Type | Range | Description |
|---|---|---|---|
| cyan | float | 0-1 | Cyan |
| magenta | float | 0-1 | Magenta |
| yellow | float | 0-1 | Yellow |
| black | float | 0-1 | Black |
Example:
from colorium import Color
color = Color(255, 0, 0)
print(color.cyan) # 0.0
print(color.magenta) # 1.0
print(color.yellow) # 1.0
print(color.black) # 0.0NCOL Property
color.ncol: strGet NCOL notation string (read-only).
Example:
from colorium import Color
color = Color(255, 0, 0)
print(color.ncol) # R0
color = Color(0, 255, 0)
print(color.ncol) # G0String Methods
to_rgb_string
def to_rgb_string(self) -> strConvert to CSS RGB string format.
Returns: rgb(255, 0, 0)
Example:
color = Color(255, 0, 0)
print(color.to_rgb_string()) # rgb(255, 0, 0)to_rgba_string
def to_rgba_string(self) -> strConvert to CSS RGBA string format with opacity.
Returns: rgba(255, 0, 0, 1.00)
Example:
color = Color(255, 0, 0, 0.5)
print(color.to_rgba_string()) # rgba(255, 0, 0, 0.50)to_hsl_string
def to_hsl_string(self) -> strConvert to CSS HSL string format.
Returns: hsl(0, 100%, 50%)
Example:
color = Color(255, 0, 0)
print(color.to_hsl_string()) # hsl(0, 100%, 50%)to_hsla_string
def to_hsla_string(self) -> strConvert to CSS HSLA string format with opacity.
Returns: hsla(0, 100%, 50%, 1.00)
Example:
color = Color(255, 0, 0, 0.5)
print(color.to_hsla_string()) # hsla(0, 100%, 50%, 0.50)to_hwb_string
def to_hwb_string(self) -> strConvert to CSS HWB string format.
Returns: hwb(0, 0%, 0%)
Example:
color = Color(255, 0, 0)
print(color.to_hwb_string()) # hwb(0, 0%, 0%)to_hwba_string
def to_hwba_string(self) -> strConvert to CSS HWBA string format with opacity.
Returns: hwba(0, 0%, 0%, 1.00)
Example:
color = Color(255, 0, 0, 0.5)
print(color.to_hwba_string()) # hwba(0, 0%, 0%, 0.50)to_cmyk_string
def to_cmyk_string(self) -> strConvert to CMYK string format.
Returns: cmyk(0%, 100%, 100%, 0%)
Example:
color = Color(255, 0, 0)
print(color.to_cmyk_string()) # cmyk(0%, 100%, 100%, 0%)to_hex_string
def to_hex_string(self) -> strConvert to hexadecimal color string.
Returns: #FF0000
Example:
color = Color(255, 0, 0)
print(color.to_hex_string()) # #FF0000to_oklch_string
def to_oklch_string(self) -> strConvert to OKLCH string format (CSS Color Level 4).
Returns: oklch(62.8% 0.258 29.2)
Example:
color = Color(255, 0, 0)
print(color.to_oklch_string()) # oklch(62.8% 0.258 29.2)to_lab_string
def to_lab_string(self) -> strConvert to CIE Lab* string format (CSS Color Level 4).
Returns: lab(53.2% 80.1 67.2)
Example:
color = Color(255, 0, 0)
print(color.to_lab_string()) # lab(53.2% 80.1 67.2)to_lch_string
def to_lch_string(self) -> strConvert to CIE LCH string format (CSS Color Level 4).
Returns: lch(53.2% 104.5 40.0)
Example:
color = Color(255, 0, 0)
print(color.to_lch_string()) # lch(53.2% 104.5 40.0)to_p3_string
def to_p3_string(self) -> strConvert to Display P3 string format.
Returns: color(display-p3 0.823 0.033 0.017)
Example:
color = Color(255, 0, 0)
print(color.to_p3_string()) # color(display-p3 0.823 0.033 0.017)to_ncol_string
def to_ncol_string(self) -> strConvert to NCOL notation string.
Returns: ncol(R0, 0%, 0%)
Example:
color = Color(255, 0, 0)
print(color.to_ncol_string()) # ncol(R0, 0%, 0%)to_name
def to_name(self) -> strGet CSS color name if this color matches a named color.
Returns: Color name or empty string
Example:
color = Color(255, 0, 0)
print(color.to_name()) # Red
color = Color(123, 45, 67)
print(color.to_name()) # ""Dictionary Methods
to_rgb
def to_rgb(self) -> Dict[str, Any]Get RGB values as a dictionary.
Returns:
{
'r': 255,
'g': 0,
'b': 0,
'a': 1.0
}Example:
color = Color(255, 0, 0)
rgb = color.to_rgb()
print(rgb['r']) # 255
print(rgb['g']) # 0
print(rgb['b']) # 0
print(rgb['a']) # 1.0to_hsl
def to_hsl(self) -> Dict[str, float]Get HSL values as a dictionary.
Returns:
{
'h': 0,
's': 1.0,
'l': 0.5,
'a': 1.0
}Example:
color = Color(255, 0, 0)
hsl = color.to_hsl()
print(hsl['h']) # 0
print(hsl['s']) # 1.0
print(hsl['l']) # 0.5
print(hsl['a']) # 1.0to_hwb
def to_hwb(self) -> Dict[str, float]Get HWB values as a dictionary.
Returns:
{
'h': 0,
'w': 0.0,
'b': 0.0,
'a': 1.0
}Example:
color = Color(255, 0, 0)
hwb = color.to_hwb()
print(hwb['h']) # 0
print(hwb['w']) # 0.0
print(hwb['b']) # 0.0
print(hwb['a']) # 1.0to_cmyk
def to_cmyk(self) -> Dict[str, float]Get CMYK values as a dictionary.
Returns:
{
'c': 0.0,
'm': 1.0,
'y': 1.0,
'k': 0.0,
'a': 1.0
}Example:
color = Color(255, 0, 0)
cmyk = color.to_cmyk()
print(cmyk['c']) # 0.0
print(cmyk['m']) # 1.0
print(cmyk['y']) # 1.0
print(cmyk['k']) # 0.0
print(cmyk['a']) # 1.0to_oklch
def to_oklch(self) -> Dict[str, float]Get OKLCH values as a dictionary.
Returns:
{
'l': 0.628,
'c': 0.258,
'h': 29.2
}Example:
color = Color(255, 0, 0)
oklch = color.to_oklch()
print(oklch['l']) # 0.628
print(oklch['c']) # 0.258
print(oklch['h']) # 29.2to_lab
def to_lab(self) -> Dict[str, float]Get CIE Lab* values as a dictionary.
Returns:
{
'l': 53.24,
'a': 80.09,
'b': 67.20
}Example:
color = Color(255, 0, 0)
lab = color.to_lab()
print(lab['l']) # 53.24
print(lab['a']) # 80.09
print(lab['b']) # 67.20to_lch
def to_lch(self) -> Dict[str, float]Get CIE LCH values as a dictionary.
Returns:
{
'l': 53.24,
'c': 104.55,
'h': 40.0
}Example:
color = Color(255, 0, 0)
lch = color.to_lch()
print(lch['l']) # 53.24
print(lch['c']) # 104.55
print(lch['h']) # 40.0to_p3
def to_p3(self) -> Dict[str, float]Get Display P3 values as a dictionary.
Returns:
{
'r': 0.8226,
'g': 0.0331,
'b': 0.0171
}Example:
color = Color(255, 0, 0)
p3 = color.to_p3()
print(p3['r']) # 0.8226
print(p3['g']) # 0.0331
print(p3['b']) # 0.0171to_ncol
def to_ncol(self) -> Dict[str, Any]Get NCOL values as a dictionary.
Returns:
{
'ncol': 'R0',
'w': 0.0,
'b': 0.0,
'a': 1.0
}Example:
color = Color(255, 0, 0)
ncol = color.to_ncol()
print(ncol['ncol']) # R0
print(ncol['w']) # 0.0
print(ncol['b']) # 0.0
print(ncol['a']) # 1.0Manipulation Methods
lighter
def lighter(self, amount: float = 0.1) -> NoneIncrease lightness by a given amount.
| Parameter | Type | Range | Description |
|---|---|---|---|
| amount | float | 0-1 | Amount to increase (default: 0.1) |
Example:
color = Color(100, 150, 200)
color.lighter(0.2)
print(color.to_hex_string()) # Brighter versiondarker
def darker(self, amount: float = 0.1) -> NoneDecrease lightness by a given amount.
| Parameter | Type | Range | Description |
|---|---|---|---|
| amount | float | 0-1 | Amount to decrease (default: 0.1) |
Example:
color = Color(100, 150, 200)
color.darker(0.2)
print(color.to_hex_string()) # Darker versionsaturate
def saturate(self, amount: float = 0.1) -> NoneIncrease saturation by a given amount.
| Parameter | Type | Range | Description |
|---|---|---|---|
| amount | float | 0-1 | Amount to increase (default: 0.1) |
Example:
color = Color(100, 150, 200)
color.saturate(0.3)
print(color.to_hex_string()) # More vividdesaturate
def desaturate(self, amount: float = 0.1) -> NoneDecrease saturation by a given amount.
| Parameter | Type | Range | Description |
|---|---|---|---|
| amount | float | 0-1 | Amount to decrease (default: 0.1) |
Example:
color = Color(100, 150, 200)
color.desaturate(0.3)
print(color.to_hex_string()) # More mutedblend
def blend(self, other: 'Color', ratio: float = 0.5) -> 'Color'Blend this color with another color.
| Parameter | Type | Range | Description |
|---|---|---|---|
| other | Color | - | Other color to blend with |
| ratio | float | 0-1 | Blend ratio (default: 0.5) |
Returns: New Color object
Example:
color1 = Color(255, 0, 0)
color2 = Color(0, 0, 255)
result = color1.blend(color2, 0.5)
print(result.to_hex_string()) # #7F007Fclone
def clone(self) -> 'Color'Create an independent copy of this color.
Returns: New Color object
Example:
original = Color(255, 0, 0)
copy = original.clone()
copy.lighter(0.5)
print(original.to_hex_string()) # #FF0000
print(copy.to_hex_string()) # Brighter versionis_dark
def is_dark(self, threshold: int = 128) -> boolCheck if color is dark based on perceived luminance.
| Parameter | Type | Range | Description |
|---|---|---|---|
| threshold | int | 0-255 | Luminance threshold (default: 128) |
Returns: True if dark, False otherwise
Example:
color = Color(0, 0, 0)
print(color.is_dark()) # True
color = Color(255, 255, 255)
print(color.is_dark()) # FalseDistance Methods
delta_e
def delta_e(self, other: 'Color', method: str = "cie2000") -> floatCalculate color difference (Delta E) between two colors.
| Parameter | Type | Description |
|---|---|---|
| other | Color | Other color to compare |
| method | str | Distance algorithm (default: "cie2000") |
Methods: "cie76", "cie94", "cie2000"
Returns: Delta E value
Example:
color1 = Color(100, 150, 200)
color2 = Color(120, 130, 180)
cie76 = color1.delta_e(color2, "cie76")
cie94 = color1.delta_e(color2, "cie94")
cie2000 = color1.delta_e(color2, "cie2000")similarity
def similarity(self, other: 'Color', method: str = "cie2000") -> floatCalculate perceptual similarity between two colors.
| Parameter | Type | Description |
|---|---|---|
| other | Color | Other color to compare |
| method | str | Distance algorithm (default: "cie2000") |
Returns: Similarity score (0.0 to 1.0)
Example:
color1 = Color(100, 150, 200)
color2 = Color(120, 130, 180)
similarity = color1.similarity(color2)
print(similarity) # 0.85is_similar_to
def is_similar_to(self, other: 'Color', threshold: float = 0.8, method: str = "cie2000") -> boolCheck if two colors are similar.
| Parameter | Type | Description |
|---|---|---|
| other | Color | Other color to compare |
| threshold | float | Similarity threshold (default: 0.8) |
| method | str | Distance algorithm (default: "cie2000") |
Returns: True if similar, False otherwise
Example:
color1 = Color(100, 150, 200)
color2 = Color(120, 130, 180)
if color1.is_similar_to(color2, threshold=0.8):
print("Colors are similar")Magic Methods
str
def __str__(self) -> strHuman-readable string representation.
Returns: RGB string like rgb(255, 0, 0)
Example:
color = Color(255, 0, 0)
print(str(color)) # rgb(255, 0, 0)repr
def __repr__(self) -> strDeveloper-friendly representation.
Returns: Constructor string
Example:
color = Color(255, 0, 0)
print(repr(color)) # Color(255, 0, 0, opacity=1.00)eq
def __eq__(self, other: object) -> boolCheck if two colors are equal.
Returns: True if equal, False otherwise
Example:
color1 = Color(255, 0, 0)
color2 = Color(255, 0, 0)
color3 = Color(0, 255, 0)
print(color1 == color2) # True
print(color1 == color3) # FalseNext Steps
- Color Functions — Factory functions
- Filter Classes — Filter API
- Utility Functions — Utility API
Previous: API Reference Next: Color Functions →