Colors
ttkbootstrap.style.Colors
A class that defines the color scheme for a theme as well as provides several static methods for manipulating colors.
A Colors
object is attached to a ThemeDefinition
and can also
be accessed through the Style.colors
property for the
current theme.
Examples:
style = Style()
# dot-notation
style.colors.primary
# get method
style.colors.get('primary')
This class is an iterator, so you can iterate over the main style color labels (primary, secondary, success, info, warning, danger):
for color_label in style.colors:
color = style.colors.get(color_label)
print(color_label, color)
If, for some reason, you need to iterate over all theme color
labels, then you can use the Colors.label_iter
method. This
will include all theme colors.
for color_label in style.colors.label_iter():
color = Colors.get(color_label)
print(color_label, color)
If you want to adjust the hsv values of an existing color by a
specific percentage (delta), you can use the Colors.update_hsv
method, which is static. In the example below, the "value delta"
or vd
is increased by 15%, which will lighten the color:
Colors.update_hsv("#9954bb", vd=0.15)
__init__(self, primary, secondary, success, info, warning, danger, light, dark, bg, fg, selectbg, selectfg, border, inputfg, inputbg, active)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
primary |
str |
The primary theme color; used by default for all widgets. |
required |
secondary |
str |
An accent color; commonly of a |
required |
success |
str |
An accent color; commonly of a |
required |
info |
str |
An accent color; commonly of a |
required |
warning |
str |
An accent color; commonly of an |
required |
danger |
str |
An accent color; commonly of a |
required |
light |
str |
An accent color. |
required |
dark |
str |
An accent color. |
required |
bg |
str |
Background color. |
required |
fg |
str |
Default text color. |
required |
selectfg |
str |
The color of selected text. |
required |
selectbg |
str |
The background color of selected text. |
required |
border |
str |
The color used for widget borders. |
required |
inputfg |
str |
The text color for input widgets. |
required |
inputbg |
str |
The text background color for input widgets. |
required |
active |
str |
An accent color. |
required |
Source code in ttkbootstrap/style.py
def __init__(
self,
primary,
secondary,
success,
info,
warning,
danger,
light,
dark,
bg,
fg,
selectbg,
selectfg,
border,
inputfg,
inputbg,
active,
):
"""
Parameters:
primary (str):
The primary theme color; used by default for all widgets.
secondary (str):
An accent color; commonly of a `grey` hue.
success (str):
An accent color; commonly of a `green` hue.
info (str):
An accent color; commonly of a `blue` hue.
warning (str):
An accent color; commonly of an `orange` hue.
danger (str):
An accent color; commonly of a `red` hue.
light (str):
An accent color.
dark (str):
An accent color.
bg (str):
Background color.
fg (str):
Default text color.
selectfg (str):
The color of selected text.
selectbg (str):
The background color of selected text.
border (str):
The color used for widget borders.
inputfg (str):
The text color for input widgets.
inputbg (str):
The text background color for input widgets.
active (str):
An accent color.
"""
self.primary = primary
self.secondary = secondary
self.success = success
self.info = info
self.warning = warning
self.danger = danger
self.light = light
self.dark = dark
self.bg = bg
self.fg = fg
self.selectbg = selectbg
self.selectfg = selectfg
self.border = border
self.inputfg = inputfg
self.inputbg = inputbg
self.active = active
get(self, color_label)
Lookup a color value from the color name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_label |
str |
A color label corresponding to a class propery |
required |
Returns:
Type | Description |
---|---|
str |
A hexadecimal color value. |
Source code in ttkbootstrap/style.py
def get(self, color_label: str):
"""Lookup a color value from the color name
Parameters:
color_label (str):
A color label corresponding to a class propery
Returns:
str:
A hexadecimal color value.
"""
return self.__dict__.get(color_label)
get_foreground(self, color_label)
Return the appropriate foreground color for the specified color_label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_label |
str |
A color label corresponding to a class property |
required |
Source code in ttkbootstrap/style.py
def get_foreground(self, color_label):
"""Return the appropriate foreground color for the specified
color_label.
Parameters:
color_label (str):
A color label corresponding to a class property
"""
if color_label == LIGHT:
return self.dark
elif color_label == DARK:
return self.light
else:
return self.selectfg
hex_to_rgb(color)
staticmethod
Convert hexadecimal color to rgb color value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color |
str |
A hexadecimal color value |
required |
Returns:
Type | Description |
---|---|
tuple[int, int, int] |
An rgb color value. |
Source code in ttkbootstrap/style.py
@staticmethod
def hex_to_rgb(color: str):
"""Convert hexadecimal color to rgb color value
Parameters:
color (str):
A hexadecimal color value
Returns:
tuple[int, int, int]:
An rgb color value.
"""
r, g, b = colorutils.color_to_rgb(color)
return r/255, g/255, b/255
label_iter()
staticmethod
Iterate over all color label properties in the Color class
Returns:
Type | Description |
---|---|
iter |
An iterator for color label names |
Source code in ttkbootstrap/style.py
@staticmethod
def label_iter():
"""Iterate over all color label properties in the Color class
Returns:
iter:
An iterator for color label names
"""
return iter(
[
"primary",
"secondary",
"success",
"info",
"warning",
"danger",
"light",
"dark",
"bg",
"fg",
"selectbg",
"selectfg",
"border",
"inputfg",
"inputbg",
"active",
]
)
make_transparent(alpha, foreground, background='#ffffff')
staticmethod
Simulate color transparency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float |
The amount of transparency; a number between 0 and 1. |
required |
foreground |
str |
The foreground color. |
required |
background |
str |
The background color. |
'#ffffff' |
Returns:
Type | Description |
---|---|
str |
A hexadecimal color representing the "transparent" version of the foreground color against the background color. |
Source code in ttkbootstrap/style.py
@staticmethod
def make_transparent(alpha, foreground, background='#ffffff'):
"""Simulate color transparency.
Parameters:
alpha (float):
The amount of transparency; a number between 0 and 1.
foreground (str):
The foreground color.
background (str):
The background color.
Returns:
str:
A hexadecimal color representing the "transparent"
version of the foreground color against the background
color.
"""
fg = ImageColor.getrgb(foreground)
bg = ImageColor.getrgb(background)
rgb_float = [alpha * c1 + (1 - alpha) * c2 for (c1, c2) in zip(fg, bg)]
rgb_int = [int(x) for x in rgb_float]
return '#{:02x}{:02x}{:02x}'.format(*rgb_int)
rgb_to_hex(r, g, b)
staticmethod
Convert rgb to hexadecimal color value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
int |
red |
required |
g |
int |
green |
required |
b |
int |
blue |
required |
Returns:
Type | Description |
---|---|
str |
A hexadecimal color value |
Source code in ttkbootstrap/style.py
@staticmethod
def rgb_to_hex(r: int, g: int, b: int):
"""Convert rgb to hexadecimal color value
Parameters:
r (int):
red
g (int):
green
b (int):
blue
Returns:
str:
A hexadecimal color value
"""
r_ = int(r * 255)
g_ = int(g * 255)
b_ = int(b * 255)
return colorutils.color_to_hex((r_, g_, b_))
rgb_to_hsv(r, g, b)
staticmethod
Convert an rgb to hsv color value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
float |
red |
required |
g |
float |
green |
required |
b |
float |
blue |
required |
Returns:
Type | Description |
---|---|
Tuple[float, float, float] |
The hsv color value. |
Source code in ttkbootstrap/style.py
@staticmethod
def rgb_to_hsv(r, g, b):
"""Convert an rgb to hsv color value.
Parameters:
r (float):
red
g (float):
green
b (float):
blue
Returns:
Tuple[float, float, float]: The hsv color value.
"""
return colorsys.rgb_to_hsv(r, g, b)
set(self, color_label, color_value)
Set a color property value. This does not update any existing widgets. Can also be used to create on-demand color properties that can be used in your program after creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_label |
str |
The name of the color to be set (key) |
required |
color_value |
str |
A hexadecimal color value |
required |
Source code in ttkbootstrap/style.py
def set(self, color_label: str, color_value: str):
"""Set a color property value. This does not update any existing
widgets. Can also be used to create on-demand color properties
that can be used in your program after creation.
Parameters:
color_label (str):
The name of the color to be set (key)
color_value (str):
A hexadecimal color value
"""
self.__dict__[color_label] = color_value
update_hsv(color, hd=0, sd=0, vd=0)
staticmethod
Modify the hue, saturation, and/or value of a given hex color value by specifying the delta.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color |
str |
A hexadecimal color value to adjust. |
required |
hd |
float |
% change in hue, hue delta. |
0 |
sd |
float |
% change in saturation, saturation delta. |
0 |
vd |
float |
% change in value, value delta. |
0 |
Returns:
Type | Description |
---|---|
str |
The resulting hexadecimal color value |
Source code in ttkbootstrap/style.py
@staticmethod
def update_hsv(color, hd=0, sd=0, vd=0):
"""Modify the hue, saturation, and/or value of a given hex
color value by specifying the _delta_.
Parameters:
color (str):
A hexadecimal color value to adjust.
hd (float):
% change in hue, _hue delta_.
sd (float):
% change in saturation, _saturation delta_.
vd (float):
% change in value, _value delta_.
Returns:
str:
The resulting hexadecimal color value
"""
r, g, b = Colors.hex_to_rgb(color)
h, s, v = colorsys.rgb_to_hsv(r, g, b)
# hue
if h * (1 + hd) > 1:
h = 1
elif h * (1 + hd) < 0:
h = 0
else:
h *= 1 + hd
# saturation
if s * (1 + sd) > 1:
s = 1
elif s * (1 + sd) < 0:
s = 0
else:
s *= 1 + sd
# value
if v * (1 + vd) > 1:
v = 0.95
elif v * (1 + vd) < 0.05:
v = 0.05
else:
v *= 1 + vd
r, g, b = colorsys.hsv_to_rgb(h, s, v)
return Colors.rgb_to_hex(r, g, b)