@@ -152,26 +152,20 @@ def getcolor(color: str, mode: str) -> int | tuple[int, ...]:
152152 r , g , b = rgb
153153 h , s , v = rgb_to_hsv (r / 255 , g / 255 , b / 255 )
154154 return int (h * 255 ), int (s * 255 ), int (v * 255 )
155- elif Image .getmodebase (mode ) == "L" :
155+ if Image .getmodebase (mode ) == "L" :
156156 r , g , b = rgb
157157 # ITU-R Recommendation 601-2 for nonlinear RGB
158158 # scaled to 24 bits to match the convert's implementation.
159159 graylevel = (r * 19595 + g * 38470 + b * 7471 + 0x8000 ) >> 16
160- if mode [- 1 ] == "A" :
161- return graylevel , alpha
162- if mode [- 1 ] == "a" :
163- return _premultiply (graylevel , alpha ), alpha
164- return graylevel
165- elif mode [- 1 ] == "A" :
166- return rgb + (alpha ,)
167- elif mode [- 1 ] == "a" :
168- return tuple (_premultiply (band , alpha ) for band in rgb ) + (alpha ,)
169- return rgb
170-
171-
172- def _premultiply (band : int , alpha : int ) -> int :
173- # Rounded, to match convert()'s premultiplication.
174- return (band * alpha + 127 ) // 255
160+ value = (graylevel ,)
161+ else :
162+ value = rgb
163+ if mode [- 1 ] == "a" :
164+ # Rounded, to match convert()'s premultiplication.
165+ value = tuple ((band * alpha + 127 ) // 255 for band in value )
166+ if mode [- 1 ] in ("A" , "a" ):
167+ return value + (alpha ,)
168+ return value [0 ] if len (value ) == 1 else value
175169
176170
177171colormap : dict [str , str | tuple [int , int , int ]] = {
0 commit comments