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

Commit 3ccb283

Browse files
Inf-inityInf-inity
authored andcommitted
some changes
1 parent d6432e6 commit 3ccb283

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

general/color_picker/cog.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import colorsys
22
import re
3+
from collections import namedtuple
34

45
from discord import Colour, Embed
56
from discord.ext import commands
@@ -15,15 +16,14 @@
1516
t = t.color_picker
1617

1718

18-
def _to_floats(given: list[tuple[int, ...]]) -> tuple[float, ...]:
19-
"""3 tuples (number from user, max-value)"""
19+
def _to_floats(given: list[namedtuple]) -> tuple[float, float, float]:
2020
out: list[float] = []
2121

2222
for arg in given:
23-
if 0 < int(arg[0]) > arg[1]:
24-
raise CommandError(t.error.invalid_input(arg[0], arg[1]))
23+
if 0 < int(arg.value) > arg.max_value:
24+
raise CommandError(t.error.invalid_input(arg.value, arg.max_value))
2525

26-
out.append(float(int(arg[0]) / arg[1]))
26+
out.append(float(int(arg.value) / arg.max_value))
2727

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

@@ -33,7 +33,7 @@ def _hex_to_color(hex_color: str) -> tuple[int, ...]:
3333

3434

3535
class ColorPickerCog(Cog, name="Color Picker"):
36-
CONTRIBUTORS = [Contributor.Tert0, Contributor.NekoFanatic]
36+
CONTRIBUTORS = [Contributor.Tert0, Contributor.Infinity]
3737

3838
RE_HEX = re.compile(r"^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$")
3939
RE_RGB = re.compile(r"^rgb\(([0-9]{1,3}), *([0-9]{1,3}), *([0-9]{1,3})\)$")
@@ -43,20 +43,39 @@ class ColorPickerCog(Cog, name="Color Picker"):
4343
@commands.command(name="color_picker", aliases=["cp", "color"])
4444
@docs(t.commands.color_picker)
4545
async def color_picker(self, ctx: Context, *, color: str):
46+
color_args = namedtuple("ColorParameter", ["value", "max_value"])
4647

4748
if color_re := self.RE_HEX.match(color):
4849
rgb = _hex_to_color(color_re.group(1))
49-
rgb = _to_floats([(rgb[0], 255), (rgb[1], 255), (rgb[2], 255)])
50+
rgb = _to_floats([color_args(rgb[0], 255), color_args(rgb[1], 255), color_args(rgb[2], 255)])
5051

5152
elif color_re := self.RE_RGB.match(color):
52-
rgb = _to_floats([(color_re.group(1), 255), (color_re.group(2), 255), (color_re.group(3), 255)])
53+
rgb = _to_floats(
54+
[
55+
color_args(color_re.group(1), 255),
56+
color_args(color_re.group(2), 255),
57+
color_args(color_re.group(3), 255),
58+
]
59+
)
5360

5461
elif color_re := self.RE_HSV.match(color):
55-
values = _to_floats([(color_re.group(1), 360), (color_re.group(2), 100), (color_re.group(3), 100)])
62+
values = _to_floats(
63+
[
64+
color_args(color_re.group(1), 360),
65+
color_args(color_re.group(2), 100),
66+
color_args(color_re.group(3), 100),
67+
]
68+
)
5669
rgb = colorsys.hsv_to_rgb(values[0], values[1], values[2])
5770

5871
elif color_re := self.RE_HSL.match(color):
59-
values = _to_floats([(color_re.group(1), 360), (color_re.group(2), 100), (color_re.group(3), 100)])
72+
values = _to_floats(
73+
[
74+
color_args(color_re.group(1), 360),
75+
color_args(color_re.group(2), 100),
76+
color_args(color_re.group(3), 100),
77+
]
78+
)
6079
rgb = colorsys.hls_to_rgb(values[0], values[2], values[1])
6180

6281
else:

0 commit comments

Comments
 (0)