colors

Color system builders for Tailwind CSS utilities

Color Enums

Define enums for the 22 Tailwind color families and 11 shade values:


ColorFamily


def ColorFamily(
    args:VAR_POSITIONAL, kwds:VAR_KEYWORD
):

Tailwind CSS color families.


ColorShade


def ColorShade(
    args:VAR_POSITIONAL, kwds:VAR_KEYWORD
):

Tailwind CSS color shade values.

Special Color Values

Define special color values that are available in addition to the color families:

Color Value Types

Define types for color values:

Color Validation

Functions to validate and parse color values:


is_valid_color_family


def is_valid_color_family(
    value:str, # The value to check
)->bool: # True if value is a valid color family

Check if a value is a valid Tailwind color family.


is_valid_shade


def is_valid_shade(
    value:str, # The value to check
)->bool: # True if value is a valid shade

Check if a value is a valid Tailwind shade.


parse_color_spec


def parse_color_spec(
    value:Union, # The color specification to parse
)->tuple: # Tuple of (color_family, shade) or (special_color, None)

Parse a color specification into family and shade.

Examples: - “red-500” -> (“red”, “500”) - (“red”, “500”) -> (“red”, “500”) - “transparent” -> (“transparent”, None)

Colored Utility Class

A utility class that supports Tailwind colors with opacity:


ColoredUtility


def ColoredUtility(
    prefix:str, # The utility prefix (e.g., 'bg', 'text', 'border')
    color:Union=None, # The color value
    opacity:Union=None, # Optional opacity value (0-100 or arbitrary)
):

Utility class with color and opacity support.

Colored Factory

Factory for creating color-based utilities with convenient API:


ColoredFactory


def ColoredFactory(
    prefix:str, # The utility prefix (e.g., 'bg', 'text', 'border')
    doc:Optional=None, # Optional documentation string
):

Factory for creating color-based utilities.

Color Family Proxy

A proxy class to handle color family attribute access (e.g., bg.red.500):


ColorFamilyProxy


def ColorFamilyProxy(
    prefix:str, # The utility prefix
    color_family:str, # The color family name
):

Proxy for accessing color shades via dot notation.

Examples

Test the color system with various use cases:

# Test basic color usage
bg = ColoredFactory("bg", "Background color utilities")
print(bg.red._500)

# Test standard color-shade combinations
assert str(bg.red._500) == "bg-red-500"
assert str(bg.blue._300) == "bg-blue-300"
assert str(bg.green._950) == "bg-green-950"
bg-red-500
# Test color family proxy access
assert str(bg.red._500) == "bg-red-500"
assert str(bg.blue._300) == "bg-blue-300"
assert str(bg.slate._950) == "bg-slate-950"

# Test with ColorShade enum
assert str(bg.red(ColorShade.SHADE_500)) == "bg-red-500"
# Test special colors
assert str(bg.transparent) == "bg-transparent"
assert str(bg.black) == "bg-black"
assert str(bg.white) == "bg-white"
assert str(bg.current) == "bg-current"
assert str(bg.inherit) == "bg-inherit"
# Test opacity modifiers
assert str(bg.red._500.opacity(50)) == "bg-red-500/50"
assert str(bg.blue._300.opacity(75)) == "bg-blue-300/75"
assert str(bg.black.opacity(10)) == "bg-black/10"

# Test opacity with arbitrary values
assert str(bg.red._500.opacity("[0.87]")) == "bg-red-500/[0.87]"
# Test arbitrary color values
assert str(bg("#ff0000")) == "bg-[#ff0000]"
assert str(bg("rgb(255, 0, 0)")) == "bg-[rgb(255, 0, 0)]"
assert str(bg("hsl(0, 100%, 50%)")) == "bg-[hsl(0, 100%, 50%)]"
# Test CSS custom properties
assert str(bg("--custom-bg")) == "bg-(--custom-bg)"
assert str(bg("--theme-primary")) == "bg-(--theme-primary)"

# Test with opacity
assert str(bg("--custom-bg", opacity=50)) == "bg-(--custom-bg)/50"
# Test factory call syntax
assert str(bg("red-500")) == "bg-red-500"
assert str(bg("red-500", opacity=75)) == "bg-red-500/75"
assert str(bg((ColorFamily.BLUE, ColorShade.SHADE_300))) == "bg-blue-300"
assert str(bg((ColorFamily.GREEN, "500"))) == "bg-green-500"

Test Functions

Comprehensive test functions following the project’s naming convention:


test_colors_enum_examples


def test_colors_enum_examples(
    
):

Test color family and shade enums.


test_colors_validation_examples


def test_colors_validation_examples(
    
):

Test color validation functions.


test_colors_factory_examples


def test_colors_factory_examples(
    
):

Test ColoredFactory with various color specifications.


test_colors_opacity_examples


def test_colors_opacity_examples(
    
):

Test opacity modifiers with color utilities.


test_colors_arbitrary_examples


def test_colors_arbitrary_examples(
    
):

Test arbitrary color values and custom properties.


test_colors_proxy_examples


def test_colors_proxy_examples(
    
):

Test ColorFamilyProxy for dot notation access.

Practical Examples

Test with multiple utility prefixes to show the color system is reusable:


test_colors_multiple_utilities_examples


def test_colors_multiple_utilities_examples(
    
):

Test color system with multiple utility types.


test_colors_practical_usage_examples


def test_colors_practical_usage_examples(
    
):

Test practical usage patterns with FastHTML components.


test_colors_modifier_examples


def test_colors_modifier_examples(
    
):

Test color utilities with modifiers for conditional styling.

Helper Functions

Utility functions for working with colors:


get_all_color_families


def get_all_color_families(
    
)->List: # List of all color family names

Get list of all Tailwind color family names.


get_all_shades


def get_all_shades(
    
)->List: # List of all shade values

Get list of all Tailwind shade values.


get_all_color_specs


def get_all_color_specs(
    
)->List: # List of all valid color-shade combinations

Get list of all valid color-shade combinations.

# Test helper functions
families = get_all_color_families()
assert len(families) == 22
assert "red" in families
assert "blue" in families
assert "slate" in families

shades = get_all_shades()
assert len(shades) == 11
assert "50" in shades
assert "500" in shades
assert "950" in shades

specs = get_all_color_specs()
assert len(specs) == 22 * 11  # 242 combinations
assert "red-500" in specs
assert "blue-300" in specs
assert "slate-950" in specs

print("✅ All color helper functions work correctly!")
✅ All color helper functions work correctly!

Documentation Test

Test that the factory provides proper documentation:


test_colors_factory_documentation


def test_colors_factory_documentation(
    
):

Test that color factories have proper documentation.