TTkColor๐ถ๏ธ
- class TTkColor(fg: Tuple[int, int, int] | None = None, bg: Tuple[int, int, int] | None = None, colorMod=None, clean=False)[source]๐ถ๏ธ
Bases:
objectTermTk Color helper
The TTkColor constructor creates the color based on HEX values.
Example:
# Foreground only colors: color_fg_red = TTkColor.fg('#FF0000') color_fg_green = TTkColor.fg('#00FF00') color_fg_blue = TTkColor.fg('#0000FF') # Background only colors: color_bg_red = TTkColor.bg('#FF0000') color_bg_green = TTkColor.bg('#00FF00') color_bg_blue = TTkColor.bg('#0000FF') # Combine color_1 = color_fg_red + color_bg_blue color_2 = color_fg_red + TTkColor.bg('#FFFF00') color_3 = color_2 + TTkColor.UNDERLINE + TTkColor.BOLD # Use presets color_4 = TTkColor.RED color_5 = TTkColor.BG_YELLOW + color_4 color_6 = color_5 + TTkColor.UNDERLINE + TTkColor.BOLD
Create a color container with optional foreground/background and modifier.
- param fg:
foreground RGB triplet
- type fg:
tuple[int, int, int] | None
- param bg:
background RGB triplet
- type bg:
tuple[int, int, int] | None
- param colorMod:
optional runtime color modifier
- type colorMod:
TTkColorModifier | None
- param clean:
force emitting a full reset before this color
- type clean:
bool
Methods๐ถ๏ธ
- static ansi(ansi: str) TTkColor[source]๐ถ๏ธ
Parse an ANSI escape sequence and return the represented color.
- background() TTkColor[source]๐ถ๏ธ
Return a color object containing only the background component.
- Returns:
a color with only background information if available, otherwise
TTkColor.RST- Return type:
- static bg(color: str, *, link: str = '', modifier: TTkColorModifier | None = None) TTkColor[source]๐ถ๏ธ
Helper to generate a Background color
Example:
color_1 = TTkColor.bg('#FF0000') color_2 = TTkColor.bg(color='#00FF00') color_3 = TTkColor.bg('#0000FF', modifier=TTkColorGradient(increment=6))
- Parameters:
color (str) โ the color representation in (str)HEX
link (str) โ (optional) hyperlink URL to associate with the color
modifier (TTkColorModifier, optional) โ (experimental) the color modifier to be used to improve the kinkiness
- Returns:
- bgToRGB() Tuple[int, int, int][source]๐ถ๏ธ
Return background RGB values.
- Returns:
background RGB tuple, or
(0,0,0)when unset- Return type:
tuple[int, int, int]
- blinking() bool[source]๐ถ๏ธ
Check whether blinking style is active.
- Returns:
False for base colors without style flags
- Return type:
bool
- bold() bool[source]๐ถ๏ธ
Check whether bold style is active.
- Returns:
False for base colors without style flags
- Return type:
bool
- colorType() int[source]๐ถ๏ธ
Return the bitmask describing which color features are active.
The result combines values from
TTkK.ColorTypefor foreground, background, and color modifiers.- Returns:
bitmask with active color feature flags
- Return type:
int
- copy(modifier=True) TTkColor[source]๐ถ๏ธ
Create a copy of this color.
- Parameters:
modifier (bool) โ include a copied color modifier when available
- Returns:
color copy
- Return type:
- static fg(color: str, *, link: str = '', modifier: TTkColorModifier | None = None) TTkColor[source]๐ถ๏ธ
Helper to generate a Foreground color
Example:
color_1 = TTkColor.fg('#FF0000') color_2 = TTkColor.fg(color='#00FF00') color_3 = TTkColor.fg('#0000FF', modifier=TTkColorGradient(increment=6))
- Parameters:
color (str) โ the color representation in (str)HEX
link (str) โ (optional) hyperlink URL to associate with the color
modifier (TTkColorModifier, optional) โ (experimental) the color modifier to be used to improve the kinkiness
- Returns:
- fgToRGB() Tuple[int, int, int][source]๐ถ๏ธ
Return foreground RGB values.
- Returns:
foreground RGB tuple, or
(0,0,0)when unset- Return type:
tuple[int, int, int]
- static fgbg(fg: str = '', bg: str = '', *, link: str = '', modifier: TTkColorModifier | None = None) TTkColor[source]๐ถ๏ธ
Helper to generate a Foreground and Background color
Example:
color_1 = TTkColor.fgbg('#FF0000','#0000FF') color_2 = TTkColor.fgbg(fg='#00FF00',bg='#0000FF') color_3 = TTkColor.fgbg('#0000FF','#0000FF', modifier=TTkColorGradient(increment=6))
- Parameters:
fg (str) โ the foreground color representation in (str)HEX
bg (str) โ the background color representation in (str)HEX
link (str) โ (optional) hyperlink URL to associate with the color
modifier (TTkColorModifier, optional) โ (experimental) the color modifier to be used to improve the kinkiness
- Returns:
- foreground() TTkColor[source]๐ถ๏ธ
Return a color object containing only the foreground component.
- Returns:
a color with only foreground information if available, otherwise
TTkColor.RST- Return type:
- getHex(ctype) str[source]๐ถ๏ธ
Return the selected component as a hexadecimal color string.
- Parameters:
ctype (int) โ target component, usually one of
TTkK.ColorType- Returns:
lowercase hexadecimal RGB string in the form
#rrggbb- Return type:
str
- hasBackground() bool[source]๐ถ๏ธ
Check whether this color has a background component.
- Returns:
True if a background color is set
- Return type:
bool
- hasForeground() bool[source]๐ถ๏ธ
Check whether this color has a foreground component.
- Returns:
True if a foreground color is set
- Return type:
bool
- static hexToRGB(val) Tuple[int, int, int][source]๐ถ๏ธ
Convert a hexadecimal color string (
#rrggbb) to RGB.- Parameters:
val (str) โ hexadecimal color string
- Returns:
RGB tuple
- Return type:
tuple[int, int, int]
- static hsl2rgb(hsl) Tuple[int, int, int][source]๐ถ๏ธ
Convert HSL values to RGB.
- Parameters:
hsl (tuple[int, int, int]) โ HSL tuple as
(hue[0..359], saturation[0..100], lightness[0..100])- Returns:
RGB tuple where each component is in
0..255- Return type:
tuple[int, int, int]
- invertFgBg() TTkColor[source]๐ถ๏ธ
Return a copy with foreground and background swapped.
- Returns:
color copy with foreground/background inverted
- Return type:
- italic() bool[source]๐ถ๏ธ
Check whether italic style is active.
- Returns:
False for base colors without style flags
- Return type:
bool
- mod(x, y) TTkColor[source]๐ถ๏ธ
Apply the configured color modifier at position
(x, y).- Parameters:
x (int) โ horizontal coordinate
y (int) โ vertical coordinate
- Returns:
transformed color, or self when no modifier is set
- Return type:
- modParam(*args, **kwargs) TTkColor[source]๐ถ๏ธ
Return a copy with updated color-modifier parameters.
- Returns:
updated color instance; unchanged instance when no modifier is set
- Return type:
- static rgb2hsl(rgb) Tuple[int, int, int][source]๐ถ๏ธ
Convert RGB values to HSL.
- Parameters:
rgb (tuple[int, int, int]) โ RGB tuple where each component is in
0..255- Returns:
HSL tuple as
(hue[0..359], saturation[0..100], lightness[0..100])- Return type:
tuple[int, int, int]
- strikethrough() bool[source]๐ถ๏ธ
Check whether strikethrough style is active.
- Returns:
False for base colors without style flags
- Return type:
bool
TTkColor Attributes๐ถ๏ธ
|
(bg) #000000 - Black |
|
(bg) #0000FF - Blue |
|
(bg) #00FFFF - Cyan |
|
(bg) #00FF00 - Green |
|
(bg) #FF00FF - Magenta |
|
(bg) #FF0000 - Red |
|
(bg) #FFFFFF - White |
|
(bg) #FFFF00 - Yellow |
|
(fg) #000000 - Black |
|
"Blinking" modifier |
|
(fg) #0000FF - Blue |
|
Bold modifier |
|
(fg) #00FFFF - Cyan |
|
(fg) #000000 - Black |
|
(fg) #0000FF - Blue |
|
(fg) #00FFFF - Cyan |
|
(fg) #00FF00 - Green |
|
(fg) #FF00FF - Magenta |
|
(fg) #FF0000 - Red |
|
(fg) #FFFFFF - White |
|
(fg) #FFFF00 - Yellow |
|
(fg) #00FF00 - Green |
|
Italic modifier |
|
(fg) #FF00FF - Magenta |
|
(fg) #FF0000 - Red |
|
Reset to the default terminal color and modifiers |
|
Striketrough modifier |
|
Underline modifier |
|
(fg) #FFFFFF - White |
|
(fg) #FFFF00 - Yellow |