Named Colors

Colorium includes all 147 CSS named colors with case-insensitive lookup. This guide covers how to use named colors effectively.

Overview

Named colors provide a convenient way to reference colors without remembering hex codes or RGB values.

Benefits

  • Human-readable color names
  • Case-insensitive lookup
  • Quick color selection
  • Easy to remember and use
  • Standard CSS color names

Basic Usage

Creating Named Colors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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")

# Case insensitive
RED = from_string("RED")
ReD = from_string("ReD")
red_alt = from_string("Red")

Getting Color Name

1
2
3
4
5
6
7
8
9
10
11
from colorium import Color

color = Color(255, 0, 0)
name = color.to_name()
print(name)  # Red

color2 = Color(100, 149, 237)
print(color2.to_name())  # CornflowerBlue

color3 = Color(123, 45, 67)
print(color3.to_name())  # "" (empty if no match)

Color Categories

Primary Colors

1
2
3
4
5
6
7
8
9
10
from colorium import from_string

primary = [
    from_string("red"),
    from_string("green"),
    from_string("blue")
]

for color in primary:
    print(color.to_hex_string())

Secondary Colors

1
2
3
4
5
6
7
from colorium import from_string

secondary = [
    from_string("yellow"),
    from_string("cyan"),
    from_string("magenta")
]

Neutral Colors

1
2
3
4
5
6
7
8
from colorium import from_string

neutrals = [
    from_string("black"),
    from_string("white"),
    from_string("gray"),
    from_string("silver")
]

Extended Colors

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
from colorium import from_string

# Reds
reds = [
    from_string("crimson"),
    from_string("maroon"),
    from_string("firebrick"),
    from_string("indianred")
]

# Blues
blues = [
    from_string("navy"),
    from_string("royalblue"),
    from_string("cornflowerblue"),
    from_string("steelblue")
]

# Greens
greens = [
    from_string("olive"),
    from_string("teal"),
    from_string("forestgreen"),
    from_string("seagreen")
]

Practical Examples

Color Name Detection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from colorium import Color, from_string

def detect_color_name(color):
    """Detect color name with fallback"""
    name = color.to_name()
    if name:
        return name
    return f"Custom-{color.to_hex_string()}"

# Examples
colors = [
    Color(255, 0, 0),
    Color(100, 149, 237),
    Color(123, 45, 67),
    from_string("crimson")
]

for color in colors:
    print(f"{color.to_hex_string()}{detect_color_name(color)}")

Finding Named Color Matches

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
32
from colorium import Color, from_string

def find_named_color_matches(target, threshold=0.9):
    """Find named colors similar to target"""
    # List of all named colors to check
    common_names = [
        "red", "green", "blue", "yellow", "cyan", "magenta",
        "black", "white", "gray", "silver",
        "crimson", "navy", "olive", "teal",
        "coral", "indigo", "lavender", "plum",
        "gold", "orange", "pink", "purple"
    ]

    matches = []
    for name in common_names:
        named_color = from_string(name)
        similarity = target.similarity(named_color)
        if similarity >= threshold:
            matches.append((name, similarity))

    # Sort by similarity
    matches.sort(key=lambda x: x[1], reverse=True)
    return matches

# Example
target = Color(200, 50, 50)
matches = find_named_color_matches(target)

print(f"Target: {target.to_hex_string()}")
print("Named color matches:")
for name, similarity in matches:
    print(f"  {name} ({similarity:.2f})")

Color Name Validation

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

def is_valid_named_color(name):
    """Check if a name is a valid named color"""
    try:
        color = from_string(name)
        return color is not None
    except:
        return False

# Test names
test_names = [
    "red", "blue", "crimson",
    "invalid_color", "not_a_color",
    "navy", "olive"
]

for name in test_names:
    valid = is_valid_named_color(name)
    print(f"'{name}': {'✓ Valid' if valid else '✗ Invalid'}")

Color Name Constants

Pre-defined Constants

1
2
3
4
5
6
7
8
9
10
11
12
from colorium import (
    RED, GREEN, BLUE,
    YELLOW, CYAN, MAGENTA,
    BLACK, WHITE, GRAY,
    CRIMSON, NAVY, OLIVE, TEAL,
    CORAL, INDIGO, LAVENDER, PLUM,
    SUCCESS, ERROR, WARNING, INFO
)

print(RED.to_hex_string())      # #FF0000
print(BLUE.to_hex_string())     # #0000FF
print(SUCCESS.to_hex_string())  # #00FF00

Creating Collections

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

def create_color_collection(names):
    """Create a collection of named colors"""
    colors = {}
    for name in names:
        colors[name] = from_string(name)
    return colors

# Create collection
warm_colors = create_color_collection([
    "red", "orange", "yellow", "coral", "gold"
])

cool_colors = create_color_collection([
    "blue", "cyan", "teal", "navy", "lavender"
])

for name, color in warm_colors.items():
    print(f"{name}: {color.to_hex_string()}")

Complete Color List

All 147 CSS Named Colors

1
2
3
4
5
6
from colorium import COLOR_NAMES, COLOR_HEXES

# Access all named colors
for i, name in enumerate(COLOR_NAMES):
    hex_val = COLOR_HEXES[i]
    print(f"{name}: #{hex_val}")

Color Name Groups

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from colorium import COLOR_NAMES, COLOR_HEXES

def group_colors_by_category():
    """Group named colors by category"""
    groups = {
        'reds': [],
        'blues': [],
        'greens': [],
        'yellows': [],
        'purples': [],
        'neutrals': [],
        'others': []
    }

    for i, name in enumerate(COLOR_NAMES):
        color = from_string(name)
        hex_val = color.to_hex_string().lower()

        # Categorize based on dominant color
        r, g, b = color.red, color.green, color.blue
        if r > g and r > b:
            groups['reds'].append(name)
        elif b > r and b > g:
            groups['blues'].append(name)
        elif g > r and g > b:
            groups['greens'].append(name)
        elif r > 200 and g > 200:
            groups['yellows'].append(name)
        elif r > 100 and b > 100:
            groups['purples'].append(name)
        elif r == g == b:
            groups['neutrals'].append(name)
        else:
            groups['others'].append(name)

    return groups

# Group colors
groups = group_colors_by_category()

for category, colors in groups.items():
    print(f"\n{category.upper()}:")
    for name in colors[:5]:  # Show first 5
        color = from_string(name)
        print(f"  {name}: {color.to_hex_string()}")

Common Patterns

Converting Between Names and Hex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from colorium import from_string, Color

def name_to_hex(name):
    """Convert named color to hex"""
    color = from_string(name)
    if color:
        return color.to_hex_string()
    return None

def hex_to_name(hex_str):
    """Convert hex to closest named color"""
    color = from_string(hex_str)
    if color:
        name = color.to_name()
        if name:
            return name
    return None

# Examples
print(name_to_hex("crimson"))   # #DC143C
print(hex_to_name("#DC143C"))   # Crimson

Creating Color Name Dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from colorium import COLOR_NAMES, COLOR_HEXES, from_string

def create_color_dictionary():
    """Create a dictionary of named colors"""
    color_dict = {}
    for i, name in enumerate(COLOR_NAMES):
        hex_val = COLOR_HEXES[i]
        color = from_string(name)
        color_dict[name] = {
            'hex': f'#{hex_val}',
            'rgb': color.to_rgb()
        }
    return color_dict

# Get color dictionary
colors = create_color_dictionary()

# Access a color
crimson_info = colors['Crimson']
print(f"Crimson hex: {crimson_info['hex']}")
print(f"Crimson RGB: {crimson_info['rgb']}")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from colorium import COLOR_NAMES, from_string

def search_colors(pattern):
    """Search for colors by name pattern"""
    results = []
    pattern = pattern.lower()

    for name in COLOR_NAMES:
        if pattern in name.lower():
            results.append(name)

    return results

# Search for colors
results = search_colors("blue")

print("Colors containing 'blue':")
for name in results:
    color = from_string(name)
    print(f"  {name}: {color.to_hex_string()}")

Best Practices

Use Constants for Common Colors

1
2
3
4
5
6
7
8
9
from colorium import RED, BLUE, SUCCESS, ERROR

# Good - use constants
primary = BLUE
status = SUCCESS if valid else ERROR

# Avoid - unnecessary string lookup
primary = from_string("blue")
status = from_string("success") if valid else from_string("error")

Cache Named Color Lookups

1
2
3
4
5
6
7
8
9
10
11
from colorium import from_string
from functools import lru_cache

@lru_cache(maxsize=200)
def get_named_color(name):
    """Cached named color lookup"""
    return from_string(name)

# Fast repeated lookups
red1 = get_named_color("red")
red2 = get_named_color("red")  # Uses cache

Handle Unknown Colors Gracefully

1
2
3
4
5
6
7
8
9
10
11
12
13
from colorium import from_string

def safe_get_color(name, default=None):
    """Safely get named color with fallback"""
    color = from_string(name)
    if color is None:
        return default or Color(0, 0, 0)
    return color

# Usage
color1 = safe_get_color("red")        # Returns red
color2 = safe_get_color("invalid")    # Returns black
color3 = safe_get_color("invalid", Color(255, 255, 255))  # Returns white

Next Steps


Previous: Color Similarity Next: Advanced Topics →

On this page
29 sections