Skip to content

Instantly share code, notes, and snippets.

@kutsan
Last active February 10, 2021 12:17
Show Gist options
  • Save kutsan/65fe43e4689c0a5db61021f048d4839d to your computer and use it in GitHub Desktop.
Save kutsan/65fe43e4689c0a5db61021f048d4839d to your computer and use it in GitHub Desktop.
Test true color support for terminal emulator.
#!/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 <[email protected]>
# @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}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment