2525
2626
2727@lru_cache
28- def getrgb (color ) :
28+ def getrgb (color : str ) -> tuple [ int , int , int ] | tuple [ int , int , int , int ] :
2929 """
3030 Convert a color string to an RGB or RGBA tuple. If the string cannot be
3131 parsed, this function raises a :py:exc:`ValueError` exception.
@@ -44,8 +44,10 @@ def getrgb(color):
4444 if rgb :
4545 if isinstance (rgb , tuple ):
4646 return rgb
47- colormap [color ] = rgb = getrgb (rgb )
48- return rgb
47+ rgb_tuple = getrgb (rgb )
48+ assert len (rgb_tuple ) == 3
49+ colormap [color ] = rgb_tuple
50+ return rgb_tuple
4951
5052 # check for known string formats
5153 if re .match ("#[a-f0-9]{3}$" , color ):
@@ -88,15 +90,15 @@ def getrgb(color):
8890 if m :
8991 from colorsys import hls_to_rgb
9092
91- rgb = hls_to_rgb (
93+ rgb_floats = hls_to_rgb (
9294 float (m .group (1 )) / 360.0 ,
9395 float (m .group (3 )) / 100.0 ,
9496 float (m .group (2 )) / 100.0 ,
9597 )
9698 return (
97- int (rgb [0 ] * 255 + 0.5 ),
98- int (rgb [1 ] * 255 + 0.5 ),
99- int (rgb [2 ] * 255 + 0.5 ),
99+ int (rgb_floats [0 ] * 255 + 0.5 ),
100+ int (rgb_floats [1 ] * 255 + 0.5 ),
101+ int (rgb_floats [2 ] * 255 + 0.5 ),
100102 )
101103
102104 m = re .match (
@@ -105,15 +107,15 @@ def getrgb(color):
105107 if m :
106108 from colorsys import hsv_to_rgb
107109
108- rgb = hsv_to_rgb (
110+ rgb_floats = hsv_to_rgb (
109111 float (m .group (1 )) / 360.0 ,
110112 float (m .group (2 )) / 100.0 ,
111113 float (m .group (3 )) / 100.0 ,
112114 )
113115 return (
114- int (rgb [0 ] * 255 + 0.5 ),
115- int (rgb [1 ] * 255 + 0.5 ),
116- int (rgb [2 ] * 255 + 0.5 ),
116+ int (rgb_floats [0 ] * 255 + 0.5 ),
117+ int (rgb_floats [1 ] * 255 + 0.5 ),
118+ int (rgb_floats [2 ] * 255 + 0.5 ),
117119 )
118120
119121 m = re .match (r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$" , color )
@@ -124,7 +126,7 @@ def getrgb(color):
124126
125127
126128@lru_cache
127- def getcolor (color , mode : str ) -> tuple [int , ...]:
129+ def getcolor (color : str , mode : str ) -> int | tuple [int , ...]:
128130 """
129131 Same as :py:func:`~PIL.ImageColor.getrgb` for most modes. However, if
130132 ``mode`` is HSV, converts the RGB value to a HSV value, or if ``mode`` is
@@ -136,33 +138,34 @@ def getcolor(color, mode: str) -> tuple[int, ...]:
136138
137139 :param color: A color string
138140 :param mode: Convert result to this mode
139- :return: ``(graylevel[ , alpha] ) or (red, green, blue[, alpha])``
141+ :return: ``graylevel, (graylevel, alpha) or (red, green, blue[, alpha])``
140142 """
141143 # same as getrgb, but converts the result to the given mode
142- color , alpha = getrgb (color ), 255
143- if len (color ) == 4 :
144- color , alpha = color [:3 ], color [3 ]
144+ rgb , alpha = getrgb (color ), 255
145+ if len (rgb ) == 4 :
146+ alpha = rgb [3 ]
147+ rgb = rgb [:3 ]
145148
146149 if mode == "HSV" :
147150 from colorsys import rgb_to_hsv
148151
149- r , g , b = color
152+ r , g , b = rgb
150153 h , s , v = rgb_to_hsv (r / 255 , g / 255 , b / 255 )
151154 return int (h * 255 ), int (s * 255 ), int (v * 255 )
152155 elif Image .getmodebase (mode ) == "L" :
153- r , g , b = color
156+ r , g , b = rgb
154157 # ITU-R Recommendation 601-2 for nonlinear RGB
155158 # scaled to 24 bits to match the convert's implementation.
156- color = (r * 19595 + g * 38470 + b * 7471 + 0x8000 ) >> 16
159+ graylevel = (r * 19595 + g * 38470 + b * 7471 + 0x8000 ) >> 16
157160 if mode [- 1 ] == "A" :
158- return color , alpha
159- else :
160- if mode [- 1 ] == "A" :
161- return color + (alpha ,)
162- return color
161+ return graylevel , alpha
162+ return graylevel
163+ elif mode [- 1 ] == "A" :
164+ return rgb + (alpha ,)
165+ return rgb
163166
164167
165- colormap = {
168+ colormap : dict [ str , str | tuple [ int , int , int ]] = {
166169 # X11 colour table from https://drafts.csswg.org/css-color-4/, with
167170 # gray/grey spelling issues fixed. This is a superset of HTML 4.0
168171 # colour names used in CSS 1.
0 commit comments