From 98254b9dc47a889d67d9b3bf603497f7ba9b2af5 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Sun, 4 May 2025 04:45:04 +0800 Subject: [PATCH] add hex2xterm & readme --- Readme.md | 35 +++++++++++++++++++++ dot_local/bin/hex2xterm | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 dot_local/bin/hex2xterm diff --git a/Readme.md b/Readme.md index 7e17db6..69acf7a 100644 --- a/Readme.md +++ b/Readme.md @@ -140,6 +140,41 @@ rm -rf ~/.config/alacritty ~/.config/zellij +擴充的小工具 +-------------------------------------------------------------------------------- +### git-pushmulti 同時上傳到多個git server +* 本體在:`~/.local/bin/git-pushmulti` + +已經設定在 `.gitconfig` 裡面的alias,用法簡述: + +``` +git pushmulti "(origin,github)" master +``` + +範例中origin,github就是remote名。 + +PS. 因為zsh與bash的限制,一旦用到`{}`符號,會跳脫在shell層處理,進不去執行檔腳本,所以權宜之計要用 `"()"`包起來 + +### imgcat 直接 cat顯示圖片檔 +* 本體在:`~/.local/share/chezmoi/.chezmoitemplates/common.sh.tmpl` + +PS. 你的終端機程式要支援sixel,已知可用的: Konsole, iTerm2, Alacritty(非官方修改版) + +``` +imgcat /usr/share/wallpapers/Mountain/contents/images/1080x1920.png +``` + +### hex2xterm 色碼轉接近的xterm-256顏色數字編號 +* 本體在:`~/.local/bin/hex2xterm` + +用法範例: +``` +hex2xterm "#FFFFFF" + +# Output: +# 最接近的 Xterm 色碼為:231 +# Xterm RGB:#FFFFFF +``` zsh -------------------------------------------------------------------------------- diff --git a/dot_local/bin/hex2xterm b/dot_local/bin/hex2xterm new file mode 100755 index 0000000..aefb306 --- /dev/null +++ b/dot_local/bin/hex2xterm @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +import sys + +# 取自 xterm 256 色表 +def xterm_colors(): + colors = [] + + # 0-15: 系統顏色(略過) + for i in range(16): + colors.append((0, 0, 0)) # dummy + + # 16–231: 6x6x6 color cube + levels = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff] + for r in levels: + for g in levels: + for b in levels: + colors.append((r, g, b)) + + # 232–255: grayscale ramp + for gray in range(8, 248, 10): + colors.append((gray, gray, gray)) + + return colors + +def hex_to_rgb(hexcode): + hexcode = hexcode.lstrip('#') + if len(hexcode) != 6: + raise ValueError("請輸入有效的 HEX 色碼,例如 #566177") + r = int(hexcode[0:2], 16) + g = int(hexcode[2:4], 16) + b = int(hexcode[4:6], 16) + return (r, g, b) + +def color_distance(c1, c2): + return sum((a - b) ** 2 for a, b in zip(c1, c2)) + +def find_nearest_xterm_color(hexcode): + rgb = hex_to_rgb(hexcode) + colors = xterm_colors() + + best_index = 16 # 開始於 16(跳過前 16 個) + best_distance = float('inf') + + for i in range(16, 256): + dist = color_distance(rgb, colors[i]) + if dist < best_distance: + best_index = i + best_distance = dist + + return best_index, colors[best_index] + +def main(): + if len(sys.argv) != 2: + print("使用方式:hex2xterm.py #RRGGBB") + return + + hexcode = sys.argv[1] + try: + idx, rgb = find_nearest_xterm_color(hexcode) + print(f"最接近的 Xterm 色碼為:{idx}") + print(f"Xterm RGB:#{rgb[0]:02X}{rgb[1]:02X}{rgb[2]:02X}") + except Exception as e: + print(f"錯誤:{e}") + +if __name__ == "__main__": + main()