fromdiscordimportColourdef_int_from_hex(hex_string:str)->int:try:returnint(hex_string,base=16)exceptExceptionasexc:raiseValueError("Input string must be a valid HEX code.")fromexcdef_hex_from_int(color_int:int)->str:ifnot0<=color_int<=0xFFFFFF:raiseValueError("Color's value must be in the range 0 to 0xFFFFFF.")returnf"#{color_int:06x}"
[docs]defcolor_from_hex(hex_string:str)->Colour:"""Convert valid hexadecimal string to :class:`discord.Colour`. Args: hex_string (str): Hexadecimal string to convert into :class:`discord.Colour` object Returns: Colour: :class:`discord.Colour` object """returnColour(_int_from_hex(hex_string))
[docs]defhex_from_color(color:Colour)->str:"""Convert :class:`discord.Colour` to hexadecimal string. Args: color (Colour): :class:`discord.Colour` object to convert into the string Returns: str: Hexadecimal string in #XXXXXX format """return_hex_from_int(color.value)