color_tools.validation
Color validation functions to check if a hex code matches a color name. Uses fuzzy matching and perceptual color distance (Delta E 2000). Useful for validating imported color data or user input.
- Note: For best fuzzy matching results, install the optional fuzzywuzzy package:
pip install fuzzywuzzy
A hybrid fallback matcher (exact/substring/Levenshtein) is used when fuzzywuzzy is not available.
- class color_tools.validation.ColorValidationRecord(is_match, name_match, name_confidence, hex_value, suggested_hex, delta_e, message)[source]
Bases:
objectResult record from validating if a hex code matches a color name.
This immutable dataclass contains comprehensive validation results including fuzzy name matching confidence, perceptual color distance (Delta E), and helpful suggestions for mismatches.
- is_match
True if the color name and hex code are a good match
- name_match
Best matching CSS color name from the database (e.g., “lightblue”)
- name_confidence
Fuzzy matching confidence 0.0-1.0 (1.0 = exact match)
- hex_value
The hex code that was validated (normalized with # prefix)
- suggested_hex
Suggested hex code for the matched name, or None if exact match
- delta_e
Perceptual color distance (Delta E 2000) between provided and suggested colors
- message
Human-readable validation result message
Example
>>> from color_tools.validation import validate_color >>> result = validate_color("light blue", "#ADD8E6") >>> print(f"Match: {result.is_match}") Match: True >>> print(f"Matched '{result.name_match}' with {result.name_confidence:.0%} confidence") Matched 'lightblue' with 100% confidence >>> print(f"Delta E: {result.delta_e:.2f}") Delta E: 0.00
- Parameters:
- color_tools.validation.validate_color(color_name, hex_code, de_threshold=20.0)[source]
Validates if a given hex code approximately matches a given color name.
- Parameters:
- Return type:
- Returns:
A ColorValidationRecord with the validation results.
Note
Uses fuzzywuzzy for fuzzy matching if available, otherwise falls back to a hybrid matcher using exact/substring/Levenshtein matching.