Type hint colour.py
This commit is contained in:
parent
6622be9f46
commit
c69b20c52c
@ -25,11 +25,23 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
import colorsys
|
import colorsys
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Optional,
|
||||||
|
Tuple,
|
||||||
|
Type,
|
||||||
|
TypeVar,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Colour',
|
'Colour',
|
||||||
'Color',
|
'Color',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CT = TypeVar('CT', bound='Colour')
|
||||||
|
|
||||||
|
|
||||||
class Colour:
|
class Colour:
|
||||||
"""Represents a Discord role colour. This class is similar
|
"""Represents a Discord role colour. This class is similar
|
||||||
to a (red, green, blue) :class:`tuple`.
|
to a (red, green, blue) :class:`tuple`.
|
||||||
@ -70,66 +82,66 @@ class Colour:
|
|||||||
if not isinstance(value, int):
|
if not isinstance(value, int):
|
||||||
raise TypeError(f'Expected int parameter, received {value.__class__.__name__} instead.')
|
raise TypeError(f'Expected int parameter, received {value.__class__.__name__} instead.')
|
||||||
|
|
||||||
self.value = value
|
self.value: int = value
|
||||||
|
|
||||||
def _get_byte(self, byte):
|
def _get_byte(self, byte: int) -> int:
|
||||||
return (self.value >> (8 * byte)) & 0xff
|
return (self.value >> (8 * byte)) & 0xff
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other: Any) -> bool:
|
||||||
return isinstance(other, Colour) and self.value == other.value
|
return isinstance(other, Colour) and self.value == other.value
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other: Any) -> bool:
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return f'#{self.value:0>6x}'
|
return f'#{self.value:0>6x}'
|
||||||
|
|
||||||
def __int__(self):
|
def __int__(self) -> int:
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self) -> str:
|
||||||
return f'<Colour value={self.value}>'
|
return f'<Colour value={self.value}>'
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self) -> int:
|
||||||
return hash(self.value)
|
return hash(self.value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def r(self):
|
def r(self) -> int:
|
||||||
""":class:`int`: Returns the red component of the colour."""
|
""":class:`int`: Returns the red component of the colour."""
|
||||||
return self._get_byte(2)
|
return self._get_byte(2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def g(self):
|
def g(self) -> int:
|
||||||
""":class:`int`: Returns the green component of the colour."""
|
""":class:`int`: Returns the green component of the colour."""
|
||||||
return self._get_byte(1)
|
return self._get_byte(1)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def b(self):
|
def b(self) -> int:
|
||||||
""":class:`int`: Returns the blue component of the colour."""
|
""":class:`int`: Returns the blue component of the colour."""
|
||||||
return self._get_byte(0)
|
return self._get_byte(0)
|
||||||
|
|
||||||
def to_rgb(self):
|
def to_rgb(self) -> Tuple[int, int, int]:
|
||||||
"""Tuple[:class:`int`, :class:`int`, :class:`int`]: Returns an (r, g, b) tuple representing the colour."""
|
"""Tuple[:class:`int`, :class:`int`, :class:`int`]: Returns an (r, g, b) tuple representing the colour."""
|
||||||
return (self.r, self.g, self.b)
|
return (self.r, self.g, self.b)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_rgb(cls, r, g, b):
|
def from_rgb(cls: Type[CT], r: int, g: int, b: int) -> CT:
|
||||||
"""Constructs a :class:`Colour` from an RGB tuple."""
|
"""Constructs a :class:`Colour` from an RGB tuple."""
|
||||||
return cls((r << 16) + (g << 8) + b)
|
return cls((r << 16) + (g << 8) + b)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_hsv(cls, h, s, v):
|
def from_hsv(cls: Type[CT], h: float, s: float, v: float) -> CT:
|
||||||
"""Constructs a :class:`Colour` from an HSV tuple."""
|
"""Constructs a :class:`Colour` from an HSV tuple."""
|
||||||
rgb = colorsys.hsv_to_rgb(h, s, v)
|
rgb = colorsys.hsv_to_rgb(h, s, v)
|
||||||
return cls.from_rgb(*(int(x * 255) for x in rgb))
|
return cls.from_rgb(*(int(x * 255) for x in rgb))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def default(cls):
|
def default(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0``."""
|
||||||
return cls(0)
|
return cls(0)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def random(cls, *, seed=None):
|
def random(cls: Type[CT], *, seed: Optional[Union[int, str, float, bytes, bytearray]] = None) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a random hue.
|
"""A factory method that returns a :class:`Colour` with a random hue.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
@ -150,125 +162,125 @@ class Colour:
|
|||||||
return cls.from_hsv(rand.random(), 1, 1)
|
return cls.from_hsv(rand.random(), 1, 1)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def teal(cls):
|
def teal(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x1abc9c``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x1abc9c``."""
|
||||||
return cls(0x1abc9c)
|
return cls(0x1abc9c)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_teal(cls):
|
def dark_teal(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x11806a``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x11806a``."""
|
||||||
return cls(0x11806a)
|
return cls(0x11806a)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def green(cls):
|
def green(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x2ecc71``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x2ecc71``."""
|
||||||
return cls(0x2ecc71)
|
return cls(0x2ecc71)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_green(cls):
|
def dark_green(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x1f8b4c``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x1f8b4c``."""
|
||||||
return cls(0x1f8b4c)
|
return cls(0x1f8b4c)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def blue(cls):
|
def blue(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x3498db``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x3498db``."""
|
||||||
return cls(0x3498db)
|
return cls(0x3498db)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_blue(cls):
|
def dark_blue(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x206694``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x206694``."""
|
||||||
return cls(0x206694)
|
return cls(0x206694)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def purple(cls):
|
def purple(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x9b59b6``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x9b59b6``."""
|
||||||
return cls(0x9b59b6)
|
return cls(0x9b59b6)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_purple(cls):
|
def dark_purple(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x71368a``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x71368a``."""
|
||||||
return cls(0x71368a)
|
return cls(0x71368a)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def magenta(cls):
|
def magenta(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xe91e63``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xe91e63``."""
|
||||||
return cls(0xe91e63)
|
return cls(0xe91e63)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_magenta(cls):
|
def dark_magenta(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xad1457``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xad1457``."""
|
||||||
return cls(0xad1457)
|
return cls(0xad1457)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def gold(cls):
|
def gold(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xf1c40f``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xf1c40f``."""
|
||||||
return cls(0xf1c40f)
|
return cls(0xf1c40f)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_gold(cls):
|
def dark_gold(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xc27c0e``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xc27c0e``."""
|
||||||
return cls(0xc27c0e)
|
return cls(0xc27c0e)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def orange(cls):
|
def orange(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xe67e22``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xe67e22``."""
|
||||||
return cls(0xe67e22)
|
return cls(0xe67e22)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_orange(cls):
|
def dark_orange(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xa84300``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xa84300``."""
|
||||||
return cls(0xa84300)
|
return cls(0xa84300)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def red(cls):
|
def red(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0xe74c3c``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0xe74c3c``."""
|
||||||
return cls(0xe74c3c)
|
return cls(0xe74c3c)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_red(cls):
|
def dark_red(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x992d22``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x992d22``."""
|
||||||
return cls(0x992d22)
|
return cls(0x992d22)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def lighter_grey(cls):
|
def lighter_grey(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x95a5a6``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x95a5a6``."""
|
||||||
return cls(0x95a5a6)
|
return cls(0x95a5a6)
|
||||||
|
|
||||||
lighter_gray = lighter_grey
|
lighter_gray = lighter_grey
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_grey(cls):
|
def dark_grey(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x607d8b``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x607d8b``."""
|
||||||
return cls(0x607d8b)
|
return cls(0x607d8b)
|
||||||
|
|
||||||
dark_gray = dark_grey
|
dark_gray = dark_grey
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def light_grey(cls):
|
def light_grey(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x979c9f``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x979c9f``."""
|
||||||
return cls(0x979c9f)
|
return cls(0x979c9f)
|
||||||
|
|
||||||
light_gray = light_grey
|
light_gray = light_grey
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def darker_grey(cls):
|
def darker_grey(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x546e7a``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x546e7a``."""
|
||||||
return cls(0x546e7a)
|
return cls(0x546e7a)
|
||||||
|
|
||||||
darker_gray = darker_grey
|
darker_gray = darker_grey
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def blurple(cls):
|
def blurple(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x7289da``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x7289da``."""
|
||||||
return cls(0x7289da)
|
return cls(0x7289da)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def greyple(cls):
|
def greyple(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x99aab5``."""
|
"""A factory method that returns a :class:`Colour` with a value of ``0x99aab5``."""
|
||||||
return cls(0x99aab5)
|
return cls(0x99aab5)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dark_theme(cls):
|
def dark_theme(cls: Type[CT]) -> CT:
|
||||||
"""A factory method that returns a :class:`Colour` with a value of ``0x36393F``.
|
"""A factory method that returns a :class:`Colour` with a value of ``0x36393F``.
|
||||||
This will appear transparent on Discord's dark theme.
|
This will appear transparent on Discord's dark theme.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user