14
14
# KIND, either express or implied. See the License for the
15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
+ from __future__ import annotations
17
18
18
19
import sys
19
20
from typing import Any , Optional , Sequence , TYPE_CHECKING
33
34
ParseableFloat = Any
34
35
ParseableInt = Any
35
36
36
-
37
37
RGB_PATTERN = r"^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*$"
38
38
RGB_PCT_PATTERN = r"^\s*rgb\(\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*\)\s*$"
39
39
RGBA_PATTERN = r"^\s*rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0\.\d+)\s*\)\s*$"
44
44
HSLA_PATTERN = r"^\s*hsla\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(0|1|0\.\d+)\s*\)\s*$"
45
45
46
46
47
- class Color ( object ) :
47
+ class Color :
48
48
"""
49
49
Color conversion support class
50
50
@@ -59,8 +59,8 @@ class Color(object):
59
59
print(Color.from_string('blue').rgba)
60
60
"""
61
61
62
- @staticmethod
63
- def from_string (str_ : str ) -> " Color" :
62
+ @classmethod
63
+ def from_string (cls , str_ : str ) -> Color :
64
64
import re
65
65
66
66
class Matcher (object ):
@@ -80,30 +80,32 @@ def groups(self) -> Sequence[str]:
80
80
m = Matcher ()
81
81
82
82
if m .match (RGB_PATTERN , str_ ):
83
- return Color (* m .groups )
83
+ return cls (* m .groups )
84
84
elif m .match (RGB_PCT_PATTERN , str_ ):
85
85
rgb = tuple ([float (each ) / 100 * 255 for each in m .groups ])
86
- return Color (* rgb )
86
+ return cls (* rgb )
87
87
elif m .match (RGBA_PATTERN , str_ ):
88
- return Color (* m .groups )
88
+ return cls (* m .groups )
89
89
elif m .match (RGBA_PCT_PATTERN , str_ ):
90
- rgba = tuple ([float (each ) / 100 * 255 for each in m .groups [:3 ]] + [m .groups [3 ]]) # type: ignore
91
- return Color (* rgba )
90
+ rgba = tuple (
91
+ [float (each ) / 100 * 255 for each in m .groups [:3 ]] + [m .groups [3 ]]) # type: ignore
92
+ return cls (* rgba )
92
93
elif m .match (HEX_PATTERN , str_ ):
93
94
rgb = tuple ([int (each , 16 ) for each in m .groups ])
94
- return Color (* rgb )
95
+ return cls (* rgb )
95
96
elif m .match (HEX3_PATTERN , str_ ):
96
97
rgb = tuple ([int (each * 2 , 16 ) for each in m .groups ])
97
- return Color (* rgb )
98
+ return cls (* rgb )
98
99
elif m .match (HSL_PATTERN , str_ ) or m .match (HSLA_PATTERN , str_ ):
99
- return Color ._from_hsl (* m .groups )
100
+ return cls ._from_hsl (* m .groups )
100
101
elif str_ .upper () in Colors .keys ():
101
102
return Colors [str_ .upper ()]
102
103
else :
103
104
raise ValueError ("Could not convert %s into color" % str_ )
104
105
105
- @staticmethod
106
- def _from_hsl (h : ParseableFloat , s : ParseableFloat , light : ParseableFloat , a : ParseableFloat = 1 ) -> "Color" :
106
+ @classmethod
107
+ def _from_hsl (cls , h : ParseableFloat , s : ParseableFloat , light : ParseableFloat ,
108
+ a : ParseableFloat = 1 ) -> Color :
107
109
h = float (h ) / 360
108
110
s = float (s ) / 100
109
111
_l = float (light ) / 100
@@ -135,9 +137,10 @@ def hue_to_rgb(lum1: float, lum2: float, hue: float) -> float:
135
137
g = hue_to_rgb (luminocity1 , luminocity2 , h )
136
138
b = hue_to_rgb (luminocity1 , luminocity2 , h - 1.0 / 3.0 )
137
139
138
- return Color (round (r * 255 ), round (g * 255 ), round (b * 255 ), a )
140
+ return cls (round (r * 255 ), round (g * 255 ), round (b * 255 ), a )
139
141
140
- def __init__ (self , red : ParseableInt , green : ParseableInt , blue : ParseableInt , alpha : ParseableFloat = 1 ) -> None :
142
+ def __init__ (self , red : ParseableInt , green : ParseableInt , blue : ParseableInt ,
143
+ alpha : ParseableFloat = 1 ) -> None :
141
144
self .red = int (red )
142
145
self .green = int (green )
143
146
self .blue = int (blue )
@@ -170,7 +173,8 @@ def __hash__(self) -> int:
170
173
return hash ((self .red , self .green , self .blue , self .alpha ))
171
174
172
175
def __repr__ (self ) -> str :
173
- return "Color(red=%d, green=%d, blue=%d, alpha=%s)" % (self .red , self .green , self .blue , self .alpha )
176
+ return "Color(red=%d, green=%d, blue=%d, alpha=%s)" % (
177
+ self .red , self .green , self .blue , self .alpha )
174
178
175
179
def __str__ (self ) -> str :
176
180
return "Color: %s" % self .rgba
0 commit comments