tput setafカラーテーブル?カラーコードを決定する方法は?

tput setafカラーテーブル?カラーコードを決定する方法は?

ターミナルを着色していますPS1

tputたとえば、次のようにカラー変数を設定します。

PURPLE=$(tput setaf 125)

質問:

125他の色(例えば)の色コードをどのように見つけることができますか?

どこかにカラーチャートガイド/チートシートがありますか?

それが何であるかよくわかりません125... 16進数の色を取得して使用できる数値に変換する方法はありますかsetaf

ベストアンサー1

入力できる色の数はで指定されますtput colors

setf基本的な8つの色を見るには(urxvt端末とxterm端末に使用されますsetaf):

$ printf '\e[%sm▒' {30..37} 0; echo           ### foreground
$ printf '\e[%sm ' {40..47} 0; echo           ### background

通常、名前は次のとおりです。

Color       #define       Value       RGB
black     COLOR_BLACK       0     0, 0, 0
red       COLOR_RED         1     max,0,0
green     COLOR_GREEN       2     0,max,0
yellow    COLOR_YELLOW      3     max,max,0
blue      COLOR_BLUE        4     0,0,max
magenta   COLOR_MAGENTA     5     max,0,max
cyan      COLOR_CYAN        6     0,max,max
white     COLOR_WHITE       7     max,max,max

拡張256色を表示するには(setafurxvtで使用):

$ printf '\e[48;5;%dm ' {0..255}; printf '\e[0m \n'

数値でソートされた出力が必要な場合:

#!/bin/bash
color(){
    for c; do
        printf '\e[48;5;%dm%03d' $c $c
    done
    printf '\e[0m \n'
}

IFS=$' \t\n'
color {0..15}
for ((i=0;i<6;i++)); do
    color $(seq $((i*36+16)) $((i*36+51)))
done
color {232..255}

インデックスラベル付き256のソートされたカラーチャート


1,600万色にはかなりの量のコードが必要です(一部のコンソールではこれを表示できません)。
基本は次のとおりです。

fb=3;r=255;g=1;b=1;printf '\e[0;%s8;2;%s;%s;%sm▒▒▒ ' "$fb" "$r" "$g" "$b"

fbはいfront/backまたは3/4

複数の色をレンダリングするコンソール機能の簡単なテストは次のとおりです。

for r in {200..255..5}; do fb=4;g=1;b=1;printf '\e[0;%s8;2;%s;%s;%sm   ' "$fb" "$r" "$g" "$b"; done; echo

赤い線、暗い色から明るい色(左から右へ) 左から右に色の変化がほとんどない赤い線で表示されます。この小さな変化が目に見える場合、コンソールは1600万色を表示できます。

RGB(赤、緑、青)の場合、それぞれrgおよび bは0から255の間の値です。

コンソールタイプがこの機能をサポートしている場合、このコードはカラーマップを生成します。

mode2header(){
    #### For 16 Million colors use \e[0;38;2;R;G;Bm each RGB is {0..255}
    printf '\e[mR\n' # reset the colors.
    printf '\n\e[m%59s\n' "Some samples of colors for r;g;b. Each one may be 000..255"
    printf '\e[m%59s\n'   "for the ansi option: \e[0;38;2;r;g;bm or \e[0;48;2;r;g;bm :"
}
mode2colors(){
    # foreground or background (only 3 or 4 are accepted)
    local fb="$1"
    [[ $fb != 3 ]] && fb=4
    local samples=(0 63 127 191 255)
    for         r in "${samples[@]}"; do
        for     g in "${samples[@]}"; do
            for b in "${samples[@]}"; do
                printf '\e[0;%s8;2;%s;%s;%sm%03d;%03d;%03d ' "$fb" "$r" "$g" "$b" "$r" "$g" "$b"
            done; printf '\e[m\n'
        done; printf '\e[m'
    done; printf '\e[mReset\n'
}
mode2header
mode2colors 3
mode2colors 4

例 前景色チャートとそのインデックスをラベルとして使用

背景色チャートとそのインデックスをラベルとして例

16進カラー値を(最も近い)0から255カラーインデックスに変換するには:

fromhex(){
    hex=${1#"#"}
    r=$(printf '0x%0.2s' "$hex")
    g=$(printf '0x%0.2s' ${hex#??})
    b=$(printf '0x%0.2s' ${hex#????})
    printf '%03d' "$(( (r<75?0:(r-35)/40)*6*6 + 
                       (g<75?0:(g-35)/40)*6   +
                       (b<75?0:(b-35)/40)     + 16 ))"
}

次のように使用してください。

$ fromhex 00fc7b
048
$ fromhex #00fc7b
048

照会に使用される色番号HTMLカラーフォーマット:

#!/bin/dash
tohex(){
    dec=$(($1%256))   ### input must be a number in range 0-255.
    if [ "$dec" -lt "16" ]; then
        bas=$(( dec%16 ))
        mul=128
        [ "$bas" -eq "7" ] && mul=192
        [ "$bas" -eq "8" ] && bas=7
        [ "$bas" -gt "8" ] && mul=255
        a="$((  (bas&1)    *mul ))"
        b="$(( ((bas&2)>>1)*mul ))" 
        c="$(( ((bas&4)>>2)*mul ))"
        printf 'dec= %3s basic= #%02x%02x%02x\n' "$dec" "$a" "$b" "$c"
    elif [ "$dec" -gt 15 ] && [ "$dec" -lt 232 ]; then
        b=$(( (dec-16)%6  )); b=$(( b==0?0: b*40 + 55 ))
        g=$(( (dec-16)/6%6)); g=$(( g==0?0: g*40 + 55 ))
        r=$(( (dec-16)/36 )); r=$(( r==0?0: r*40 + 55 ))
        printf 'dec= %3s color= #%02x%02x%02x\n' "$dec" "$r" "$g" "$b"
    else
        gray=$(( (dec-232)*10+8 ))
        printf 'dec= %3s  gray= #%02x%02x%02x\n' "$dec" "$gray" "$gray" "$gray"
    fi
}

for i in $(seq 0 255); do
    tohex ${i}
done

次のように使用してください(「基本」は最初の16色、「色」は基本グループ、「灰色」は最後の灰色です)。

$ tohex 125                  ### A number in range 0-255
dec= 125 color= #af005f
$ tohex 6
dec=   6 basic= #008080
$ tohex 235
dec= 235  gray= #262626

おすすめ記事