| 1 |
import re |
| 2 |
|
| 3 |
from django.core.validators import RegexValidator |
| 4 |
from django.utils.translation import gettext_lazy as _ |
| 5 |
|
| 6 |
COLOR_HEX_RE = re.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") |
| 7 |
color_hex_validator = RegexValidator( |
| 8 |
COLOR_HEX_RE, |
| 9 |
_("Enter a valid hex color, eg. #000000"), |
| 10 |
"invalid", |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
COLOR_HEXA_RE = re.compile("#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$") |
| 15 |
color_hexa_validator = RegexValidator( |
| 16 |
COLOR_HEXA_RE, |
| 17 |
_("Enter a valid hexa color, eg. #00000000"), |
| 18 |
"invalid", |
| 19 |
) |
| 20 |
|
| 21 |
COLOR_RGB_RE = re.compile( |
| 22 |
|
| 23 |
r"^rgb\(" |
| 24 |
|
| 25 |
r"(\d{1,3})" |
| 26 |
|
| 27 |
r",\s?" |
| 28 |
|
| 29 |
r"(\d{1,3})" |
| 30 |
|
| 31 |
r",\s?" |
| 32 |
|
| 33 |
r"(\d{1,3})" |
| 34 |
|
| 35 |
r"\)$" |
| 36 |
) |
| 37 |
color_rgb_validator = RegexValidator( |
| 38 |
COLOR_RGB_RE, |
| 39 |
_("Enter a valid rgb color, eg. rgb(128, 128, 128)"), |
| 40 |
"invalid", |
| 41 |
) |
| 42 |
COLOR_RGBA_RE = re.compile( |
| 43 |
|
| 44 |
r"^rgba\(" |
| 45 |
|
| 46 |
r"(\d{1,3})" |
| 47 |
|
| 48 |
r",\s?" |
| 49 |
|
| 50 |
r"(\d{1,3})" |
| 51 |
|
| 52 |
r",\s?" |
| 53 |
|
| 54 |
r"(\d{1,3})" |
| 55 |
|
| 56 |
r",\s?" |
| 57 |
|
| 58 |
r"(0(\.\d{1,2})?|1(\.0)?)" |
| 59 |
|
| 60 |
r"\)$" |
| 61 |
) |
| 62 |
color_rgba_validator = RegexValidator( |
| 63 |
COLOR_RGBA_RE, |
| 64 |
_("Enter a valid rgba color, eg. rgba(128, 128, 128, 0.5)"), |
| 65 |
"invalid", |
| 66 |
) |