Allow rbg and rgba , hsl and hsla as valid colors.
See original GitHub issueThink we can use
^(#[0-9a-f]{3}|#(?:[0-9a-f]{2}){2,4}|(rgb|hsl)a?\((\d+%?(deg|rad|grad|turn)?[,\s]+){2,3}[\s\/]*[\d\.]+%?\))
As regex to also validate
hence change to
_color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$')
_rbg_rgba_hsl_hsla = re.compile('^(#[0-9a-f]{3}|#(?:[0-9a-f]{2}){2,4}|(rgb|hsl)a?\((\d+%?(deg|rad|grad|turn)?[,\s]+){2,3}[\s\/]*[\d\.]+%?\))')
class Color(traitlets.Unicode):
"""A string holding a valid HTML color such as 'blue', '#060482', '#A80'"""
info_text = 'a valid HTML color'
default_value = traitlets.Undefined
def validate(self, obj, value):
if value.lower() in _color_names or _color_re.match(value) or _rbg_rgba_hsl_hsla.match(color) :
return value
self.error(obj, value)
checked with code:
import re
_rbg_rgba_hsl_hsla = re.compile('^(#[0-9a-f]{3}|#(?:[0-9a-f]{2}){2,4}|(rgb|hsl)a?\((\d+%?(deg|rad|grad|turn)?[,\s]+){2,3}[\s\/]*[\d\.]+%?\))')
test_list = [
'rgb(255, 0, 0, 0.3)', # red
'rgb(0, 255, 700)', # green
'rgb(0, 0, 255)', # blue
# RGBA Colors
'rgba(255, 0, 0, 3)', # red with opacity
'rgba(0, 255, 0)', # green with opacity
'rgba(0, 0, 255, 1)', # blue with opacity
# HSL Colors
'hsl(120, 100%, 500%)', # green
'hsl(120, 100%, 75)', # light green
'hsl(120, 100%, 25%)', # dark green
'hsl(120, 60%, 70)', # pastel green
# HSLA Colors
'hsl(120, 100%, 50%, 0.2)', # green with opacity
'hsla(120, 100%, 75%, 40)', # light green with opacity
'hsl(120, 100%, 70%, 0.6)', # dark green with opacity
'hsla(120, 60%, 70%, 2)', # pastel green with opacity
]
result = []
for color in test_list:
result.append(_rbg_rgba_hsl_hsla.match(color))
dict(zip(test_list,result))
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
CSS3 RGBA, HSL and HSLA Color Values - Tutorial Republic
Colors can be defined in the RGBA model (red-green-blue-alpha) using the rgba() functional notation. RGBA color model are an extension of RGB color...
Read more >HTML HSL and HSLA Colors - W3Schools
HSLA color values are an extension of HSL with an Alpha channel (opacity). HSL Color Values. In HTML, a color can be specified...
Read more >RGBA, HSLA and HSL Colors
RGBA & HSL/HSLA colors are introduced in CSS3. Till CSS2, we were using RGB colors ( red, green, blue ) only. RGB and...
Read more >How to use HSL and HSLa Colors in CSS - Techstacker
HSL and HSLa are color methods in CSS that you can use as alternatives to Hex or RGB/RGBa to apply more real-world colors...
Read more ><color> - CSS: Cascading Style Sheets - MDN Web Docs
Many designers find HSL more intuitive than RGB, since it allows hue, saturation, and lightness to each be adjusted independently. HSL can also ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Actually, I see that the tests does not include any HSL values. They might have gotten lost somewhere. Putting that as a TODO on the PR.
vidartf-fullcolor has been merged and closes this issue.