From edeb48ab0489c0ac0fcbf0a3a45dde11236f613c Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Wed, 3 Nov 2021 21:13:24 -0400 Subject: rename to colour --- bot/exts/utilities/colour.py | 230 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 bot/exts/utilities/colour.py (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py new file mode 100644 index 00000000..5ff1e2b8 --- /dev/null +++ b/bot/exts/utilities/colour.py @@ -0,0 +1,230 @@ +import colorsys +import json +import pathlib +import random +from io import BytesIO + +import discord +import rapidfuzz +from PIL import Image, ImageColor +from discord.ext import commands + +from bot.bot import Bot +from bot.exts.core.extensions import invoke_help_command + +THUMBNAIL_SIZE = (80, 80) + + +class Colour(commands.Cog): + """Cog for the Colour command.""" + + def __init__(self, bot: Bot): + self.bot = bot + with open(pathlib.Path("bot/resources/utilities/ryanzec_colours.json")) as f: + self.colour_mapping = json.load(f) + del self.colour_mapping['_'] # Delete source credit entry + + async def send_colour_response(self, ctx: commands.Context, rgb: tuple[int, int, int]) -> None: + """Create and send embed from user given colour information.""" + name = self._rgb_to_name(rgb) + if name is None: + name = "No match found" + + try: + colour_or_color = ctx.invoked_parents[0] + except IndexError: + colour_or_color = "colour" + + colour_mode = ctx.invoked_with + if colour_mode == "random": + input_colour = "random" + elif colour_mode in ("colour", "color"): + input_colour = rgb + else: + input_colour = ctx.args[2:][0] + + if colour_mode not in ("name", "hex", "random", "color", "colour"): + colour_mode = colour_mode.upper() + else: + colour_mode = colour_mode.title() + + colour_embed = discord.Embed( + title=colour_or_color.title(), + description=f"{colour_mode} information for `{name or input_colour}`.", + colour=discord.Color.from_rgb(*rgb) + ) + colour_conversions = self.get_colour_conversions(rgb) + for colour_space, value in colour_conversions.items(): + colour_embed.add_field( + name=colour_space, + value=f"`{value}`", + inline=True + ) + + thumbnail = Image.new("RGB", THUMBNAIL_SIZE, color=rgb) + buffer = BytesIO() + thumbnail.save(buffer, "PNG") + buffer.seek(0) + thumbnail_file = discord.File(buffer, filename="colour.png") + + colour_embed.set_thumbnail(url="attachment://colour.png") + + await ctx.send(file=thumbnail_file, embed=colour_embed) + + @commands.group(aliases=("color",), invoke_without_command=True) + async def colour(self, ctx: commands.Context, *, extra: str) -> None: + """User initiated command to create an embed that displays colour information.""" + if ctx.invoked_subcommand is None: + try: + extra_colour = ImageColor.getrgb(extra) + await self.send_colour_response(ctx, extra_colour) + except ValueError: + await invoke_help_command(ctx) + + @colour.command() + async def rgb(self, ctx: commands.Context, red: int, green: int, blue: int) -> None: + """Command to create an embed from an RGB input.""" + if any(c not in range(0, 256) for c in (red, green, blue)): + raise commands.BadArgument( + message=f"RGB values can only be from 0 to 255. User input was: `{red, green, blue}`." + ) + rgb_tuple = (red, green, blue) + await self.send_colour_response(ctx, rgb_tuple) + + @colour.command() + async def hsv(self, ctx: commands.Context, hue: int, saturation: int, value: int) -> None: + """Command to create an embed from an HSV input.""" + if (hue not in range(0, 361)) or any(c not in range(0, 101) for c in (saturation, value)): + raise commands.BadArgument( + message="Hue can only be from 0 to 360. Saturation and Value can only be from 0 to 100. " + f"User input was: `{hue, saturation, value}`." + ) + hsv_tuple = ImageColor.getrgb(f"hsv({hue}, {saturation}%, {value}%)") + await self.send_colour_response(ctx, hsv_tuple) + + @colour.command() + async def hsl(self, ctx: commands.Context, hue: int, saturation: int, lightness: int) -> None: + """Command to create an embed from an HSL input.""" + if (hue not in range(0, 361)) or any(c not in range(0, 101) for c in (saturation, lightness)): + raise commands.BadArgument( + message="Hue can only be from 0 to 360. Saturation and Lightness can only be from 0 to 100. " + f"User input was: `{hue, saturation, lightness}`." + ) + hsl_tuple = ImageColor.getrgb(f"hsl({hue}, {saturation}%, {lightness}%)") + await self.send_colour_response(ctx, hsl_tuple) + + @colour.command() + async def cmyk(self, ctx: commands.Context, cyan: int, magenta: int, yellow: int, key: int) -> None: + """Command to create an embed from a CMYK input.""" + if any(c not in range(0, 101) for c in (cyan, magenta, yellow, key)): + raise commands.BadArgument( + message=f"CMYK values can only be from 0 to 100. User input was: `{cyan, magenta, yellow, key}`." + ) + r = round(255 * (1 - (cyan / 100)) * (1 - (key / 100))) + g = round(255 * (1 - (magenta / 100)) * (1 - (key / 100))) + b = round(255 * (1 - (yellow / 100)) * (1 - (key / 100))) + await self.send_colour_response(ctx, (r, g, b)) + + @colour.command() + async def hex(self, ctx: commands.Context, hex_code: str) -> None: + """Command to create an embed from a HEX input.""" + if "#" not in hex_code: + hex_code = f"#{hex_code}" + hex_tuple = ImageColor.getrgb(hex_code) + await self.send_colour_response(ctx, hex_tuple) + + @colour.command() + async def name(self, ctx: commands.Context, user_colour_name: str) -> None: + """Command to create an embed from a name input.""" + hex_colour = self.match_colour_name(user_colour_name) + hex_tuple = ImageColor.getrgb(hex_colour) + await self.send_colour_response(ctx, hex_tuple) + + @colour.command() + async def random(self, ctx: commands.Context) -> None: + """Command to create an embed from a randomly chosen colour from the reference file.""" + hex_colour = random.choice(list(self.colour_mapping.values())) + hex_tuple = ImageColor.getrgb(f"#{hex_colour}") + await self.send_colour_response(ctx, hex_tuple) + + def get_colour_conversions(self, rgb: tuple[int, int, int]) -> dict[str, str]: + """Create a dictionary mapping of colour types and their values.""" + colour_name = self._rgb_to_name(rgb) + if colour_name is None: + colour_name = "No match found" + return { + "RGB": rgb, + "HSV": self._rgb_to_hsv(rgb), + "HSL": self._rgb_to_hsl(rgb), + "CMYK": self._rgb_to_cmyk(rgb), + "Hex": self._rgb_to_hex(rgb), + "Name": colour_name + } + + @staticmethod + def _rgb_to_hsv(rgb: tuple[int, int, int]) -> tuple[int, int, int]: + """Convert RGB values to HSV values.""" + rgb_list = [val / 255 for val in rgb] + h, s, v = colorsys.rgb_to_hsv(*rgb_list) + hsv = (round(h * 360), round(s * 100), round(v * 100)) + return hsv + + @staticmethod + def _rgb_to_hsl(rgb: tuple[int, int, int]) -> tuple[int, int, int]: + """Convert RGB values to HSL values.""" + rgb_list = [val / 255.0 for val in rgb] + h, l, s = colorsys.rgb_to_hls(*rgb_list) + hsl = (round(h * 360), round(s * 100), round(l * 100)) + return hsl + + @staticmethod + def _rgb_to_cmyk(rgb: tuple[int, int, int, int]) -> tuple[int, int, int, int]: + """Convert RGB values to CMYK values.""" + rgb_list = [val / 255.0 for val in rgb] + if not any(rgb_list): + return 0, 0, 0, 100 + k = 1 - max(rgb_list) + c = round((1 - rgb_list[0] - k) * 100 / (1 - k)) + m = round((1 - rgb_list[1] - k) * 100 / (1 - k)) + y = round((1 - rgb_list[2] - k) * 100 / (1 - k)) + cmyk = (c, m, y, round(k * 100)) + return cmyk + + @staticmethod + def _rgb_to_hex(rgb: tuple[int, int, int]) -> str: + """Convert RGB values to HEX code.""" + hex_ = ''.join([hex(val)[2:].zfill(2) for val in rgb]) + hex_code = f"#{hex_}".upper() + return hex_code + + def _rgb_to_name(self, rgb: tuple[int, int, int]) -> str: + """Convert RGB values to a fuzzy matched name.""" + input_hex_colour = self._rgb_to_hex(rgb) + try: + match, certainty, _ = rapidfuzz.process.extractOne( + query=input_hex_colour, + choices=self.colour_mapping.values(), + score_cutoff=80 + ) + colour_name = [name for name, hex_code in self.colour_mapping.items() if hex_code == match][0] + except TypeError: + colour_name = None + return colour_name + + def match_colour_name(self, input_colour_name: str) -> str: + """Convert a colour name to HEX code.""" + try: + match, certainty, _ = rapidfuzz.process.extractOne( + query=input_colour_name, + choices=self.colour_mapping.keys(), + score_cutoff=80 + ) + hex_match = f"#{self.colour_mapping[match]}" + except (ValueError, TypeError): + raise commands.BadArgument(message=f"No color found for: `{input_colour_name}`") + return hex_match + + +def setup(bot: Bot) -> None: + """Load the Colour cog.""" + bot.add_cog(Colour(bot)) -- cgit v1.2.3 From 4fed12e92a6bb7280e6b69af07267dd82b0d6d0d Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Tue, 9 Nov 2021 08:03:20 -0500 Subject: fix: type hinting _rgb_to_cmyk --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 5ff1e2b8..c2a5e7e7 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -178,7 +178,7 @@ class Colour(commands.Cog): return hsl @staticmethod - def _rgb_to_cmyk(rgb: tuple[int, int, int, int]) -> tuple[int, int, int, int]: + def _rgb_to_cmyk(rgb: tuple[int, int, int]) -> tuple[int, int, int, int]: """Convert RGB values to CMYK values.""" rgb_list = [val / 255.0 for val in rgb] if not any(rgb_list): -- cgit v1.2.3 From 2bb00fbf680d49c92bb40af71659489801cc0f99 Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Tue, 9 Nov 2021 19:44:18 -0500 Subject: fix: handle alpha values in hex code Co-authored-by: Sn4u <35849006+Sn4u@users.noreply.github.com> --- bot/exts/utilities/colour.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index c2a5e7e7..7b128393 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -130,7 +130,13 @@ class Colour(commands.Cog): """Command to create an embed from a HEX input.""" if "#" not in hex_code: hex_code = f"#{hex_code}" + if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits+"#" for _ in hex_code): + raise commands.BadArgument( + message=f"HEX values must be hexadecimal and take the form *#RRGGBB* or *#RGB*. User input was: `{hex_code}`.") + hex_tuple = ImageColor.getrgb(hex_code) + if len(hex_tuple) == 4: + hex_tuple = hex_tuple[:-1] # color must be RGB. If RGBA, we remove the alpha value await self.send_colour_response(ctx, hex_tuple) @colour.command() -- cgit v1.2.3 From 385cb3aacf60bdaea67fe59890e9c5d894f9d65a Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 19:48:09 -0500 Subject: fix: add import, handle no name match in embed -Added `import string` to use the `string.hexdigits` method to check hex codes. -Handled bug where no name match found would be repeated in the embed in the first line as well as the value for the Name field. --- bot/exts/utilities/colour.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 7b128393..4528615b 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -2,6 +2,7 @@ import colorsys import json import pathlib import random +import string from io import BytesIO import discord @@ -27,8 +28,6 @@ class Colour(commands.Cog): async def send_colour_response(self, ctx: commands.Context, rgb: tuple[int, int, int]) -> None: """Create and send embed from user given colour information.""" name = self._rgb_to_name(rgb) - if name is None: - name = "No match found" try: colour_or_color = ctx.invoked_parents[0] @@ -132,7 +131,9 @@ class Colour(commands.Cog): hex_code = f"#{hex_code}" if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits+"#" for _ in hex_code): raise commands.BadArgument( - message=f"HEX values must be hexadecimal and take the form *#RRGGBB* or *#RGB*. User input was: `{hex_code}`.") + message="HEX values must be hexadecimal and take the form *#RRGGBB* or *#RGB*. " + f"User input was: `{hex_code}`." + ) hex_tuple = ImageColor.getrgb(hex_code) if len(hex_tuple) == 4: -- cgit v1.2.3 From 57c13ff35801281b5e592c9f6c195093a2fc5c7b Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 20:20:19 -0500 Subject: bug: handle multi word name entries and full input --- bot/exts/utilities/colour.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 4528615b..cede3312 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -39,8 +39,10 @@ class Colour(commands.Cog): input_colour = "random" elif colour_mode in ("colour", "color"): input_colour = rgb + elif colour_mode == "name": + input_colour = ctx.kwargs["user_colour_name"] else: - input_colour = ctx.args[2:][0] + input_colour = tuple(ctx.args[2:]) if colour_mode not in ("name", "hex", "random", "color", "colour"): colour_mode = colour_mode.upper() @@ -141,7 +143,7 @@ class Colour(commands.Cog): await self.send_colour_response(ctx, hex_tuple) @colour.command() - async def name(self, ctx: commands.Context, user_colour_name: str) -> None: + async def name(self, ctx: commands.Context, *, user_colour_name: str) -> None: """Command to create an embed from a name input.""" hex_colour = self.match_colour_name(user_colour_name) hex_tuple = ImageColor.getrgb(hex_colour) -- cgit v1.2.3 From 1de5e9b905310eb4745b9b5c83561dea3a289ea9 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 20:39:39 -0500 Subject: chore: pull hex match out of try/except block --- bot/exts/utilities/colour.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index cede3312..656dddba 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -228,10 +228,9 @@ class Colour(commands.Cog): choices=self.colour_mapping.keys(), score_cutoff=80 ) - hex_match = f"#{self.colour_mapping[match]}" except (ValueError, TypeError): raise commands.BadArgument(message=f"No color found for: `{input_colour_name}`") - return hex_match + return f"#{self.colour_mapping[match]}" def setup(bot: Bot) -> None: -- cgit v1.2.3 From 93e6e96c89e621db6e463f4d5716533fd85cd96f Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 20:44:28 -0500 Subject: chore: re-arrange command invocation with try/except --- bot/exts/utilities/colour.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 656dddba..e69930f4 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -75,12 +75,14 @@ class Colour(commands.Cog): @commands.group(aliases=("color",), invoke_without_command=True) async def colour(self, ctx: commands.Context, *, extra: str) -> None: """User initiated command to create an embed that displays colour information.""" - if ctx.invoked_subcommand is None: - try: - extra_colour = ImageColor.getrgb(extra) - await self.send_colour_response(ctx, extra_colour) - except ValueError: - await invoke_help_command(ctx) + if ctx.invoked_subcommand: + return + + try: + extra_colour = ImageColor.getrgb(extra) + await self.send_colour_response(ctx, extra_colour) + except ValueError: + await invoke_help_command(ctx) @colour.command() async def rgb(self, ctx: commands.Context, red: int, green: int, blue: int) -> None: -- cgit v1.2.3 From c70a4e7670805ca774d1c0d634e86dc6759d8669 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 21:22:49 -0500 Subject: chore: remove 0 from range and change to " --- bot/exts/utilities/colour.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index e69930f4..9cbf3394 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -87,7 +87,7 @@ class Colour(commands.Cog): @colour.command() async def rgb(self, ctx: commands.Context, red: int, green: int, blue: int) -> None: """Command to create an embed from an RGB input.""" - if any(c not in range(0, 256) for c in (red, green, blue)): + if any(c not in range(256) for c in (red, green, blue)): raise commands.BadArgument( message=f"RGB values can only be from 0 to 255. User input was: `{red, green, blue}`." ) @@ -97,7 +97,7 @@ class Colour(commands.Cog): @colour.command() async def hsv(self, ctx: commands.Context, hue: int, saturation: int, value: int) -> None: """Command to create an embed from an HSV input.""" - if (hue not in range(0, 361)) or any(c not in range(0, 101) for c in (saturation, value)): + if (hue not in range(361)) or any(c not in range(101) for c in (saturation, value)): raise commands.BadArgument( message="Hue can only be from 0 to 360. Saturation and Value can only be from 0 to 100. " f"User input was: `{hue, saturation, value}`." @@ -108,7 +108,7 @@ class Colour(commands.Cog): @colour.command() async def hsl(self, ctx: commands.Context, hue: int, saturation: int, lightness: int) -> None: """Command to create an embed from an HSL input.""" - if (hue not in range(0, 361)) or any(c not in range(0, 101) for c in (saturation, lightness)): + if (hue not in range(361)) or any(c not in range(101) for c in (saturation, lightness)): raise commands.BadArgument( message="Hue can only be from 0 to 360. Saturation and Lightness can only be from 0 to 100. " f"User input was: `{hue, saturation, lightness}`." @@ -119,7 +119,7 @@ class Colour(commands.Cog): @colour.command() async def cmyk(self, ctx: commands.Context, cyan: int, magenta: int, yellow: int, key: int) -> None: """Command to create an embed from a CMYK input.""" - if any(c not in range(0, 101) for c in (cyan, magenta, yellow, key)): + if any(c not in range(101) for c in (cyan, magenta, yellow, key)): raise commands.BadArgument( message=f"CMYK values can only be from 0 to 100. User input was: `{cyan, magenta, yellow, key}`." ) @@ -204,7 +204,7 @@ class Colour(commands.Cog): @staticmethod def _rgb_to_hex(rgb: tuple[int, int, int]) -> str: """Convert RGB values to HEX code.""" - hex_ = ''.join([hex(val)[2:].zfill(2) for val in rgb]) + hex_ = "".join([hex(val)[2:].zfill(2) for val in rgb]) hex_code = f"#{hex_}".upper() return hex_code -- cgit v1.2.3 From 524d33a9909697386283d966dcda1dc712a16fb0 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Tue, 9 Nov 2021 21:55:49 -0500 Subject: fix: replace 'random' in embed with color mode --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 9cbf3394..7e5065cd 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -36,7 +36,7 @@ class Colour(commands.Cog): colour_mode = ctx.invoked_with if colour_mode == "random": - input_colour = "random" + colour_mode = colour_or_color elif colour_mode in ("colour", "color"): input_colour = rgb elif colour_mode == "name": -- cgit v1.2.3 From 6f9da835444e0d357edc06839d9b98eafe72af30 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Wed, 10 Nov 2021 06:46:55 -0500 Subject: fix: handle user hex in embed --- bot/exts/utilities/colour.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 7e5065cd..ec7545cd 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -41,6 +41,8 @@ class Colour(commands.Cog): input_colour = rgb elif colour_mode == "name": input_colour = ctx.kwargs["user_colour_name"] + elif colour_mode == "hex": + input_colour = ctx.args[2:][0] else: input_colour = tuple(ctx.args[2:]) -- cgit v1.2.3 From b9b1d8c10e999e16cbd067dc49710634e23e6bd7 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Wed, 10 Nov 2021 06:48:05 -0500 Subject: fix: change kwarg to color_input instead of extra --- bot/exts/utilities/colour.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index ec7545cd..1e64993c 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -75,13 +75,13 @@ class Colour(commands.Cog): await ctx.send(file=thumbnail_file, embed=colour_embed) @commands.group(aliases=("color",), invoke_without_command=True) - async def colour(self, ctx: commands.Context, *, extra: str) -> None: + async def colour(self, ctx: commands.Context, *, color_input: str) -> None: """User initiated command to create an embed that displays colour information.""" if ctx.invoked_subcommand: return try: - extra_colour = ImageColor.getrgb(extra) + extra_colour = ImageColor.getrgb(color_input) await self.send_colour_response(ctx, extra_colour) except ValueError: await invoke_help_command(ctx) -- cgit v1.2.3 From 6b21ffaef0e545d1710f2703c450c5492e100be7 Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Wed, 10 Nov 2021 08:37:18 -0500 Subject: test: UI/UX updates, not tested yet --- bot/exts/utilities/colour.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 1e64993c..259e3394 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -4,6 +4,7 @@ import pathlib import random import string from io import BytesIO +from typing import Optional, Union import discord import rapidfuzz @@ -75,8 +76,11 @@ class Colour(commands.Cog): await ctx.send(file=thumbnail_file, embed=colour_embed) @commands.group(aliases=("color",), invoke_without_command=True) - async def colour(self, ctx: commands.Context, *, color_input: str) -> None: + async def colour(self, ctx: commands.Context, *, color_input: Optional[str] = None) -> None: """User initiated command to create an embed that displays colour information.""" + if color_input is None: + await self.random() + if ctx.invoked_subcommand: return @@ -133,13 +137,17 @@ class Colour(commands.Cog): @colour.command() async def hex(self, ctx: commands.Context, hex_code: str) -> None: """Command to create an embed from a HEX input.""" - if "#" not in hex_code: + if hex_code[0] != "#": hex_code = f"#{hex_code}" - if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits+"#" for _ in hex_code): - raise commands.BadArgument( - message="HEX values must be hexadecimal and take the form *#RRGGBB* or *#RGB*. " - f"User input was: `{hex_code}`." + + if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits for _ in hex_code[1:]): + hex_error_embed = discord.Embed( + title="The input hex code is not valid.", + message=f"Cannot convert `{hex_code}` to a recognizable Hex format. " + "Hex values must be hexadecimal and take the form *#RRGGBB* or *#RGB*.", + colour=discord.Colour.dark_red() ) + await ctx.send(hex_error_embed) hex_tuple = ImageColor.getrgb(hex_code) if len(hex_tuple) == 4: @@ -149,7 +157,7 @@ class Colour(commands.Cog): @colour.command() async def name(self, ctx: commands.Context, *, user_colour_name: str) -> None: """Command to create an embed from a name input.""" - hex_colour = self.match_colour_name(user_colour_name) + hex_colour = await self.match_colour_name(ctx, user_colour_name) hex_tuple = ImageColor.getrgb(hex_colour) await self.send_colour_response(ctx, hex_tuple) @@ -224,7 +232,7 @@ class Colour(commands.Cog): colour_name = None return colour_name - def match_colour_name(self, input_colour_name: str) -> str: + async def match_colour_name(self, ctx: commands.Context, input_colour_name: str) -> Union[str, None]: """Convert a colour name to HEX code.""" try: match, certainty, _ = rapidfuzz.process.extractOne( @@ -233,7 +241,12 @@ class Colour(commands.Cog): score_cutoff=80 ) except (ValueError, TypeError): - raise commands.BadArgument(message=f"No color found for: `{input_colour_name}`") + name_error_embed = discord.Embed( + title="No colour match found.", + description=f"No color found for: `{input_colour_name}`", + colour=discord.Color.dark_red() + ) + await ctx.send(name_error_embed) return f"#{self.colour_mapping[match]}" -- cgit v1.2.3 From f2735273ceb7a2eabf3beee93e1ed21635b4bfd0 Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Wed, 10 Nov 2021 08:40:46 -0500 Subject: test: return after default random invocation --- bot/exts/utilities/colour.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 259e3394..e0607bf5 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -80,6 +80,7 @@ class Colour(commands.Cog): """User initiated command to create an embed that displays colour information.""" if color_input is None: await self.random() + return if ctx.invoked_subcommand: return -- cgit v1.2.3 From 87711b7b90110fc9d62d1ef880f69c1fbe6366b6 Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Wed, 10 Nov 2021 14:59:33 -0500 Subject: test: correct embed descriptions --- bot/exts/utilities/colour.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index e0607bf5..08a6973b 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -144,11 +144,11 @@ class Colour(commands.Cog): if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits for _ in hex_code[1:]): hex_error_embed = discord.Embed( title="The input hex code is not valid.", - message=f"Cannot convert `{hex_code}` to a recognizable Hex format. " + description=f"Cannot convert `{hex_code}` to a recognizable Hex format. " "Hex values must be hexadecimal and take the form *#RRGGBB* or *#RGB*.", colour=discord.Colour.dark_red() ) - await ctx.send(hex_error_embed) + await ctx.send(embed=hex_error_embed) hex_tuple = ImageColor.getrgb(hex_code) if len(hex_tuple) == 4: @@ -247,7 +247,7 @@ class Colour(commands.Cog): description=f"No color found for: `{input_colour_name}`", colour=discord.Color.dark_red() ) - await ctx.send(name_error_embed) + await ctx.send(embed=name_error_embed) return f"#{self.colour_mapping[match]}" -- cgit v1.2.3 From aaad72a40b21b9d3b1d43d67a3206923722d8575 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Wed, 10 Nov 2021 19:08:58 -0500 Subject: cleanup: finalize reviews requested changes -Change _ to `digit` -Remove redundant "Command" from docstrings. Changed to "Create embed from ..." -Change hex command custom embed to `BadArgument` for consistency --- bot/exts/utilities/colour.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 08a6973b..9f7bedb5 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -79,7 +79,7 @@ class Colour(commands.Cog): async def colour(self, ctx: commands.Context, *, color_input: Optional[str] = None) -> None: """User initiated command to create an embed that displays colour information.""" if color_input is None: - await self.random() + await self.random(ctx) return if ctx.invoked_subcommand: @@ -93,7 +93,7 @@ class Colour(commands.Cog): @colour.command() async def rgb(self, ctx: commands.Context, red: int, green: int, blue: int) -> None: - """Command to create an embed from an RGB input.""" + """Create an embed from an RGB input.""" if any(c not in range(256) for c in (red, green, blue)): raise commands.BadArgument( message=f"RGB values can only be from 0 to 255. User input was: `{red, green, blue}`." @@ -103,7 +103,7 @@ class Colour(commands.Cog): @colour.command() async def hsv(self, ctx: commands.Context, hue: int, saturation: int, value: int) -> None: - """Command to create an embed from an HSV input.""" + """Create an embed from an HSV input.""" if (hue not in range(361)) or any(c not in range(101) for c in (saturation, value)): raise commands.BadArgument( message="Hue can only be from 0 to 360. Saturation and Value can only be from 0 to 100. " @@ -114,7 +114,7 @@ class Colour(commands.Cog): @colour.command() async def hsl(self, ctx: commands.Context, hue: int, saturation: int, lightness: int) -> None: - """Command to create an embed from an HSL input.""" + """Create an embed from an HSL input.""" if (hue not in range(361)) or any(c not in range(101) for c in (saturation, lightness)): raise commands.BadArgument( message="Hue can only be from 0 to 360. Saturation and Lightness can only be from 0 to 100. " @@ -125,7 +125,7 @@ class Colour(commands.Cog): @colour.command() async def cmyk(self, ctx: commands.Context, cyan: int, magenta: int, yellow: int, key: int) -> None: - """Command to create an embed from a CMYK input.""" + """Create an embed from a CMYK input.""" if any(c not in range(101) for c in (cyan, magenta, yellow, key)): raise commands.BadArgument( message=f"CMYK values can only be from 0 to 100. User input was: `{cyan, magenta, yellow, key}`." @@ -137,18 +137,15 @@ class Colour(commands.Cog): @colour.command() async def hex(self, ctx: commands.Context, hex_code: str) -> None: - """Command to create an embed from a HEX input.""" + """Create an embed from a HEX input.""" if hex_code[0] != "#": hex_code = f"#{hex_code}" - if len(hex_code) not in (4, 5, 7, 9) or any(_ not in string.hexdigits for _ in hex_code[1:]): - hex_error_embed = discord.Embed( - title="The input hex code is not valid.", - description=f"Cannot convert `{hex_code}` to a recognizable Hex format. " - "Hex values must be hexadecimal and take the form *#RRGGBB* or *#RGB*.", - colour=discord.Colour.dark_red() + if len(hex_code) not in (4, 5, 7, 9) or any(digit not in string.hexdigits for digit in hex_code[1:]): + raise commands.BadArgument( + message=f"Cannot convert `{hex_code}` to a recognizable Hex format. " + "Hex values must be hexadecimal and take the form *#RRGGBB* or *#RGB*." ) - await ctx.send(embed=hex_error_embed) hex_tuple = ImageColor.getrgb(hex_code) if len(hex_tuple) == 4: @@ -157,14 +154,14 @@ class Colour(commands.Cog): @colour.command() async def name(self, ctx: commands.Context, *, user_colour_name: str) -> None: - """Command to create an embed from a name input.""" + """Create an embed from a name input.""" hex_colour = await self.match_colour_name(ctx, user_colour_name) hex_tuple = ImageColor.getrgb(hex_colour) await self.send_colour_response(ctx, hex_tuple) @colour.command() async def random(self, ctx: commands.Context) -> None: - """Command to create an embed from a randomly chosen colour from the reference file.""" + """Create an embed from a randomly chosen colour from the reference file.""" hex_colour = random.choice(list(self.colour_mapping.values())) hex_tuple = ImageColor.getrgb(f"#{hex_colour}") await self.send_colour_response(ctx, hex_tuple) -- cgit v1.2.3 From 231ad0e58ee7b2eb58f48833abe469bf61582bdc Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Wed, 10 Nov 2021 19:20:56 -0500 Subject: cleanup: change main command docstring to be more clear --- bot/exts/utilities/colour.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 9f7bedb5..b233fc1e 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -77,7 +77,11 @@ class Colour(commands.Cog): @commands.group(aliases=("color",), invoke_without_command=True) async def colour(self, ctx: commands.Context, *, color_input: Optional[str] = None) -> None: - """User initiated command to create an embed that displays colour information.""" + """ + Create an embed that displays colour information. + + If no subcommand is called, a randomly selected colour will be selected and shown. + """ if color_input is None: await self.random(ctx) return -- cgit v1.2.3 From ed1fb463c4e04fa4628e46df33144a4b5c579b82 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Thu, 11 Nov 2021 16:54:10 -0500 Subject: fix: remove async call for match_colour_name --- bot/exts/utilities/colour.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index b233fc1e..ed69beaf 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -159,7 +159,15 @@ class Colour(commands.Cog): @colour.command() async def name(self, ctx: commands.Context, *, user_colour_name: str) -> None: """Create an embed from a name input.""" - hex_colour = await self.match_colour_name(ctx, user_colour_name) + hex_colour = self.match_colour_name(ctx, user_colour_name) + if hex_colour is None: + name_error_embed = discord.Embed( + title="No colour match found.", + description=f"No color found for: `{user_colour_name}`", + colour=discord.Color.dark_red() + ) + await ctx.send(embed=name_error_embed) + return hex_tuple = ImageColor.getrgb(hex_colour) await self.send_colour_response(ctx, hex_tuple) @@ -234,7 +242,7 @@ class Colour(commands.Cog): colour_name = None return colour_name - async def match_colour_name(self, ctx: commands.Context, input_colour_name: str) -> Union[str, None]: + def match_colour_name(self, ctx: commands.Context, input_colour_name: str) -> Union[str, None]: """Convert a colour name to HEX code.""" try: match, certainty, _ = rapidfuzz.process.extractOne( @@ -243,12 +251,7 @@ class Colour(commands.Cog): score_cutoff=80 ) except (ValueError, TypeError): - name_error_embed = discord.Embed( - title="No colour match found.", - description=f"No color found for: `{input_colour_name}`", - colour=discord.Color.dark_red() - ) - await ctx.send(embed=name_error_embed) + return None return f"#{self.colour_mapping[match]}" -- cgit v1.2.3 From a8aebd4fe051052f52b68c822d0762c56eba9591 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Thu, 11 Nov 2021 16:58:09 -0500 Subject: fix: change color to Colour in comment --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index ed69beaf..a35b393d 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -153,7 +153,7 @@ class Colour(commands.Cog): hex_tuple = ImageColor.getrgb(hex_code) if len(hex_tuple) == 4: - hex_tuple = hex_tuple[:-1] # color must be RGB. If RGBA, we remove the alpha value + hex_tuple = hex_tuple[:-1] # Colour must be RGB. If RGBA, we remove the alpha value await self.send_colour_response(ctx, hex_tuple) @colour.command() -- cgit v1.2.3 From 36a316f8ca27b35325ab31d575cc944a72f22c01 Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Thu, 11 Nov 2021 17:00:05 -0500 Subject: update: remove redundancy in docstring Co-authored-by: Hedy Li --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index ed69beaf..3b5c8681 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -80,7 +80,7 @@ class Colour(commands.Cog): """ Create an embed that displays colour information. - If no subcommand is called, a randomly selected colour will be selected and shown. + If no subcommand is called, a randomly selected colour will be shown. """ if color_input is None: await self.random(ctx) -- cgit v1.2.3 From 1914905bdc7bd591ff906562ee0496346d37a045 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Thu, 11 Nov 2021 17:02:45 -0500 Subject: fix: remove alpha values in embed for hex --- bot/exts/utilities/colour.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index c865859a..d438fa27 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -43,7 +43,7 @@ class Colour(commands.Cog): elif colour_mode == "name": input_colour = ctx.kwargs["user_colour_name"] elif colour_mode == "hex": - input_colour = ctx.args[2:][0] + input_colour = ctx.args[2:][0][0:-2] else: input_colour = tuple(ctx.args[2:]) @@ -80,7 +80,7 @@ class Colour(commands.Cog): """ Create an embed that displays colour information. - If no subcommand is called, a randomly selected colour will be shown. + If no subcommand is called, a randomly selected colour will be selected and shown. """ if color_input is None: await self.random(ctx) -- cgit v1.2.3 From 6b3ac76a7e5e63503bbba139538e52d23480b3fc Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Thu, 11 Nov 2021 17:07:15 -0500 Subject: update: remove redundancy in dosctring --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index d438fa27..6772fa1f 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -80,7 +80,7 @@ class Colour(commands.Cog): """ Create an embed that displays colour information. - If no subcommand is called, a randomly selected colour will be selected and shown. + If no subcommand is called, a randomly selected colour will be shown. """ if color_input is None: await self.random(ctx) -- cgit v1.2.3 From 45ac010fd6d2f8f9774cc5f408aa31c40fac42e6 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Fri, 12 Nov 2021 10:05:15 -0500 Subject: fix: check length of hex before strip --- bot/exts/utilities/colour.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 6772fa1f..66df5e0b 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -43,7 +43,9 @@ class Colour(commands.Cog): elif colour_mode == "name": input_colour = ctx.kwargs["user_colour_name"] elif colour_mode == "hex": - input_colour = ctx.args[2:][0][0:-2] + input_colour = ctx.args[2:][0] + if len(input_colour) >= 7: + input_colour = input_colour[0:-2] else: input_colour = tuple(ctx.args[2:]) -- cgit v1.2.3 From 5b3a23fe7b81d07c842271dc1947aa60133a2f8d Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:08:21 -0500 Subject: fix: replace Union with Optional type hint Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 66df5e0b..5cd01fb5 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -244,7 +244,7 @@ class Colour(commands.Cog): colour_name = None return colour_name - def match_colour_name(self, ctx: commands.Context, input_colour_name: str) -> Union[str, None]: + def match_colour_name(self, ctx: commands.Context, input_colour_name: str) -> Optional[str]: """Convert a colour name to HEX code.""" try: match, certainty, _ = rapidfuzz.process.extractOne( -- cgit v1.2.3 From 8bbef57ccda68c388cf3d4b4fbc90fedf0e8597e Mon Sep 17 00:00:00 2001 From: brad90four <42116429+brad90four@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:08:42 -0500 Subject: fix: bare return instead of explicit None Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 5cd01fb5..c7edec0d 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -253,7 +253,7 @@ class Colour(commands.Cog): score_cutoff=80 ) except (ValueError, TypeError): - return None + return return f"#{self.colour_mapping[match]}" -- cgit v1.2.3 From 6d5505366313b2ddb9d1a8f11b758e043e466ece Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Fri, 12 Nov 2021 10:10:12 -0500 Subject: fix: remove equal sign from hex length check --- bot/exts/utilities/colour.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index c7edec0d..66dbfa30 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -4,7 +4,7 @@ import pathlib import random import string from io import BytesIO -from typing import Optional, Union +from typing import Optional import discord import rapidfuzz @@ -44,7 +44,7 @@ class Colour(commands.Cog): input_colour = ctx.kwargs["user_colour_name"] elif colour_mode == "hex": input_colour = ctx.args[2:][0] - if len(input_colour) >= 7: + if len(input_colour) > 7: input_colour = input_colour[0:-2] else: input_colour = tuple(ctx.args[2:]) -- cgit v1.2.3 From e7923ed258ed7bf51cafbeb030df265a0992b1f1 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Sat, 13 Nov 2021 09:44:03 -0500 Subject: fix: remove unnecessary return in main command --- bot/exts/utilities/colour.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 66dbfa30..0681e807 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -88,9 +88,6 @@ class Colour(commands.Cog): await self.random(ctx) return - if ctx.invoked_subcommand: - return - try: extra_colour = ImageColor.getrgb(color_input) await self.send_colour_response(ctx, extra_colour) -- cgit v1.2.3 From e87e037e772fa2319c786210f176fd5088f00131 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Sat, 13 Nov 2021 10:09:04 -0500 Subject: fix: update type hint, color to colour, embed wording --- bot/exts/utilities/colour.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 0681e807..e33f65c6 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -29,7 +29,6 @@ class Colour(commands.Cog): async def send_colour_response(self, ctx: commands.Context, rgb: tuple[int, int, int]) -> None: """Create and send embed from user given colour information.""" name = self._rgb_to_name(rgb) - try: colour_or_color = ctx.invoked_parents[0] except IndexError: @@ -38,8 +37,9 @@ class Colour(commands.Cog): colour_mode = ctx.invoked_with if colour_mode == "random": colour_mode = colour_or_color + input_colour = name elif colour_mode in ("colour", "color"): - input_colour = rgb + input_colour = name elif colour_mode == "name": input_colour = ctx.kwargs["user_colour_name"] elif colour_mode == "hex": @@ -55,8 +55,8 @@ class Colour(commands.Cog): colour_mode = colour_mode.title() colour_embed = discord.Embed( - title=colour_or_color.title(), - description=f"{colour_mode} information for `{name or input_colour}`.", + title=f"{name or input_colour}", + description=f"{colour_or_color.title()} information for {colour_mode} `{input_colour or name}`.", colour=discord.Color.from_rgb(*rgb) ) colour_conversions = self.get_colour_conversions(rgb) @@ -78,18 +78,18 @@ class Colour(commands.Cog): await ctx.send(file=thumbnail_file, embed=colour_embed) @commands.group(aliases=("color",), invoke_without_command=True) - async def colour(self, ctx: commands.Context, *, color_input: Optional[str] = None) -> None: + async def colour(self, ctx: commands.Context, *, colour_input: Optional[str] = None) -> None: """ Create an embed that displays colour information. If no subcommand is called, a randomly selected colour will be shown. """ - if color_input is None: + if colour_input is None: await self.random(ctx) return try: - extra_colour = ImageColor.getrgb(color_input) + extra_colour = ImageColor.getrgb(colour_input) await self.send_colour_response(ctx, extra_colour) except ValueError: await invoke_help_command(ctx) @@ -162,7 +162,7 @@ class Colour(commands.Cog): if hex_colour is None: name_error_embed = discord.Embed( title="No colour match found.", - description=f"No color found for: `{user_colour_name}`", + description=f"No colour found for: `{user_colour_name}`", colour=discord.Color.dark_red() ) await ctx.send(embed=name_error_embed) @@ -172,7 +172,7 @@ class Colour(commands.Cog): @colour.command() async def random(self, ctx: commands.Context) -> None: - """Create an embed from a randomly chosen colour from the reference file.""" + """Create an embed from a randomly chosen colour.""" hex_colour = random.choice(list(self.colour_mapping.values())) hex_tuple = ImageColor.getrgb(f"#{hex_colour}") await self.send_colour_response(ctx, hex_tuple) @@ -227,7 +227,7 @@ class Colour(commands.Cog): hex_code = f"#{hex_}".upper() return hex_code - def _rgb_to_name(self, rgb: tuple[int, int, int]) -> str: + def _rgb_to_name(self, rgb: tuple[int, int, int]) -> Optional[str]: """Convert RGB values to a fuzzy matched name.""" input_hex_colour = self._rgb_to_hex(rgb) try: -- cgit v1.2.3 From e9cda06c2ab147ace8e64a27503cf99e17948418 Mon Sep 17 00:00:00 2001 From: bradtimmis Date: Sat, 13 Nov 2021 10:17:00 -0500 Subject: fix: handle bare command embed title --- bot/exts/utilities/colour.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index e33f65c6..7c83fc66 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -39,7 +39,7 @@ class Colour(commands.Cog): colour_mode = colour_or_color input_colour = name elif colour_mode in ("colour", "color"): - input_colour = name + input_colour = ctx.kwargs["colour_input"] elif colour_mode == "name": input_colour = ctx.kwargs["user_colour_name"] elif colour_mode == "hex": -- cgit v1.2.3 From 55f008ee03c15b24551bdc5509459337df9acd8e Mon Sep 17 00:00:00 2001 From: aru Date: Wed, 15 Dec 2021 18:15:16 -0500 Subject: minor: allow color command in dev-media (#944) Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> --- bot/exts/utilities/colour.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'bot/exts/utilities/colour.py') diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 7c83fc66..ee6bad93 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -11,8 +11,10 @@ import rapidfuzz from PIL import Image, ImageColor from discord.ext import commands +from bot import constants from bot.bot import Bot from bot.exts.core.extensions import invoke_help_command +from bot.utils.decorators import whitelist_override THUMBNAIL_SIZE = (80, 80) @@ -78,6 +80,11 @@ class Colour(commands.Cog): await ctx.send(file=thumbnail_file, embed=colour_embed) @commands.group(aliases=("color",), invoke_without_command=True) + @whitelist_override( + channels=constants.WHITELISTED_CHANNELS, + roles=constants.STAFF_ROLES, + categories=[constants.Categories.development, constants.Categories.media] + ) async def colour(self, ctx: commands.Context, *, colour_input: Optional[str] = None) -> None: """ Create an embed that displays colour information. -- cgit v1.2.3