1616t = t .color_picker
1717
1818
19- def _to_floats (given : list [namedtuple ]) -> tuple [float , float , float ]:
19+ COLOR_ARGS = namedtuple ("ColorParameter" , ["value" , "max_value" ])
20+ COLOR = namedtuple ("RGB_Tuple" , ["R" , "G" , "B" ])
21+
22+
23+ def _to_floats (given : list [COLOR_ARGS ]) -> tuple [float , float , float ]:
2024 out : list [float ] = []
2125
2226 for arg in given :
@@ -28,8 +32,8 @@ def _to_floats(given: list[namedtuple]) -> tuple[float, float, float]:
2832 return out [0 ], out [1 ], out [2 ]
2933
3034
31- def _hex_to_color (hex_color : str ) -> tuple [ int , ...] :
32- return tuple (int (hex_color [i : i + 2 ], 16 ) for i in (0 , 2 , 4 )) # noqa: E203
35+ def _hex_to_color (hex_color : str ) -> COLOR :
36+ return COLOR ( * tuple (int (hex_color [i : i + 2 ], 16 ) for i in (0 , 2 , 4 ) )) # noqa: E203
3337
3438
3539class ColorPickerCog (Cog , name = "Color Picker" ):
@@ -44,39 +48,20 @@ class ColorPickerCog(Cog, name="Color Picker"):
4448 @commands .command (name = "color_picker" , aliases = ["cp" , "color" ])
4549 @docs (t .commands .color_picker )
4650 async def color_picker (self , ctx : Context , * , color : str ):
47- color_args = namedtuple ("ColorParameter" , ["value" , "max_value" ])
4851
4952 if color_re := self .RE_HEX .match (color ):
5053 rgb = _hex_to_color (color_re .group (1 ))
51- rgb = _to_floats ([color_args (rgb [0 ], 255 ), color_args ( rgb [ 1 ], 255 ), color_args ( rgb [ 2 ], 255 )])
54+ rgb = _to_floats ([COLOR_ARGS (rgb [i ], 255 ) for i in range ( 3 )])
5255
5356 elif color_re := self .RE_RGB .match (color ):
54- rgb = _to_floats (
55- [
56- color_args (color_re .group (2 ), 255 ),
57- color_args (color_re .group (3 ), 255 ),
58- color_args (color_re .group (4 ), 255 ),
59- ]
60- )
57+ rgb = _to_floats ([COLOR_ARGS (color_re .group (i ), 255 ) for i in range (2 , 5 )])
6158
6259 elif color_re := self .RE_HSV .match (color ):
63- values = _to_floats (
64- [
65- color_args (color_re .group (2 ), 360 ),
66- color_args (color_re .group (3 ), 100 ),
67- color_args (color_re .group (4 ), 100 ),
68- ]
69- )
60+ values = _to_floats ([COLOR_ARGS (color_re .group (i [0 ]), i [1 ]) for i in ((2 , 360 ), (3 , 100 ), (4 , 100 ))])
7061 rgb = colorsys .hsv_to_rgb (values [0 ], values [1 ], values [2 ])
7162
7263 elif color_re := self .RE_HSL .match (color ):
73- values = _to_floats (
74- [
75- color_args (color_re .group (2 ), 360 ),
76- color_args (color_re .group (3 ), 100 ),
77- color_args (color_re .group (4 ), 100 ),
78- ]
79- )
64+ values = _to_floats ([COLOR_ARGS (color_re .group (i [0 ]), i [1 ]) for i in ((2 , 360 ), (3 , 100 ), (4 , 100 ))])
8065 rgb = colorsys .hls_to_rgb (values [0 ], values [2 ], values [1 ])
8166
8267 else :
0 commit comments