diff options
Diffstat (limited to 'bot/exts/utilities')
-rw-r--r-- | bot/exts/utilities/color.py | 140 |
1 files changed, 61 insertions, 79 deletions
diff --git a/bot/exts/utilities/color.py b/bot/exts/utilities/color.py index dd922bf9..1a4f7031 100644 --- a/bot/exts/utilities/color.py +++ b/bot/exts/utilities/color.py @@ -1,113 +1,95 @@ # imports import colorsys import logging +import re +from io import BytesIO -import PIL -from discord import Embed +from PIL import Image, ImageColor +from discord import Embed, File from discord.ext import commands from rapidfuzz import process from bot.bot import Bot from bot.constants import Colours -# Planning to use discord-flags, hence require changes to poetry.lock file -# from discord.ext import flags logger = logging.getLogger(__name__) -# constants if needed -# Color URLs - will be replaced by JSON file? -COLOR_JSON_PATH = ".bot//exts//resources//evergreen//" -COLOR_URL_XKCD = "https://xkcd.com/color/rgb/" -COLOR_URL_NAME_THAT_COLOR = "https://github.com/ryanzec/name-that-color/blob/master/lib/ntc.js#L116-L1681" - - -COLOR_ERROR = Embed( - title="Input color is not possible", - description="The color code {user_color} is not a possible color combination." - "\nThe range of possible values are: " - "\nRGB & HSV: 0-255" - "\nCMYK: 0-100%" - "\nHSL: 0-360 degrees" - "\nHex: #000000-#FFFFFF" -) -COLOR_EMBED = Embed( - title="{color_name}", - description="RGB" - "\n{RGB}" - "\nHSV" - "\n{HSV}" - "\nCMYK" - "\n{CMYK}" - "\nHSL" - "\n{HSL}" - "\nHex" - "\n{Hex}" -) + +ERROR_MSG = """The color code {user_color} is not a possible color combination. +\nThe range of possible values are: +\nRGB & HSV: 0-255 +\nCMYK: 0-100% +\nHSL: 0-360 degrees +\nHex: #000000-#FFFFFF +""" # define color command -class Color(commands.cog): +class Color(commands.Cog): """User initiated command to receive color information.""" def __init__(self, bot: Bot): self.bot = bot - # ? possible to use discord-flags to allow user to decide on color - # https://pypi.org/project/discord-flags/ - # @flags.add_flag("--rgb", type=str) - # @flags.add_flag("--hsv", type=str) - # @flags.add_flag("--cmyk", type=str) - # @flags.add_flag("--hsl", type=str) - # @flags.add_flag("--hex", type=str) - # @flags.add_flag("--name", type=str) - # @flags.command() - @commands.command(aliases=["color", "colour"]) + @commands.command(aliases=["colour"]) @commands.cooldown(1, 10, commands.cooldowns.BucketType.user) async def color(self, ctx: commands.Context, *, user_color: str) -> None: """Send information on input color code or color name.""" # need to check if user_color is RGB, HSV, CMYK, HSL, Hex or color name # should we assume the color is RGB if not defined? - # should discord tags be used? - # need to review discord.py V2.0 - - # TODO code to check if color code is possible - await ctx.send(embed=COLOR_ERROR.format(color=user_color)) - # await ctx.send(embed=COLOR_EMBED.format( - # RGB=color_dict["RGB"], - # HSV=color_dict["HSV"], - # HSL=color_dict["HSL"], - # CMYK=color_dict["CMYK"], - # HSL=color_dict["HSL"], - # Hex=color_dict["Hex"], - # color_name=color_dict["color_name"] - # ).set_image() # url for image? - # ) - - # TODO pass for now - pass + + if "#" in user_color: + logger.info(f"{user_color = }") + hex_match = re.search(r"^#(?:[0-9a-fA-F]{3}){1,2}$", user_color) + if hex_match: + hex_color = int(hex(int(user_color.replace("#", ""), 16)), 0) + logger.info(f"{hex_color = }") + rgb_color = ImageColor.getcolor(user_color, "RGB") + else: + await ctx.send(embed=Embed( + title="An error has occured.", + description=ERROR_MSG.format(user_color=user_color) + ) + ) + + elif "RGB" or "rgb" in user_color: + rgb_parse = user_color.split() + rgb = rgb_parse[1:].replace(", ", "") + logger.info(f"{rgb = }") + logger.info(f"{rgb[0] = }") + logger.info(f"{rgb[1] = }") + logger.info(f"{rgb[2] = }") + rgb_color = tuple(rgb) + hex_color = f"0x{int(rgb[0]):02x}{int(rgb[1]):02x}{int(rgb[2]):02x}" + + main_embed = Embed( + title=user_color, + color=hex_color, + ) + async with ctx.typing(): + file = await self._create_thumbnail_attachment(rgb_color) + main_embed.set_thumbnail(url="attachment://color.png") + + await ctx.send(file=file, embed=main_embed) + + async def _create_thumbnail_attachment(self, color: str) -> File: + """Generate a thumbnail from `color`.""" + + thumbnail = Image.new("RGB", (100, 100), color=color) + bufferedio = BytesIO() + thumbnail.save(bufferedio, format="PNG") + bufferedio.seek(0) + + file = File(bufferedio, filename="color.png") + + return file + # if user_color in color_lists: # # TODO fuzzy match for color # pass - async def color_converter(self, color: str, code_type: str) -> dict: - """Generate alternative color codes for use in the embed.""" - # TODO add code to take color and code type and return other types - # color_dict = { - # "RGB": color_RGB, - # "HSV": color_HSV, - # "HSL": color_HSL, - # "CMYK": color_CMYK, - # "HSL": color_HSL, - # "Hex": color_Hex, - # "color_name": color_name, - # } - pass - - async def photo_generator(self, color: str) -> None: - """Generate photo to use in embed.""" - # TODO need to find a way to store photo in cache to add to embed, then remove def setup(bot: Bot) -> None: |