Skip to content

Instantly share code, notes, and snippets.

@archatas
Last active May 18, 2024 23:31
Show Gist options
  • Save archatas/85721e325797d043b62aeb7edf86c772 to your computer and use it in GitHub Desktop.
Save archatas/85721e325797d043b62aeb7edf86c772 to your computer and use it in GitHub Desktop.
CSS Color Validator for Django models and forms
import re
from django.core.exceptions import ValidationError
def validate_css_color(value):
"""
Custom validation function for background color field.
Args:
value (str): The value entered for the background color field.
Returns:
str: The cleaned value (if valid color) or raises a ValidationError.
"""
# Regexes for different color formats
hex_regex = r"^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$"
rgb_regex = r"^rgb\(\s*(?:\d{1,3}\s+|\d{1,3})\s*(?:,\s+|\s+)(?:\d{1,3}\s+|\d{1,3})\s*(?:,\s+|\s+)\d{1,3}\s*\)$"
rgba_regex = r"^rgba\(\s*(?:\d{1,3}\s+|\d{1,3})\s*(?:,\s+|\s+)(?:\d{1,3}\s+|\d{1,3})\s*(?:,\s+|\s+)(?:\d{1,3}\s+|\d{1,3})\s*(?:,\s*0?\.\d+|\d)(?:,\s*0?\.\d+|\d)?\s*\)$"
hsla_regex = r"^hsla\(\s*(?:\d{1,3}|\d{0,2}\.\d{1,3})?\s*,\s*\d{1,3}\%?\s*,\s*\d{1,3}\%?\s*(?:,\s*0?\.\d+|\d)?\s*\)$"
named_color_regex = r"^(yellowgreen|yellow|whitesmoke|white|wheat|violet|turquoise|transparent|tomato|thistle|teal|tan|steelblue|springgreen|snow|slategrey|slategray|slateblue|skyblue|silver|sienna|seashell|seagreen|sandybrown|salmon|saddlebrown|royalblue|rosybrown|red|rebeccapurple|purple|powderblue|plum|pink|peru|peachpuff|papayawhip|palevioletred|paleturquoise|palegreen|palegoldenrod|orchid|orangered|orange|olivedrab|olive|oldlace|navy|navajowhite|moccasin|mistyrose|mintcream|midnightblue|mediumvioletred|mediumturquoise|mediumspringgreen|mediumslateblue|mediumseagreen|mediumpurple|mediumorchid|mediumblue|mediumaquamarine|maroon|magenta|linen|limegreen|lime|lightyellow|lightsteelblue|lightslategrey|lightslategray|lightskyblue|lightseagreen|lightsalmon|lightpink|lightgrey|lightgreen|lightgray|lightgoldenrodyellow|lightcyan|lightcoral|lightblue|lemonchiffon|lawngreen|lavenderblush|lavender|khaki|ivory|indigo|indianred|hotpink|honeydew|grey|greenyellow|green|gray|goldenrod|gold|ghostwhite|gainsboro|fuchsia|forestgreen|floralwhite|firebrick|dodgerblue|dimgrey|dimgray|deepskyblue|deeppink|darkviolet|darkturquoise|darkslategrey|darkslategray|darkslateblue|darkseagreen|darksalmon|darkred|darkorchid|darkorange|darkolivegreen|darkmagenta|darkkhaki|darkgrey|darkgreen|darkgray|darkgoldenrod|darkcyan|darkblue|cyan|crimson|cornsilk|cornflowerblue|coral|chocolate|chartreuse|cadetblue|burlywood|brown|blueviolet|blue|blanchedalmond|black|bisque|beige|azure|aquamarine|aqua|antiquewhite|aliceblue)$"
# Check for valid hex color
if re.match(hex_regex, value):
return value.upper()
# Check for RGB or RGBA color
elif re.match(rgb_regex, value) or re.match(rgba_regex, value) or re.match(hsla_regex, value):
return value.lower()
# Check for named color (using predefined list)
elif re.match(named_color_regex, value):
return value.lower()
# If none of the above matches, raise a validation error
else:
raise ValidationError(
"Invalid color format. Please use a valid hex, rgb(a), hsl(a), or named color."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment