TTkColor๐ŸŒถ๏ธ

class TTkColor(fg: Tuple[int, int, int] | None = None, bg: Tuple[int, int, int] | None = None, colorMod=None, clean=False)[source]๐ŸŒถ๏ธ

Bases: object

TermTk 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.

Parameters:

ansi (str) โ€“ ANSI escape sequence to parse

Returns:

the TTkColor representing the ANSI value

Return type:

TTkColor

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:

TTkColor

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:

TTkColor

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.ColorType for 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:

TTkColor

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:

TTkColor

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:

TTkColor

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:

TTkColor

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:

TTkColor

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:

TTkColor

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:

TTkColor

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

underline() bool[source]๐ŸŒถ๏ธ

Check whether underline style is active.

Returns:

False for base colors without style flags

Return type:

bool

withoutModifiers() TTkColor[source]๐ŸŒถ๏ธ

Return this color without style modifiers.

For base TTkColor, no text-style modifiers are stored, so this method returns the current instance unchanged.

Returns:

the color instance without text-style modifiers

Return type:

TTkColor

TTkColor Attributes๐ŸŒถ๏ธ

BG_BLACK

(bg) #000000 - Black

BG_BLUE

(bg) #0000FF - Blue

BG_CYAN

(bg) #00FFFF - Cyan

BG_GREEN

(bg) #00FF00 - Green

BG_MAGENTA

(bg) #FF00FF - Magenta

BG_RED

(bg) #FF0000 - Red

BG_WHITE

(bg) #FFFFFF - White

BG_YELLOW

(bg) #FFFF00 - Yellow

BLACK

(fg) #000000 - Black

BLINKING

"Blinking" modifier

BLUE

(fg) #0000FF - Blue

BOLD

Bold modifier

CYAN

(fg) #00FFFF - Cyan

FG_BLACK

(fg) #000000 - Black

FG_BLUE

(fg) #0000FF - Blue

FG_CYAN

(fg) #00FFFF - Cyan

FG_GREEN

(fg) #00FF00 - Green

FG_MAGENTA

(fg) #FF00FF - Magenta

FG_RED

(fg) #FF0000 - Red

FG_WHITE

(fg) #FFFFFF - White

FG_YELLOW

(fg) #FFFF00 - Yellow

GREEN

(fg) #00FF00 - Green

ITALIC

Italic modifier

MAGENTA

(fg) #FF00FF - Magenta

RED

(fg) #FF0000 - Red

RST

Reset to the default terminal color and modifiers

STRIKETROUGH

Striketrough modifier

UNDERLINE

Underline modifier

WHITE

(fg) #FFFFFF - White

YELLOW

(fg) #FFFF00 - Yellow