Source code for libbot.pycord.utils.color

from discord import Colour


def _int_from_hex(hex_string: str) -> int:
    try:
        return int(hex_string, base=16)
    except Exception as exc:
        raise ValueError("Input string must be a valid HEX code.") from exc


def _hex_from_int(color_int: int) -> str:
    if not 0 <= color_int <= 0xFFFFFF:
        raise ValueError("Color's value must be in the range 0 to 0xFFFFFF.")

    return f"#{color_int:06x}"


[docs] def color_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 """ return Colour(_int_from_hex(hex_string))
[docs] def hex_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)