This repository was archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Added Color Picker Command (for HEX Colors) #157
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d63fac4
Added Color Picker Command for HEX Colors
Tert0 8e3f212
Restored Pipfile.lock
Tert0 2a7a488
Fixed Linting Error
Tert0 dcc9fa8
Removed old Todo and renamed var
Tert0 98d300b
Added RGB/HSV/HSL Support and changed Help/Error message
Tert0 cec1fe7
Compiles Regex and improved convertions
Tert0 100505e
Fixed Errors
Tert0 302c991
Changed to formated strings
Tert0 34c2125
'Fixed' Analyse Error
Tert0 00171bb
Merge branch 'experimental' into color-picker
Tert0 efa29c8
Moved Comments
Tert0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
| 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')) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.