33import re
44from typing import Any
55
6- from PIL import ImageColor , Image
76from discord import Embed , Colour , File
87from discord .ext import commands
9- from discord .ext .commands import Context
8+ from discord .ext .commands import Context , CommandError
109
1110from PyDrocsid .cog import Cog
1211from PyDrocsid .command import reply
@@ -34,15 +33,18 @@ def _to_int(colors: tuple[Any, Any, Any]) -> tuple[int, ...]:
3433 hsv : tuple [int , ...]
3534 hsl : tuple [int , ...]
3635
36+ def _hex_to_color (hex_color : str ) -> tuple [int , ...]:
37+ return tuple (int (hex_color [i :i + 2 ], 16 ) for i in (0 , 2 , 4 ))
38+
3739 if color_re := self .RE_HEX .match (color ):
3840 color_hex = color_re .group (1 )
39- rgb = ImageColor . getcolor ( color , "RGB" )
40- hsv = ImageColor . getcolor ( color , "HSV" )
41+ rgb = _hex_to_color ( color_hex )
42+ hsv = _to_int ( colorsys . rgb_to_hsv ( * rgb ) )
4143 hsl = _to_int (colorsys .rgb_to_hls (* rgb ))
4244 elif color_re := self .RE_RGB .match (color ):
4345 rgb = _to_int ((color_re .group (1 ), color_re .group (2 ), color_re .group (3 )))
4446 color_hex = "{0:02x}{1:02x}{2:02x}" .format (* rgb )
45- hsv = ImageColor . getcolor ( f"# { color_hex } " , "HSV" )
47+ hsv = _to_int ( colorsys . rgb_to_hsv ( * rgb ) )
4648 hsl = _to_int (colorsys .rgb_to_hls (* rgb ))
4749 elif color_re := self .RE_HSV .match (color ):
4850 hsv = _to_int ((color_re .group (1 ), color_re .group (2 ), color_re .group (3 )))
@@ -55,18 +57,12 @@ def _to_int(colors: tuple[Any, Any, Any]) -> tuple[int, ...]:
5557 hsv = _to_int (colorsys .rgb_to_hsv (* rgb ))
5658 color_hex = "{0:02x}{1:02x}{2:02x}" .format (* rgb )
5759 else :
58- embed : Embed = Embed (title = t .error_parse_color_title (color ), description = t .error_parse_color_example )
59- await reply (ctx , embed = embed )
60- return
60+ raise CommandError (t .error_parse_color_example (color ))
6161
62- img : Image = Image .new ("RGB" , (100 , 100 ), rgb )
63- with io .BytesIO () as image_binary :
64- img .save (image_binary , "PNG" )
65- image_binary .seek (0 )
66- embed : Embed = Embed (title = "Color Picker" , color = Colour (int (color_hex , 16 )))
67- embed .add_field (name = "HEX" , value = f"#{ color_hex } " )
68- embed .add_field (name = "RGB" , value = f"rgb{ rgb } " )
69- embed .add_field (name = "HSV" , value = f"hsv{ hsv } " )
70- embed .add_field (name = "HSL" , value = f"hsl{ hsl } " )
71- embed .set_image (url = "attachment://color.png" )
72- await reply (ctx , embed = embed , file = File (fp = image_binary , filename = "color.png" ))
62+ embed : Embed = Embed (title = "Color Picker" , color = Colour (int (color_hex , 16 )))
63+ embed .set_image (url = f"https://singlecolorimage.com/get/{ color_hex } /300x50" )
64+ embed .add_field (name = "HEX" , value = f"`#{ color_hex } `" )
65+ embed .add_field (name = "RGB" , value = f"`rgb{ rgb } `" )
66+ embed .add_field (name = "HSV" , value = f"`hsv{ hsv } `" )
67+ embed .add_field (name = "HSL" , value = f"`hsl{ hsl } `" )
68+ await reply (ctx , embed = embed )
0 commit comments