CustomTkinter/customtkinter/customtkinter_color_manager.py

64 lines
2.3 KiB
Python
Raw Normal View History

2021-03-05 02:32:17 +03:00
import sys
2021-03-04 20:27:46 +03:00
class CTkColorManager:
2021-09-13 15:27:29 +03:00
2021-03-04 20:27:46 +03:00
MAIN = ("#1C94CF", "#1C94CF")
MAIN_HOVER = ("#5FB4DD", "#5FB4DD")
ENTRY = ("white", "#222222")
TEXT = ("black", "white")
SLIDER_BG = ("#6B6B6B", "#222222")
PROGRESS_BG = ("#6B6B6B", "#222222")
FRAME = ("#D4D5D6", "#3F3F3F")
2021-03-05 22:27:34 +03:00
FRAME_2 = ("#BFBEC1", "#505050")
2021-08-25 18:02:16 +03:00
CHECKBOX_LINES = ("black", "#ededed")
2021-03-04 20:27:46 +03:00
2021-07-23 01:44:13 +03:00
DARKEN_COLOR_FACTOR = 0.8 # used for generate color for disabled button
@staticmethod
2021-09-13 15:27:29 +03:00
def single_color(color, appearance_mode: int) -> str:
""" color can be either a single hex color string or a color name or it can be a
tuple color with (light_color, dark_color). The functions then returns
always a single color string """
if type(color) == tuple:
return color[appearance_mode]
else:
return color
@staticmethod
def rgb2hex(rgb_color: tuple):
2021-07-23 01:44:13 +03:00
return "#{:02x}{:02x}{:02x}".format(round(rgb_color[0]), round(rgb_color[1]), round(rgb_color[2]))
@staticmethod
def hex2rgb(hex_color: str):
return tuple(int(hex_color.strip("#")[i:i+2], 16) for i in (0, 2, 4))
@staticmethod
2021-09-13 15:27:29 +03:00
def darken_hex_color(hex_color: str) -> str:
2021-07-23 01:44:13 +03:00
try:
rgb_color = CTkColorManager.hex2rgb(hex_color)
dark_rgb_color = (rgb_color[0] * CTkColorManager.DARKEN_COLOR_FACTOR,
rgb_color[1] * CTkColorManager.DARKEN_COLOR_FACTOR,
rgb_color[2] * CTkColorManager.DARKEN_COLOR_FACTOR)
return CTkColorManager.rgb2hex(dark_rgb_color)
except Exception as err:
2021-09-13 15:27:29 +03:00
sys.stderr.write("ERROR (CTkColorManager): failed to darken the following color: " + str(hex_color) + " " + str(err))
2021-07-23 01:44:13 +03:00
return hex_color
2021-03-04 20:27:46 +03:00
@classmethod
2021-09-13 15:27:29 +03:00
def set_theme_color(cls, hex_color: str, hex_color_hover: str):
2021-08-25 18:02:16 +03:00
cls.MAIN = hex_color
cls.MAIN_HOVER = hex_color_hover
2021-03-04 20:27:46 +03:00
@classmethod
2021-09-13 15:27:29 +03:00
def set_theme(cls, main_color: str):
2021-03-04 20:27:46 +03:00
if main_color.lower() == "green":
cls.set_theme_color("#2EDEA4", "#82FCD4")
elif main_color.lower() == "blue":
cls.set_theme_color("#1C94CF", "#5FB4DD")
2021-03-05 02:32:17 +03:00
else:
sys.stderr.write("WARNING (CTkColorManager): No such color theme available: {}\n".format(main_color))