@@ -121,17 +121,16 @@ def as_named(self, *, fallback: bool = False) -> str:
121121 Raises:
122122 ValueError: When no named color is found and fallback is `False`.
123123 """
124- if self ._rgba .alpha is None :
125- rgb = cast (Tuple [int , int , int ], self .as_rgb_tuple ())
126- try :
127- return COLORS_BY_VALUE [rgb ]
128- except KeyError as e :
129- if fallback :
130- return self .as_hex ()
131- else :
132- raise ValueError ('no named color found, use fallback=True, as_hex() or as_rgb()' ) from e
133- else :
124+ if self ._rgba .alpha is not None :
134125 return self .as_hex ()
126+ rgb = cast (Tuple [int , int , int ], self .as_rgb_tuple ())
127+ try :
128+ return COLORS_BY_VALUE [rgb ]
129+ except KeyError as e :
130+ if fallback :
131+ return self .as_hex ()
132+ else :
133+ raise ValueError ('no named color found, use fallback=True, as_hex() or as_rgb()' ) from e
135134
136135 def as_hex (self , format : Literal ['short' , 'long' ] = 'short' ) -> str :
137136 """Returns the hexadecimal representation of the color.
@@ -149,7 +148,7 @@ def as_hex(self, format: Literal['short', 'long'] = 'short') -> str:
149148 as_hex = '' .join (f'{ v :02x} ' for v in values )
150149 if format == 'short' and all (c in repeat_colors for c in values ):
151150 as_hex = '' .join (as_hex [c ] for c in range (0 , len (as_hex ), 2 ))
152- return '#' + as_hex
151+ return f'# { as_hex } '
153152
154153 def as_rgb (self ) -> str :
155154 """
@@ -179,16 +178,10 @@ def as_rgb_tuple(self, *, alpha: bool | None = None) -> ColorTuple:
179178 If alpha is included, it is in the range 0 to 1.
180179 """
181180 r , g , b = (float_to_255 (c ) for c in self ._rgba [:3 ])
182- if alpha is None :
183- if self ._rgba .alpha is None :
184- return r , g , b
185- else :
186- return r , g , b , self ._alpha_float ()
187- elif alpha :
188- return r , g , b , self ._alpha_float ()
189- else :
190- # alpha is False
181+ if alpha is None and self ._rgba .alpha is None or alpha is not None and not alpha :
191182 return r , g , b
183+ else :
184+ return r , g , b , self ._alpha_float ()
192185
193186 def as_hsl (self ) -> str :
194187 """
@@ -225,11 +218,7 @@ def as_hsl_tuple(self, *, alpha: bool | None = None) -> HslColorTuple:
225218 return h , s , l
226219 else :
227220 return h , s , l , self ._alpha_float ()
228- if alpha :
229- return h , s , l , self ._alpha_float ()
230- else :
231- # alpha is False
232- return h , s , l
221+ return (h , s , l , self ._alpha_float ()) if alpha else (h , s , l )
233222
234223 def _alpha_float (self ) -> float :
235224 return 1 if self ._rgba .alpha is None else self ._rgba .alpha
@@ -315,20 +304,14 @@ def parse_str(value: str) -> RGBA:
315304 if m :
316305 * rgb , a = m .groups ()
317306 r , g , b = (int (v * 2 , 16 ) for v in rgb )
318- if a :
319- alpha : float | None = int (a * 2 , 16 ) / 255
320- else :
321- alpha = None
307+ alpha = int (a * 2 , 16 ) / 255 if a else None
322308 return ints_to_rgba (r , g , b , alpha )
323309
324310 m = re .fullmatch (r_hex_long , value_lower )
325311 if m :
326312 * rgb , a = m .groups ()
327313 r , g , b = (int (v , 16 ) for v in rgb )
328- if a :
329- alpha = int (a , 16 ) / 255
330- else :
331- alpha = None
314+ alpha = int (a , 16 ) / 255 if a else None
332315 return ints_to_rgba (r , g , b , alpha )
333316
334317 m = re .fullmatch (r_rgb , value_lower ) or re .fullmatch (r_rgb_v4_style , value_lower )
@@ -390,11 +373,11 @@ def parse_color_value(value: int | str, max_val: int = 255) -> float:
390373 """
391374 try :
392375 color = float (value )
393- except ValueError :
376+ except ValueError as e :
394377 raise PydanticCustomError (
395378 'color_error' ,
396379 'value is not a valid color: color values must be a valid number' ,
397- )
380+ ) from e
398381 if 0 <= color <= max_val :
399382 return color / max_val
400383 else :
@@ -425,11 +408,11 @@ def parse_float_alpha(value: None | str | float | int) -> float | None:
425408 alpha = float (value [:- 1 ]) / 100
426409 else :
427410 alpha = float (value )
428- except ValueError :
411+ except ValueError as e :
429412 raise PydanticCustomError (
430413 'color_error' ,
431414 'value is not a valid color: alpha values must be a valid float' ,
432- )
415+ ) from e
433416
434417 if math .isclose (alpha , 1 ):
435418 return None
@@ -465,7 +448,7 @@ def parse_hsl(h: str, h_units: str, sat: str, light: str, alpha: float | None =
465448 h_value = h_value % rads / rads
466449 else :
467450 # turns
468- h_value = h_value % 1
451+ h_value %= 1
469452
470453 r , g , b = hls_to_rgb (h_value , l_value , s_value )
471454 return RGBA (r , g , b , parse_float_alpha (alpha ))
0 commit comments