add hex2xterm & readme

This commit is contained in:
Yuan Chiu 2025-05-04 04:45:04 +08:00
parent cdbc062183
commit 98254b9dc4
2 changed files with 102 additions and 0 deletions

View File

@ -140,6 +140,41 @@ rm -rf ~/.config/alacritty ~/.config/zellij
</details> </details>
擴充的小工具
--------------------------------------------------------------------------------
### 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 zsh
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

67
dot_local/bin/hex2xterm Executable file
View File

@ -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
# 16231: 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))
# 232255: 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()