Last active
May 27, 2024 13:02
-
-
Save marslo/8e4e1988de79957deb12f0eecec588ec to your computer and use it in GitHub Desktop.
color utilis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# @author : Anthony Bourdain | |
# @credit : https://stackoverflow.com/a/55073732/2940319 | |
# @usage : | |
# - `rgbtohex 17 0 26` ==> 1001A | |
# - `rgbtohex -h 17 0 26` ==> #1001A | |
function rgbtohex () { | |
addleadingzero () { awk '{if(length($0)<2){printf "0";} print $0;}';} | |
if [[ ${1} == "-h" ]]; then | |
r=${2}; g=${3}; b=${4};h='#'; | |
else | |
r=${1}; g=${2}; b=${3};h=''; | |
fi | |
r=$(echo "obase=16; ${r}" | bc | addleadingzero) | |
g=$(echo "obase=16; ${g}" | bc | addleadingzero) | |
b=$(echo "obase=16; ${b}" | bc | addleadingzero) | |
echo "${h}${r}${g}${b}" | |
} | |
# @author : Anthony Bourdain | |
# @credit : https://stackoverflow.com/a/55073732/2940319 | |
# @usage : | |
# - `rgbto256 0 95, 135` ==> 22 | |
function rgbto256 () { | |
echo "define trunc(x){auto os;os=scale;scale=0;x/=1;scale=os;return x;};" \ | |
"16 + 36 * trunc(${1}/51) + 6 * trunc(${2}/51) +" \ | |
" trunc(${3}/51)" | bc | |
# XTerm Color Number = 16 + 36 * R + 6 * G + B | 0 <= R,G,B <= 5 | |
} | |
# @author : Anthony Bourdain | |
# @credit : https://stackoverflow.com/a/55073732/2940319 | |
# @usage : | |
# - `hexttorgb "11001A" ==> 17 0 26 | |
# - `hexttorgb "#11001A" ==> 17 0 26 | |
function hextorgb () { | |
hexinput=$(echo "${1}" | tr '[:lower:]' '[:upper:]') # uppercase-ing | |
hexinput=$(echo "${hexinput}" | tr -d '#') # remove Hash if needed | |
a=$(echo "${hexinput}" | cut -c-2) | |
b=$(echo "${hexinput}" | cut -c3-4) | |
c=$(echo "${hexinput}" | cut -c5-6) | |
r=$(echo "ibase=16; ${a}" | bc) | |
g=$(echo "ibase=16; ${b}" | bc) | |
b=$(echo "ibase=16; ${c}" | bc) | |
echo "${r} ${g} ${b}" | |
} | |
# Generates Truecolor Escape Sequences from Hex Strings. (remove '\\' to use) | |
# @author : Anthony Bourdain | |
# @credit : https://stackoverflow.com/a/55073732/2940319 | |
# @params : | |
# -fg Prints as a foreground color. (default) | |
# -bg Prints as a background color. | |
# @usage : | |
# - `trueHexPrint -fg "11001A" ==> '\e[38;2;17;0;26m' | |
# - `trueHexPrint -bg "11001A" ==> '\e[48;2;17;0;26m' | |
function trueHexPrint () { | |
if [[ ${1} =~ "-fg" || ${1} =~ "-f" ]]; then | |
fgbg=38; hexinput=${2}; | |
elif [[ ${1} =~ "-bg" || ${1} =~ "-b" ]]; then | |
fgbg=48; hexinput=${2}; | |
else | |
fgbg=38; hexinput=${1} | |
fi | |
hexinput=$(echo "${hexinput}" | tr '[:lower:]' '[:upper:]') # uppercase-ing | |
hexinput=$(echo "${hexinput}" | tr -d '#') # remove Hash if needed | |
a=$(echo "${hexinput}" | cut -c-2) | |
b=$(echo "${hexinput}" | cut -c3-4) | |
c=$(echo "${hexinput}" | cut -c5-6) | |
r=$(echo "ibase=16; ${a}" | bc) | |
g=$(echo "ibase=16; ${b}" | bc) | |
b=$(echo "ibase=16; ${c}" | bc) | |
printf "\\\\e[%s;2;%s;%s;%sm" "${fgbg}" "${r}" "${g}" "${b}" # Remove one set of '\\' to utilize | |
} | |
# @author : Anthony Bourdain | |
# @credit : https://stackoverflow.com/a/55073732/2940319 | |
function XColorTable () { | |
i=16 | |
for ((r = 0; r <= 255; r+=40)); do # do tricolor | |
for ((g = 0; g <= 255; g+=40)); do | |
for ((b = 0; b <= 255; b+=40)); do | |
hex=$(rgbtohex ${r} ${g} ${b}) | |
printf "%-3s = %-18s => %b\n" "$((i++))" "rgb(${r}, ${g}, ${b})" "$(trueHexPrint "${hex}")#${hex}\e[0m" | |
if ((b == 0)); then b=55; fi | |
done | |
if ((g == 0)); then g=55; fi | |
done | |
if ((r == 0)); then r=55; fi | |
done | |
for ((m = 8; m <= 238; m+=10)); do # do monochrome | |
hex=$(rgbtohex ${m} ${m} ${m}) | |
printf "%-3s = %-18s => %b\n" "$((i++))" "rgb(${m}, ${m}, ${m})" "$(trueHexPrint "${hex}")#${hex}\x1b[0m" | |
done | |
} | |
# Generates 256 Color code table with RGB and Hex values | |
# @author : marslo | |
# @usage : | |
# - to show specific color(s) with parameter(s) : | |
# `xCodeTable 30 40 50` ==> '30 = rgb(0, 135, 135) => #008787\n...\n...' | |
# - to show all 256 colors code table without parameter : | |
# `xCodeTable` | |
function xColorTable() { | |
declare -A COLORS | |
declare -i i=16 | |
function _prettyPrint() { | |
local _r _g _b colorCode hexCode | |
colorCode="$1" | |
IFS=' ' read -r _r _g _b <<< "$2"; unset IFS; | |
hexCode=$(rgbtohex "${_r}" "${_g}" "${_b}") | |
printf "%-3s = %-18s => %b\n" "${colorCode}" "rgb(${_r}, ${_g}, ${_b})" "$(trueHexPrint "${hexCode}")#${hexCode}\x1b[0m" | |
} | |
for ((r = 0; r <= 255; r+=40)); do # do tricolor | |
for ((g = 0; g <= 255; g+=40)); do | |
for ((b = 0; b <= 255; b+=40)); do | |
COLORS[$i]+="$r $g $b" | |
(( i++ )) | |
if ((b == 0)); then b=55; fi | |
done | |
if ((g == 0)); then g=55; fi | |
done | |
if ((r == 0)); then r=55; fi | |
done | |
for ((m = 8; m <= 238; m+=10)); do # do monochrome | |
COLORS[$i]+="$m $m $m" | |
(( i++ )) | |
done | |
[[ 0 -ne "$#" ]] && output=$( echo "$@" | fmt -1) || output=$(printf "%s\n" "${!COLORS[@]}" | sort -n) | |
while read -r _c; do | |
_prettyPrint "${_c}" "${COLORS[${_c}]}" | |
done <<< "${output}" | |
} | |
# vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment