[docs]classColour:def__init__(self,value:int):ifnotisinstance(value,int):raiseTypeError(f"value must be an integer, not {type(value)}")ifvalue<0orvalue>0xFFFFFF:raiseValueError(f"value must be between 0 and 16777215, not {value}")self.value:int=valuedef__int__(self)->int:returnself.valuedef__str__(self)->str:returnself.to_hex()def__repr__(self)->str:returnf"<Colour value={self.value}>"def_get_byte(self,byte:int)->int:return(self.value>>(8*byte))&0xFF@propertydefr(self)->int:""" `int`: Returns the red component of the colour """returnself._get_byte(2)@propertydefg(self)->int:""" `int`: Returns the green component of the colour """returnself._get_byte(1)@propertydefb(self)->int:""" `int`: Returns the blue component of the colour """returnself._get_byte(0)
[docs]@classmethoddeffrom_rgb(cls,r:int,g:int,b:int)->Self:""" Creates a Colour object from RGB values Parameters ---------- r: `int` Red value g: `int` Green value b: `int` Blue value Returns ------- `Colour` The colour object """returncls((r<<16)+(g<<8)+b)
[docs]defto_rgb(self)->tuple[int,int,int]:""" `tuple[int, int, int]`: Returns the RGB values of the colour` """return(self.r,self.g,self.b)
[docs]@classmethoddeffrom_hex(cls,hex:str)->Self:""" Creates a Colour object from a hex string Parameters ---------- hex: `str` The hex string to convert Returns ------- `Colour` The colour object Raises ------ `ValueError` Invalid hex colour """find_hex=utils.re_hex.search(hex)ifnotfind_hex:raiseValueError(f"Invalid hex colour {hex!r}")ifhex.startswith("#"):hex=hex[1:]iflen(hex)==3:hex=hex*2returncls(int(hex,16))
[docs]defto_hex(self)->str:""" `str`: Returns the hex value of the colour """returnf"#{self.value:06x}"
[docs]@classmethoddefdefault(cls)->Self:""" `Colour`: Returns the default colour (#000000, Black) """returncls(0)
[docs]@classmethoddefrandom(cls,*,seed:Any|None=None)->Self:""" Creates a random colour Parameters ---------- seed: `Optional[Any]` The seed to use for the random colour to make it deterministic Returns ------- `Colour` The random colour """r=random.Random(seed)ifseedelserandomreturncls(r.randint(0,0xFFFFFF))
# Colours based on https://flatuicolors.com/palette/defo# A few of them are custom to Discord however
[docs]@classmethoddefturquoise(cls)->Self:""" `Colour`: Returns the turquoise colour """returncls(0x1abc9c)
[docs]@classmethoddefgreen_sea(cls)->Self:""" `Colour`: Returns the green sea colour """returncls(0x16a085)
[docs]@classmethoddefemerald(cls)->Self:""" `Colour`: Returns the emerald colour """returncls(0x2ecc71)
[docs]@classmethoddefnephritis(cls)->Self:""" `Colour`: Returns the nephritis colour """returncls(0x27ae60)
[docs]@classmethoddefpeter_river(cls)->Self:""" `Colour`: Returns the peter river colour """returncls(0x3498db)
[docs]@classmethoddefbelize_hole(cls)->Self:""" `Colour`: Returns the belize hole colour """returncls(0x2980b9)
[docs]@classmethoddefamethyst(cls)->Self:""" `Colour`: Returns the amethyst colour """returncls(0x9b59b6)
[docs]@classmethoddefwisteria(cls)->Self:""" `Colour`: Returns the wisteria colour """returncls(0x8e44ad)
[docs]@classmethoddefmellow_melon(cls)->Self:""" `Colour`: Returns the mellow melon colour """returncls(0xe91e63)
[docs]@classmethoddefplum_perfect(cls)->Self:""" `Colour`: Returns the plum perfect colour """returncls(0xad1457)
[docs]@classmethoddefsun_flower(cls)->Self:""" `Colour`: Returns the sun flower colour """returncls(0xf1c40f)
[docs]@classmethoddeforange(cls)->Self:""" `Colour`: Returns the orange colour """returncls(0xf39c12)
[docs]@classmethoddefcarrot(cls)->Self:""" `Colour`: Returns the carrot colour """returncls(0xe67e22)
[docs]@classmethoddefpumpkin(cls)->Self:""" `Colour`: Returns the pumpkin colour """returncls(0xd35400)
[docs]@classmethoddefalizarin(cls)->Self:""" `Colour`: Returns the alizarin colour """returncls(0xe74c3c)
[docs]@classmethoddefpomegranate(cls)->Self:""" `Colour`: Returns the pomegranate colour """returncls(0xc0392b)
[docs]@classmethoddefdusty_sky(cls)->Self:""" `Colour`: Returns the dusty sky colour """returncls(0x95a5a6)
[docs]@classmethoddefharrison_grey(cls)->Self:""" `Colour`: Returns the harrison grey colour """returncls(0x979c9f)
[docs]@classmethoddefwhale_shark(cls)->Self:""" `Colour`: Returns the whale shark colour """returncls(0x607d8b)
[docs]@classmethoddefblue_sentinel(cls)->Self:""" `Colour`: Returns the blue sentinel colour """returncls(0x546e7a)
[docs]classColor(Colour):""" Alias for Colour """def__init__(self,value:int):super().__init__(value)def__repr__(self)->str:returnf"<Color value={self.value}>"