Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ aiohttp = "*"
alembic = "*"
pydrocsid = "*"
"discord.py" = "*"
pillow = "*"

[dev-packages]
flake8 = "*"
Expand Down
57 changes: 55 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MorpheusHelper
Bot for the [Discord Server of The Morpheus Tutorials](https://discord.gg/BnYZ8XS)
Bot for the [Discord Server of The Morpheus Tutorials](https://discord.gg/themorpheus)

## Development
### Prerequisites
Expand Down
2 changes: 2 additions & 0 deletions morpheushelper/cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cogs.run_code import RunCodeCog
from cogs.verification import VerificationCog
from cogs.voice_channel import VoiceChannelCog
from cogs.color_picker import ColorPickerCog

COGS = [
RunCodeCog,
Expand All @@ -44,4 +45,5 @@
PollsCog,
DiscordpyDocumentationCog,
AdventOfCodeCog,
ColorPickerCog,
]
57 changes: 57 additions & 0 deletions morpheushelper/cogs/color_picker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from PyDrocsid.translations import translations
from discord import Embed, File, Colour
from discord.ext import commands
from discord.ext.commands import Cog, Bot
import re
from PIL import ImageColor, Image
import io
import colorsys


class ColorPickerCog(Cog):
Comment thread
Tert0 marked this conversation as resolved.
RE_HEX = re.compile(r'^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$')
RE_RGB = re.compile(r'^rgb\(([0-9]{1,3})\, ?([0-9]{1,3})\, ?([0-9]{1,3})\)$')
RE_HSV = re.compile(r'^hsv\(([0-9]{1,3})\, ?([0-9]{1,3})\, ?([0-9]{1,3})\)$')
RE_HSL = re.compile(r'^hsl\(([0-9]{1,3})\, ?([0-9]{1,3})\, ?([0-9]{1,3})\)$')

def __init__(self, bot: Bot):
self.bot = bot

@commands.command(name="colorpicker", aliases=["cp", "color"])
async def colorpicker(self, ctx, *, color: str):
if color_re := self.RE_HEX.match(color):
color_hex = color_re.group(1)
rgb: tuple[int] = ImageColor.getcolor(color, "RGB")
hsv: tuple[int] = ImageColor.getcolor(color, "HSV")
hsl = tuple(map(int, colorsys.rgb_to_hls(*rgb))) # skipcq: PYL-E1120
elif color_re := self.RE_RGB.match(color):
rgb = (int(color_re.group(1)), int(color_re.group(2)), int(color_re.group(3)))
color_hex = '%02x%02x%02x' % rgb
hsv: tuple[int] = ImageColor.getcolor(f'#{color_hex}', "HSV")
hsl = tuple(map(int, colorsys.rgb_to_hls(*rgb))) # skipcq: PYL-E1120
elif color_re := self.RE_HSV.match(color):
hsv: tuple[int] = (int(color_re.group(1)), int(color_re.group(2)), int(color_re.group(3)))
rgb = tuple(map(int, colorsys.hsv_to_rgb(*hsv))) # skipcq: PYL-E1120
color_hex = '%02x%02x%02x' % rgb
hsl = tuple(map(int, colorsys.rgb_to_hls(*rgb))) # skipcq: PYL-E1120
elif color_re := self.RE_HSL.match(color):
hsl: tuple[int] = (int(color_re.group(1)), int(color_re.group(2)), int(color_re.group(3)))
rgb = tuple(map(int, colorsys.hls_to_rgb(*hsl))) # skipcq: PYL-E1120
hsv = tuple(map(int, colorsys.rgb_to_hsv(*rgb))) # skipcq: PYL-E1120
color_hex = '%02x%02x%02x' % rgb
else:
embed: Embed = Embed(title=translations.f_error_parse_color(color),
description=translations.error_parse_color_example)
await ctx.send(embed=embed)
return
img: Image = Image.new('RGB', (100, 100), rgb)
with io.BytesIO() as image_binary:
img.save(image_binary, 'PNG')
image_binary.seek(0)
embed: Embed = Embed(title='Colorpicker', color=Colour(int(color_hex, 16)))
embed.add_field(name='HEX', value=f'#{color_hex}')
embed.add_field(name='RGB', value=f'rgb{rgb}')
embed.add_field(name='HSV', value=f'hsv{hsv}')
embed.add_field(name='HSL', value=f'hsl{hsl}')
embed.set_image(url="attachment://color.png")
await ctx.send(embed=embed, file=File(fp=image_binary, filename='color.png'))
14 changes: 14 additions & 0 deletions translations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,17 @@ run_usage: |
your code
`​`​`
run_output: Run Output

# color picker
error_parse_color: "Error cant parse HEX/RGB/HSV/HSL Color Code(\"{}\")"
error_parse_color_example: >
**Examples:**
- #fff
- #ffffff
- #000000
- rgb(255, 0, 0)
- rgb(255, 255, 255)
- hsv(0, 0, 0)
- hsv(0, 0, 255)
- hsl(0, 106, 0)
- hsl(0, 191, 0)