[RSS] Better hex code matching

This commit is contained in:
aikaterna
2020-09-22 01:50:08 -04:00
parent 3d59573ca8
commit 02fccf2edd
2 changed files with 17 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
import discord
import re
from scipy.spatial import KDTree
import webcolors
@@ -6,25 +7,17 @@ import webcolors
class Color:
"""Helper for color handling."""
@staticmethod
async def _color_converter(hex_code_or_color_word: str):
async def _color_converter(self, hex_code_or_color_word: str):
"""
Used for user input on rss embed color
Input: discord.Color name, CSS3 color name, 0xFFFFFF, #FFFFFF, FFFFFF
Output: 0xFFFFFF
"""
# #FFFFFF to 0xFFFFFF
hex_code = hex_code_or_color_word.replace("#", "0x")
# FFFFFF to 0xFFFFFF
# 0xFFFFFF checking
try:
int(hex_code, 16)
if hex_code[:2] != "0x":
if len(hex_code) == 6:
hex_code = f"0x{hex_code}"
except ValueError:
pass
# #FFFFFF and FFFFFF to 0xFFFFFF
hex_match = re.match(r"#?[a-f0-9]{6}", hex_code_or_color_word.lower())
if hex_match:
hex_code = f"0x{hex_code_or_color_word.lstrip('#')}"
return hex_code
# discord.Color checking
if hasattr(discord.Color, hex_code_or_color_word):
@@ -40,7 +33,7 @@ class Color:
except ValueError:
pass
return hex_code
return None
async def _hex_to_css3_name(self, hex_code: str):
"""

View File

@@ -25,7 +25,7 @@ from .tag_type import INTERNAL_TAGS, VALID_IMAGES, TagType
log = logging.getLogger("red.aikaterna.rss")
__version__ = "1.1.2"
__version__ = "1.1.3"
class RSS(commands.Cog):
@@ -376,6 +376,14 @@ class RSS(commands.Cog):
color = color.replace(" ", "_")
hex_code = await Color()._color_converter(color)
if not hex_code:
await ctx.send(
"Not a valid color code. Use a hex code like #990000, a "
"Discord color name or a CSS3 color name.\n"
"<https://discordpy.readthedocs.io/en/latest/api.html#colour>\n"
"<https://www.w3.org/TR/2018/REC-css-color-3-20180619/#svg-color>"
)
return
user_facing_hex = hex_code.replace("0x", "#")
color_name = await Color()._hex_to_css3_name(hex_code)