Last active
January 8, 2025 16:14
-
-
Save kutsan/65fe43e4689c0a5db61021f048d4839d to your computer and use it in GitHub Desktop.
Test true color support for terminal emulator.
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 zsh | |
## | |
# Test true color support for terminal emulator. | |
# | |
# To test it, `source` this file and use `print_true_color f1c40f 'something'`. | |
# | |
# @author Kutsan Kaplan <me@kutsankaplan.com> | |
# @license WTFPL | |
# @version 1.0.0 | |
## | |
## | |
# @param {string} $1 HEX color code without '#' symbol. | |
# @param {string} $2 Test text. | |
## | |
function print_true_color() { | |
local RGB=$(printf "%d %d %d" 0x${1:0:2} 0x${1:2:2} 0x${1:4:2}) | |
typeset -g tone | |
foreach tone ('R' 'G' 'B') { | |
local i=${i:-1} | |
local $tone=$(printf $RGB | cut --fields=$i --delimiter=' ') | |
let i++ | |
} | |
unset tone | |
local fg_lc="%{\e[38;2;" | |
local bg_lc="%{\e[48;2;" | |
local rc="m%}$2%{\e[0m%}" | |
print -P "${fg_lc}${R};${G};${B}${rc}" | |
} |
Comments are disabled for this gist.