Skip to content

colorutils module

color_to_hex(color, model='rgb')

Convert color value to hex.

The color and model parameters represent the color to be converted. The value is expected to be a string for "name" and "hex" models and a Tuple or List for "rgb" and "hsl" models.

Parameters:

Name Type Description Default
color Any

The color values for the model being converted.

required
model str

The color model being converted.

'rgb'

Returns:

Type Description
str

The hexadecimal color value.

Source code in ttkbootstrap/colorutils.py
def color_to_hex(color, model=RGB):
    """Convert color value to hex.

    The color and model parameters represent the color to be converted.
    The value is expected to be a string for "name" and "hex" models and
    a Tuple or List for "rgb" and "hsl" models.

    Parameters:

        color (Any):
            The color values for the model being converted.

        model (str):
            The color model being converted.

    Returns:

        str:
            The hexadecimal color value.
    """
    r, g, b = color_to_rgb(color, model)
    return f'#{r:02x}{g:02x}{b:02x}'

color_to_hsl(color, model='hex')

Convert color value to hsl.

The color and model parameters represent the color to be converted. The value is expected to be a string for "name" and "hex" models and a Tuple or List for "rgb" and "hsl" models.

Parameters:

Name Type Description Default
color Any

The color values for the model being converted.

required
model str

The color model being converted.

'hex'

Returns:

Type Description
Tuple[int, int, int]

The hsl color values.

Source code in ttkbootstrap/colorutils.py
def color_to_hsl(color, model=HEX):
    """Convert color value to hsl.

    The color and model parameters represent the color to be converted.
    The value is expected to be a string for "name" and "hex" models and
    a Tuple or List for "rgb" and "hsl" models.

    Parameters:

        color (Any):
            The color values for the model being converted.

        model (str):
            The color model being converted.

    Returns:

        Tuple[int, int, int]:
            The hsl color values.
    """
    r, g, b = color_to_rgb(color, model)
    hls = rgb_to_hls(r/255, g/255, b/255)
    h = int(hls[0]*HUE)
    l = int(hls[1]*LUM)
    s = int(hls[2]*SAT)
    return h, s, l

color_to_rgb(color, model='hex')

Convert color value to rgb.

The color and model parameters represent the color to be converted. The value is expected to be a string for "name" and "hex" models and a Tuple or List for "rgb" and "hsl" models.

Parameters:

Name Type Description Default
color Any

The color values for the model being converted.

required
model str

The color model being converted.

'hex'

Returns:

Type Description
Tuple[int, int, int]

The rgb color values.

Source code in ttkbootstrap/colorutils.py
def color_to_rgb(color, model=HEX):
    """Convert color value to rgb.

    The color and model parameters represent the color to be converted.
    The value is expected to be a string for "name" and "hex" models and
    a Tuple or List for "rgb" and "hsl" models.

    Parameters:

        color (Any):
            The color values for the model being converted.

        model (str):
            The color model being converted.

    Returns:

        Tuple[int, int, int]:
            The rgb color values.
    """
    color_ = conform_color_model(color, model)    
    try:
        return ImageColor.getrgb(color_)
    except:
        print('this')

conform_color_model(color, model)

Conform the color values to a string that can be interpreted by the PIL.ImageColor.getrgb method.

Parameters:

Name Type Description Default
color Union[Tuple[int, int, int], str]

The color value to conform.

required
model str

One of 'HSL', 'RGB', or 'HEX'

required

Returns:

Type Description
str

A color value string that can be used as a parameter in the PIL.ImageColor.getrgb method.

Source code in ttkbootstrap/colorutils.py
def conform_color_model(color, model):
    """Conform the color values to a string that can be interpreted
    by the `PIL.ImageColor.getrgb method`.

    Parameters:

        color (Union[Tuple[int, int, int], str]):
            The color value to conform.

        model (str):
            One of 'HSL', 'RGB', or 'HEX'

    Returns:

        str:
            A color value string that can be used as a parameter in the
            PIL.ImageColor.getrgb method.
    """    
    if model == HSL:
        h, s, l = color
        return f'hsl({h},{s}%,{l}%)'
    elif model == RGB:
        r, g, b = color
        return f'rgb({r},{g},{b})'
    else:
        return color

contrast_color(color, model='rgb', darkcolor='#000', lightcolor='#fff')

Returns the best matching contrasting light or dark color for the given color.

Parameters:

Name Type Description Default
color Any

The color value to evaluate.

required
model str

The model of the color value to be evaluated. 'rgb' by default.

'rgb'
darkcolor Any

The color value to be returned when the constrasting color should be dark.

'#000'
lightcolor Any

The color value to be returned when the constrasting color should be light.

'#fff'

Returns:

Type Description
str

The matching color value.

Source code in ttkbootstrap/colorutils.py
def contrast_color(color, model=RGB, darkcolor='#000', lightcolor='#fff'):
    """Returns the best matching contrasting light or dark color for
    the given color.

    Parameters:

        color (Any):
            The color value to evaluate.

        model (str):
            The model of the color value to be evaluated. 'rgb' by 
            default.

        darkcolor (Any):
            The color value to be returned when the constrasting color 
            should be dark.

        lightcolor (Any):
            The color value to be returned when the constrasting color
            should be light.

    Returns:

        str:
            The matching color value.
    """
    if model != RGB:
        r, g, b = color_to_rgb(color, model)
    else:
        r, g, b = color

    luminance = ((0.299 * r) + (0.587 * g) + (0.114 * b))/255
    if luminance > 0.5:
        return darkcolor
    else:
        return lightcolor

update_hsl_value(color, hue=None, sat=None, lum=None, inmodel='hsl', outmodel='hsl')

Change hue, saturation, or lumenosity of the color based on the hue, sat, lum parameters provided.

Parameters:

Name Type Description Default
color Any

The color

required
hue int

A number between 0 and 360.

None
sat int

A number between 0 and 100.

None
lum int

A number between 0 and 100.

None
inmodel str

The color model used by the color to be changed. One of hsl, rgb, hex, name.

'hsl'
outmodel str

The color value model to be returned when the color is changed. One of hsl, rgb, hex.

'hsl'

Returns:

Type Description
Union[Tuple[int, int, int], str]

The color value based on the selected color model.

Source code in ttkbootstrap/colorutils.py
def update_hsl_value(color, hue=None, sat=None, lum=None, inmodel=HSL, outmodel=HSL):
    """Change hue, saturation, or lumenosity of the color based on the
    hue, sat, lum parameters provided.

    Parameters:

        color (Any):
            The color

        hue (int):
            A number between 0 and 360.

        sat (int):
            A number between 0 and 100.

        lum (int):
            A number between 0 and 100.

        inmodel (str):
            The color model used by the color to be changed. One of
            hsl, rgb, hex, name.

        outmodel (str):
            The color value model to be returned when the color is
            changed. One of hsl, rgb, hex.

    Returns:

        Union[Tuple[int, int, int], str]:
            The color value based on the selected color model.
    """
    h, s, l = color_to_hsl(color, inmodel)
    if hue is not None:
        h = hue
    if sat is not None:
        s = sat
    if lum is not None:
        l = lum
    if outmodel == RGB:
        return color_to_rgb([h, s, l], HSL)
    elif outmodel == HEX:
        return color_to_hex([h, s, l], HSL)
    else:
        return h, s, l