#!/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()