3
3
import re
4
4
from typing import Any
5
5
6
- from PIL import ImageColor , Image
7
6
from discord import Embed , Colour , File
8
7
from discord .ext import commands
9
- from discord .ext .commands import Context
8
+ from discord .ext .commands import Context , CommandError
10
9
11
10
from PyDrocsid .cog import Cog
12
11
from PyDrocsid .command import reply
@@ -34,15 +33,18 @@ def _to_int(colors: tuple[Any, Any, Any]) -> tuple[int, ...]:
34
33
hsv : tuple [int , ...]
35
34
hsl : tuple [int , ...]
36
35
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
+
37
39
if color_re := self .RE_HEX .match (color ):
38
40
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 ) )
41
43
hsl = _to_int (colorsys .rgb_to_hls (* rgb ))
42
44
elif color_re := self .RE_RGB .match (color ):
43
45
rgb = _to_int ((color_re .group (1 ), color_re .group (2 ), color_re .group (3 )))
44
46
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 ) )
46
48
hsl = _to_int (colorsys .rgb_to_hls (* rgb ))
47
49
elif color_re := self .RE_HSV .match (color ):
48
50
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, ...]:
55
57
hsv = _to_int (colorsys .rgb_to_hsv (* rgb ))
56
58
color_hex = "{0:02x}{1:02x}{2:02x}" .format (* rgb )
57
59
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 ))
61
61
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