跳转至

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:

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

model (str):
    The color model being converted.

Returns:

str:
    The hexadecimal color value.
Source code in src/ttkbootstrap/colorutils.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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:

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.
Source code in src/ttkbootstrap/colorutils.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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:

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.
Source code in src/ttkbootstrap/colorutils.py
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
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:

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.
Source code in src/ttkbootstrap/colorutils.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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:

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.
Source code in src/ttkbootstrap/colorutils.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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:

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.
Source code in src/ttkbootstrap/colorutils.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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