D
Documentation
v1.0.0
Color Creation
Colorium provides multiple ways to create colors, from simple RGB values to complex color space conversions. This guide covers all creation methods.
Basic Creation Methods
From RGB Values
The most fundamental way to create a color is using RGB values.
1
2
3
4
5
6
7
8
9
10
11
12
from colorium import Color
# Create from RGB (0-255)
red = Color(255, 0, 0)
green = Color(0, 255, 0)
blue = Color(0, 0, 255)
# With opacity
semi_transparent = Color(255, 0, 0, 0.5)
# Values are automatically clamped
clamped = Color(300, -10, 100) # Becomes (255, 0, 100)From Hex Strings
Hex strings are commonly used in web development.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from colorium import from_hex
# With hash prefix
red = from_hex("#FF0000")
blue = from_hex("#0000FF")
# Without hash prefix
green = from_hex("00FF00")
# Shorthand hex
red_short = from_hex("#F00") # Becomes #FF0000
# Invalid hex returns None
invalid = from_hex("GGGGGG") # NoneFrom Named Colors
Colorium includes 147 CSS named colors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from colorium import from_string
# Basic colors
red = from_string("red")
blue = from_string("blue")
green = from_string("green")
# Extended colors
crimson = from_string("crimson")
navy = from_string("navy")
olive = from_string("olive")
teal = from_string("teal")
# Complex names
light_steel_blue = from_string("lightsteelblue")
medium_slate_blue = from_string("mediumslateblue")
# Case insensitive
RED = from_string("RED")
Red = from_string("Red")From Color Spaces
Create colors directly in any supported color space.
HSL (Hue, Saturation, Lightness)
1
2
3
4
5
6
7
8
9
10
11
12
from colorium import from_hsl
# Pure colors
red = from_hsl(0, 1.0, 0.5)
green = from_hsl(120, 1.0, 0.5)
blue = from_hsl(240, 1.0, 0.5)
# Pastel colors
pastel_blue = from_hsl(200, 0.3, 0.7)
# Dark colors
dark_green = from_hsl(120, 0.8, 0.2)HWB (Hue, Whiteness, Blackness)
1
2
3
4
5
6
7
8
9
10
11
12
from colorium import from_hwb
# Pure colors
red = from_hwb(0, 0, 0)
green = from_hwb(120, 0, 0)
blue = from_hwb(240, 0, 0)
# Tinted colors (with white)
pink = from_hwb(0, 0.5, 0)
# Shaded colors (with black)
dark_red = from_hwb(0, 0, 0.5)CMYK (Cyan, Magenta, Yellow, Black)
1
2
3
4
5
6
7
8
9
10
from colorium import from_cmyk
# Process colors
red = from_cmyk(0, 1.0, 1.0, 0)
green = from_cmyk(1.0, 0, 1.0, 0)
blue = from_cmyk(1.0, 1.0, 0, 0)
# Black variants
black = from_cmyk(0, 0, 0, 1.0)
rich_black = from_cmyk(0.5, 0.5, 0.5, 1.0)OKLCH (Modern CSS Color Level 4)
1
2
3
4
5
6
from colorium import from_oklch
# Create OKLCH colors
color1 = from_oklch(0.5, 0.2, 180)
color2 = from_oklch(0.7, 0.1, 45)
color3 = from_oklch(0.3, 0.3, 300)CIE Lab*
1
2
3
4
5
6
from colorium import from_lab
# LAB colors
color1 = from_lab(50, 20, -30)
color2 = from_lab(70, -10, 40)
color3 = from_lab(30, 50, 20)CIE LCH
1
2
3
4
5
6
from colorium import from_lch
# LCH colors
color1 = from_lch(50, 30, 180)
color2 = from_lch(70, 20, 45)
color3 = from_lch(30, 40, 300)Display P3 (Wide Gamut)
1
2
3
4
5
6
from colorium import from_p3
# P3 colors (can exceed 1.0 for wide gamut)
color1 = from_p3(0.8, 0.3, 0.5)
color2 = from_p3(1.2, 0.6, 0.2)
color3 = from_p3(0.2, 0.9, 0.8)From Strings
The universal parser supports many formats.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from colorium import from_string
# Named colors
red = from_string("red")
# Hex colors
hex_color = from_string("#FF0000")
# RGB strings
rgb_color = from_string("rgb(255, 0, 0)")
rgba_color = from_string("rgba(255, 0, 0, 0.5)")
# HSL strings
hsl_color = from_string("hsl(0, 100%, 50%)")
hsla_color = from_string("hsla(0, 100%, 50%, 0.5)")
# HWB strings
hwb_color = from_string("hwb(0, 0%, 0%)")
# CMYK strings
cmyk_color = from_string("cmyk(0%, 100%, 100%, 0%)")
# NCOL strings
ncol_color = from_string("ncol(R50, 0%, 0%)")Using Color Constants
Colorium provides many pre-defined color constants.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from colorium import (
# Primary colors
RED, GREEN, BLUE,
# Secondary colors
YELLOW, CYAN, MAGENTA,
# Neutral colors
BLACK, WHITE, GRAY,
# Semantic colors
SUCCESS, ERROR, WARNING, INFO,
# Brand colors
FACEBOOK_BLUE, TWITTER_BLUE,
GOOGLE_RED, GOOGLE_GREEN,
GOOGLE_YELLOW, GOOGLE_BLUE,
# Extended colors
CRIMSON, NAVY, OLIVE, TEAL,
CORAL, INDIGO, LAVENDER, PLUM
)
# Use constants directly
primary = BLUE
secondary = GRAY
status = SUCCESS if valid else ERROR
# Constants are Color objects
print(RED.to_hex_string()) # #FF0000
print(BLUE.to_rgb_string()) # rgb(0, 0, 255)Advanced Creation
From Dictionaries
1
2
3
4
5
6
7
8
9
10
from colorium import Color
# From RGB dict
rgb_dict = {'r': 255, 'g': 0, 'b': 0}
color = Color(rgb_dict['r'], rgb_dict['g'], rgb_dict['b'])
# From HSL dict
hsl_dict = {'h': 0, 's': 1.0, 'l': 0.5}
from colorium import from_hsl
color = from_hsl(hsl_dict['h'], hsl_dict['s'], hsl_dict['l'])From Other Color Objects
1
2
3
4
5
6
7
8
from colorium import Color
# Clone a color
original = Color(255, 0, 0)
clone = original.clone() # Independent copy
# Copy via constructor
copy = Color(original.red, original.green, original.blue)From RGB Percentages
1
2
3
4
5
6
7
from colorium import Color
# Convert percentages to 0-255
def from_percentages(r, g, b):
return Color(int(r * 255), int(g * 255), int(b * 255))
color = from_percentages(1.0, 0.5, 0.0) # #FF8000Input Validation
Colorium automatically validates and clamps values.
RGB Validation
1
2
3
4
5
6
7
8
9
10
11
from colorium import Color
# Values are clamped to 0-255
color = Color(300, -10, 100)
print(color.red) # 255
print(color.green) # 0
print(color.blue) # 100
# Opacity clamped to 0-1
color = Color(255, 0, 0, 2.5)
print(color.opacity) # 1.0Space Validation
1
2
3
4
from colorium import from_hsl
# HSL values are clamped
color = from_hsl(400, 2.0, 1.5) # Clamped to (40, 1.0, 1.0)String Validation
1
2
3
4
5
from colorium import from_string
# Invalid strings return None
invalid = from_string("not a color") # None
empty = from_string("") # NoneCommon Patterns
Creating a Color Palette
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from colorium import Color, from_hsl
def create_palette(base_hue, count=5):
"""Create a palette from a base hue"""
palette = []
for i in range(count):
hue = (base_hue + i * 30) % 360
saturation = 0.8 - i * 0.1
lightness = 0.3 + i * 0.1
color = from_hsl(hue, saturation, lightness)
palette.append(color)
return palette
# Create a palette
palette = create_palette(200, 5)
for i, color in enumerate(palette):
print(f"Color {i+1}: {color.to_hex_string()}")Creating Named Color Collections
1
2
3
4
5
6
7
8
9
10
from colorium import from_string
# Create a collection
colors = {
name: from_string(name)
for name in ['red', 'blue', 'green', 'yellow', 'purple']
}
# Access by name
print(colors['red'].to_hex_string())Creating Colors from User Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from colorium import from_string, Color
def parse_color_input(input_str):
"""Parse color input from various formats"""
# Try named color first
color = from_string(input_str)
if color:
return color
# Try hex with # prefix
if input_str.startswith('#'):
return from_string(input_str)
# Try RGB values
try:
r, g, b = map(int, input_str.split(','))
return Color(r, g, b)
except:
return None
# Examples
color1 = parse_color_input('red')
color2 = parse_color_input('#FF0000')
color3 = parse_color_input('255,0,0')Performance Tips
Reuse Colors
1
2
3
4
5
6
7
from colorium import RED, BLUE
# Good - reuse constants
color = RED
# Avoid - create new color each time
color = Color(255, 0, 0)Use Factory Functions
1
2
3
4
5
6
7
from colorium import from_string, from_hsl
# Good - clear intent
color = from_string("crimson")
# Also good
color = from_hsl(0, 1.0, 0.5)Avoid Unnecessary Conversion
1
2
3
4
5
6
7
8
9
10
from colorium import Color
color = Color(255, 0, 0)
# Good - access directly
print(color.red)
# Avoid - unnecessary conversion
rgb = color.to_rgb()
print(rgb['r'])Next Steps
- Color Manipulation — Working with colors
- Color Conversion — Converting between spaces
- Color Spaces — Understanding color spaces
Previous: Color Spaces Next: Color Manipulation →
On this page
31 sections