aboutsummaryrefslogtreecommitdiffstats
path: root/bot
diff options
context:
space:
mode:
authorGravatar bradtimmis <[email protected]>2021-09-11 09:57:13 -0400
committerGravatar Chris Lovering <[email protected]>2021-10-05 16:42:47 +0100
commit4e316e7991862db76b78e16019c02461c3c52fc2 (patch)
tree97c2ecff9fa3062c31b7ce811c238648e5b23158 /bot
parentAdded "colour information" and "colour conversion" features (diff)
Add fuzzy match function
I made a few changes, the biggest being the fuzzy match function to return a hex color code based on an input color name. Open items that I can think of so far: -Since the json file has color names and hex values, in order to use fuzzy matching for a color name the color must first be converted to hex. Currently there is only a rgb to anything function which returns values in a dictionary. -The main embed creation references the rgb_color before it is defined, should the command function be moved to the bottom of the file or just the main embed creation and sending? -When using the rgb mode, should the user be forced to do (r, g, b) or should the command handle an input of "r, g, b"? If you are reading this, thank you.
Diffstat (limited to 'bot')
-rw-r--r--bot/exts/utilities/color.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/bot/exts/utilities/color.py b/bot/exts/utilities/color.py
index 6abfc006..9e199dd4 100644
--- a/bot/exts/utilities/color.py
+++ b/bot/exts/utilities/color.py
@@ -1,4 +1,5 @@
import colorsys
+import json
import logging
import re
from io import BytesIO
@@ -6,7 +7,7 @@ from io import BytesIO
from PIL import Image, ImageColor
from discord import Embed, File
from discord.ext import commands
-# from rapidfuzz import process
+from rapidfuzz import process
from bot.bot import Bot
from bot.constants import Colours
@@ -23,6 +24,8 @@ ERROR_MSG = """The color code {user_color} is not a possible color combination.
\nHex: #000000-#FFFFFF
"""
+COLOR_LIST = "bot/resources/utilities/ryanzec_colours.json"
+
# define color command
class Color(commands.Cog):
@@ -46,7 +49,7 @@ class Color(commands.Cog):
else:
await ctx.send(
embed=Embed(
- title="An error has occured.",
+ title="There was an issue converting the hex color code.",
description=ERROR_MSG.format(user_color=user_color),
)
)
@@ -58,9 +61,10 @@ class Color(commands.Cog):
pass
elif mode.lower() == "cmyk":
pass
+ elif mode.lower() == "name":
+ color_name, hex_color = self.match_color(user_color)
else:
# mode is either None or an invalid code
- # need to handle whether user passes color name
if mode is None:
no_mode_embed = Embed(
title="No 'mode' was passed, please define a color code.",
@@ -70,7 +74,7 @@ class Color(commands.Cog):
return
wrong_mode_embed = Embed(
title=f"The color code {mode} is not a valid option",
- description="Possible modes are: Hex, RGB, HSV, HSL and CMYK.",
+ description="Possible modes are: Name, Hex, RGB, HSV, HSL and CMYK.",
color=Colours.soft_red,
)
await ctx.send(embed=wrong_mode_embed)
@@ -78,15 +82,15 @@ class Color(commands.Cog):
async with ctx.typing():
main_embed = Embed(
- title=user_color, # need to replace with fuzzymatch color name
+ title=color_name,
description='(Approx..)',
- color=hex_color,
+ color=rgb_color,
)
file = await self.create_thumbnail_attachment(rgb_color)
main_embed.set_thumbnail(url="attachment://color.png")
-
fields = self.get_color_fields(rgb_color)
+
for field in fields:
main_embed.add_field(
name=field['name'],
@@ -94,7 +98,7 @@ class Color(commands.Cog):
inline=False,
)
- await ctx.send(file=file, embed=main_embed)
+ await ctx.send(file=file, embed=main_embed)
@staticmethod
async def create_thumbnail_attachment(color: str) -> File:
@@ -192,6 +196,17 @@ class Color(commands.Cog):
# if user_color in color_lists:
# # fuzzy match for color
+ @staticmethod
+ def match_color(user_color: str) -> str:
+ """Use fuzzy matching to return a hex color code based on the user's input."""
+ with open(COLOR_LIST) as f:
+ color_list = json.load(f)
+ logger.debug(f"{type(color_list) = }")
+ match, certainty, _ = process.extractOne(query=user_color, choices=color_list.keys(), score_cutoff=50)
+ logger.debug(f"{match = }, {certainty = }")
+ hex_match = color_list[match]
+ logger.debug(f"{hex_match = }")
+ return match, hex_match
def setup(bot: Bot) -> None: