Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Color Picker #34

Open
wants to merge 38 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f4c9835
Added Colorpicker command
Tert0 Jun 19, 2021
0abe44b
Added Translations
Tert0 Jun 19, 2021
39ca814
Reformated wth black
Tert0 Jun 19, 2021
ffddefc
Added _to_int function, improved type hints
Tert0 Oct 1, 2021
4bf432e
Improved HEX Color Converter
Tert0 Oct 5, 2021
695dec8
Fixed PEP8
Tert0 Oct 5, 2021
0b436e3
Added new Line
Tert0 Oct 5, 2021
107ded8
Removed PIL, Added Color API, Better Error Message
Tert0 Oct 9, 2021
e382e9c
Fixed PEP8 for Stackoverflow Copied Code :)
Tert0 Oct 9, 2021
66ea618
Fix Black
Tert0 Oct 9, 2021
6a1e54d
Fix Codestyle
Tert0 Oct 9, 2021
8400535
Fix Codestyle
Tert0 Oct 9, 2021
888081c
Update general/color_picker/cog.py
Tert0 Oct 18, 2021
5024108
Update general/color_picker/cog.py
Tert0 Oct 18, 2021
dac10ad
Fixed color picker cog
Defelo Jun 11, 2022
044ca59
Added color picker documentation placeholder
Defelo Jun 11, 2022
2d12d08
Everything working (except HLS)
Jun 11, 2022
f962392
Fixed linting
Jun 11, 2022
9512543
Removed HLS, because transformation can be done
Jun 12, 2022
d7cf472
Fixed thread auto join
Defelo Jun 13, 2022
e8bf57f
Wrote documentation + changed some code style
Jun 14, 2022
ddf55ed
Fixed codestyle + edited translation-text
Jun 15, 2022
6290be4
Added more options for spam_detection (incomplete)
Apr 8, 2022
ed16cf6
Fixed exception if bot is blocked by the member
Defelo Jun 15, 2022
f6ef9df
Improved translations
Defelo Jun 15, 2022
6c0d6cb
Improved spam_detection status embed
Defelo Jun 15, 2022
700f5a3
Improved aliases
Defelo Jun 15, 2022
f2c6a93
Update cog.py
Jun 15, 2022
88b0eae
Update documentation.md
Jun 15, 2022
c1b1fed
Removed useless _to_rgb function
Jun 15, 2022
152569b
Changed Contributor name 'NekoFanatic' into 'Infinity'
Jun 17, 2022
119a3c1
Rewrote code with namedtuples
Jun 20, 2022
e86bec1
Fixed mistake in translation
Jun 20, 2022
4f31221
Made brackets optional in regex
TheCataliasTNT2k Jun 20, 2022
8cae34e
Refactored code
Jun 20, 2022
0b5e139
Resolved change requests
Jun 27, 2022
1e2121d
Formatted code
Jun 27, 2022
2928422
Merge branch 'develop' into feature/color-picker
Jul 18, 2022
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
2 changes: 2 additions & 0 deletions general/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .betheprofessional import BeTheProfessionalCog
from .color_picker import ColorPickerCog
from .custom_commands import CustomCommandsCog
from .discord_bot_token_deleter import DiscordBotTokenDeleterCog
from .news import NewsCog
Expand All @@ -12,6 +13,7 @@

__all__ = [
"BeTheProfessionalCog",
"ColorPickerCog",
"CustomCommandsCog",
"DiscordBotTokenDeleterCog",
"NewsCog",
Expand Down
4 changes: 4 additions & 0 deletions general/color_picker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .cog import ColorPickerCog


__all__ = ["ColorPickerCog"]
92 changes: 92 additions & 0 deletions general/color_picker/cog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import colorsys
import re
from collections import namedtuple

from discord import Colour, Embed
from discord.ext import commands
from discord.ext.commands import CommandError, Context

from PyDrocsid.cog import Cog
from PyDrocsid.command import docs, reply
from PyDrocsid.translations import t

from ...contributor import Contributor


t = t.color_picker


COLOR_ARGS = namedtuple("ColorParameter", ["value", "max_value"])
COLOR = namedtuple("RGB_Tuple", ["R", "G", "B"])


def _to_floats(given: list[COLOR_ARGS]) -> tuple[float, float, float]:
"""
takes a list with tuples containing a value and a max_values
at the end it should return a number between 0 and 1 for each tuple
"""
out: list[float] = []

for arg in given:
if 0 < int(arg.value) > arg.max_value:
raise CommandError(t.error.invalid_input(arg.value, arg.max_value))

out.append(float(int(arg.value) / arg.max_value))

return out[0], out[1], out[2]


def _hex_to_color(hex_color: str) -> COLOR:
return COLOR(*tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))) # noqa: E203


class ColorPickerCog(Cog, name="Color Picker"):
CONTRIBUTORS = [Contributor.Tert0, Contributor.Infinity]

RE_HEX = re.compile(r"^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$")
REG = r" *(\()?([0-9]{1,3}),? *([0-9]{1,3}),? *([0-9]{1,3})(?(1)\)|)$"
RE_RGB = re.compile(r"^rgb" + REG)
RE_HSV = re.compile(r"^hsv" + REG)
RE_HSL = re.compile(r"^hsl" + REG)

@commands.command(name="color_picker", aliases=["cp", "color"])
@docs(t.commands.color_picker)
async def color_picker(self, ctx: Context, *, color: str):

if color_re := self.RE_HEX.match(color):
rgb = _hex_to_color(color_re.group(1))
rgb = _to_floats([COLOR_ARGS(rgb[i], 255) for i in range(3)])

elif color_re := self.RE_RGB.match(color):
rgb = _to_floats([COLOR_ARGS(color_re.group(i), 255) for i in range(2, 5)])

elif color_re := self.RE_HSV.match(color):
rgb = colorsys.hsv_to_rgb(
*_to_floats([COLOR_ARGS(color_re.group(i), value) for i, value in ((2, 360), (3, 100), (4, 100))])
)

elif color_re := self.RE_HSL.match(color):
rgb = colorsys.hls_to_rgb(
*_to_floats([COLOR_ARGS(color_re.group(i), value) for i, value in ((2, 360), (3, 100), (4, 100))])
)

else:
raise CommandError(t.error.parse_color_example(color))

h, s, v = colorsys.rgb_to_hsv(*rgb)
hsv = (int(round(h * 360, 0)), int(round(s * 100, 0)), int(round(v * 100, 0)))

h, l, s = colorsys.rgb_to_hls(*rgb)
hsl = (int(round(h * 360)), int(round(s * 100)), int(round(l * 100)))

rgb = tuple(int(color * 255) for color in rgb)
color_hex = f"{rgb[0]:02x}{rgb[1]:02x}{rgb[2]:02x}"

embed: Embed = Embed(title=t.embed.title, color=Colour(int(color_hex, 16)))
embed.set_image(url=f"https://singlecolorimage.com/get/{color_hex}/300x50")
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}`")

await reply(ctx, embed=embed)
16 changes: 16 additions & 0 deletions general/color_picker/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Color Picker

Contains a command that can be used to convert different types of colors into each other


## `color_picker`

```css
.[color_picker|cp|color] <color>
```

Arguments:

| Argument | Required | Description |
|:--------:|:--------:|:--------------------------------------------------------------------------------------------------------------------------------------|
| `color` | | can be a hex-code or it can start with [`rgb`, `hsl` or `hsv`] and has to get in brackets behind that 3 numbers, separated by a comma |
17 changes: 17 additions & 0 deletions general/color_picker/translations/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
commands:
color_picker: Convert RGB, HEX, HSL or HSV into each other


embed:
title: Color Picker

error:
invalid_input: "`{}` has to be at least `0` or smaller than `{}`"
parse_color_example: >
**Error cant parse HEX/RGB/HSL/HSV Color Code(`{}`)**

**Examples:**
- `#fdf12f` # #000000 - #FFFFFF
- `rgb(255, 255, 100)` # (0-255, 0-255, 0-255)
- `hsv(343, 50, 100)` # (0-360, 0-100, 0-100)
- `hsl(298, 0, 99)` # (0-360, 0-100, 0-100)