aboutsummaryrefslogtreecommitdiff
path: root/thcolor/utils.py
blob: f18482e5f45ae0768a80f75d7f133c953970a181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2022 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
# *****************************************************************************
""" Utilities for the thcolor module. """

from thcolor.colors import SRGBColor as _SRGBColor

__all__ = ['factor', 'rgb']


def rgb(x):
    """ Return an RGB color out of the given 6-digit hexadecimal code. """

    return _SRGBColor(
        int(x[1:3], 16) / 255,
        int(x[3:5], 16) / 255,
        int(x[5:7], 16) / 255,
    )


def factor(x, max_: int = 100, clip: bool = False):
    """ Return a factor based on if something is a float or an int. """

    if isinstance(x, float):
        pass
    elif x in (0, 1) and max_ == 100:
        x = float(x)
    else:
        x /= max_

    if clip:
        x = max(0, min(1, x))

    return x

# End of file.