-
Notifications
You must be signed in to change notification settings - Fork 9
/
color.py
39 lines (35 loc) · 1.27 KB
/
color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import re
import webcolors
from .attribute import Attribute
class Color(Attribute):
"""
Color Attribute.
Usage: color=Color(red,green,blue)
"""
def __init__(self, red=0, green=0, blue=0):
if isinstance(red, str):
# hex to tuple to Color
color = red.lstrip("#")
hexcolor = re.search(r"^(?:[0-9a-fA-F]{3}){1,2}$", color)
wcrgb = None
if not hexcolor:
# try appending leading zeros until it works
for i in range(8):
try:
wcrgb = webcolors.name_to_rgb("#"+i*"0"+color)
except:
break
if wcrgb is not None:
c = (wcrgb.red, wcrgb.green, wcrgb.blue)
else:
c = (128,128,128)
else:
c = tuple(int(color[c:c+2], 16) for c in (0, 2, 4))
red, green, blue = c
if isinstance(red, float): red = int(red)
if isinstance(blue, float): blue = int(blue)
if isinstance(green, float): green = int(green)
super().__init__(red=red, green=green, blue=blue)
@property
def hex(self):
return "#{:02x}{:02x}{:02x}".format(self.red, self.green, self.blue)