Debian Codegraph @

colorfield/validators.py

raw symbols SCIP
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 # prefix and opening parenthesis
23 r"^rgb\("
24 # first number: red channel
25 r"(\d{1,3})"
26 # comma and optional space
27 r",\s?"
28 # second number: green channel
29 r"(\d{1,3})"
30 # comma and optional space
31 r",\s?"
32 # third number: blue channel
33 r"(\d{1,3})"
34 # closing parenthesis
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 # prefix and opening parenthesis
44 r"^rgba\("
45 # first number: red channel
46 r"(\d{1,3})"
47 # comma and optional space
48 r",\s?"
49 # second number: green channel
50 r"(\d{1,3})"
51 # comma and optional space
52 r",\s?"
53 # third number: blue channel
54 r"(\d{1,3})"
55 # comma and optional space
56 r",\s?"
57 # alpha channel: decimal number between 0 and 1
58 r"(0(\.\d{1,2})?|1(\.0)?)"
59 # closing parenthesis
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 )