commit dd1564fdbc9103f88dcc75518bdffeded6d66a23 Author: Andreev Gregory <1@example.com> Date: Tue Mar 25 12:22:22 2025 +0300 tui_colors.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a62ffe6 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +include config.mk + +all: + + +install: + mkdir -p ${PREFIX}/bin + cp tui_colors.py ${PREFIX}/bin + chmod 755 ${PREFIX}/bin/tui_colors.py + +uninstall: + rm -f ${PREFIX}/bin/tui_colors.py + +.PHONY: install uninstall diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..a67a797 --- /dev/null +++ b/config.mk @@ -0,0 +1 @@ +PREFIX=/usr/local diff --git a/tui_colors.py b/tui_colors.py new file mode 100755 index 0000000..23c3271 --- /dev/null +++ b/tui_colors.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +import os + +def determine_group(col): + if col == 0 or col == 7 or col == 8 or col == 15 or col >= 232: + return 0 + if col < 8: + return col + if col < 16: + return col - 8 + cp = col - 16 + r = cp // 36 + g = ((cp % 36) // 6) + b = cp % 6 + if r == g and g == b: + return 0 + r /= 5 + g /= 5 + b /= 5 + max_val = max(r, g, b) + min_val = min(r, g, b) + delta = max_val - min_val + if max_val == r: + hue = (g - b) / delta + hue = hue + 6 if hue < 0 else hue + elif max_val == g: + hue = (b - r) / delta + 2 + else: + hue = (r - g) / delta + 4 + if hue < 0.5: + return 1 + if hue < 1.5: + return 3 + if hue < 2.5: + return 2 + if hue < 3.5: + return 6 + if hue < 4.5: + return 4 + if hue < 5.5: + return 5 + return 1 + + +def get_contrasting_color_rgb(r, g, b): + luminance = (0.299 * r + 0.587 * g + 0.114 * b) + return 15 if luminance < 0.7 else 0 + + +def get_contrasting_color(col): + if col == 7: + return 0 + if col == 8: + return 15 + if col < 8: + return 15 + if col < 16: + return 0 + if col > 249: + return 0 + if col >= 232: + return 15 + cp = col - 16 + r = cp // 36 + g = ((cp % 36) // 6) + b = cp % 6 + return get_contrasting_color_rgb(r / 5, g / 5, b / 5) + + +def print_color(col, fg_col): + print(f"\x1b[48;5;{col}m\x1b[38;5;{fg_col}m {col:3} \x1b[0m ", end="") + +def print_color_block(col): + print_color(col, get_contrasting_color(col)) + +def print_empty_block(): + print(end=" ") + +if __name__ == "__main__": + groups = [[] for i in range(7)] + for col in range(256): + groups[determine_group(col)].append(col) + + width = os.get_terminal_size().columns + height = os.get_terminal_size().lines + k = (width - 13) // 42 + if k < 1: + for i in range(7): + w = 0 + for col in groups[i]: + if w + 6 >= width: + print() + w = 0 + print_color_block(col) + w += 6 + print("\n") + else: + n = max(map(lambda i: len(groups[i]) // k + (1 if len(groups[i]) % k else 0), range(7))) + vk = max(1, height // n - 1) + for L in range(n): + if L: + print(end="\n" * vk) + for i in range(7): + if i: + print(end=" ") + for j in range(k): + gi = L * k + j + if gi < len(groups[i]): + print_color_block(groups[i][gi]) + else: + print_empty_block()