| 1 |
#!/bin/sh |
| 2 |
############################################################################## |
| 3 |
# Copyright 2020,2025 Thomas E. Dickey # |
| 4 |
# Copyright 2016 Free Software Foundation, Inc. # |
| 5 |
# # |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a # |
| 7 |
# copy of this software and associated documentation files (the "Software"), # |
| 8 |
# to deal in the Software without restriction, including without limitation # |
| 9 |
# the rights to use, copy, modify, merge, publish, distribute, distribute # |
| 10 |
# with modifications, sublicense, and/or sell copies of the Software, and to # |
| 11 |
# permit persons to whom the Software is furnished to do so, subject to the # |
| 12 |
# following conditions: # |
| 13 |
# # |
| 14 |
# The above copyright notice and this permission notice shall be included in # |
| 15 |
# all copies or substantial portions of the Software. # |
| 16 |
# # |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # |
| 18 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # |
| 20 |
# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # |
| 21 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # |
| 22 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # |
| 23 |
# DEALINGS IN THE SOFTWARE. # |
| 24 |
# # |
| 25 |
# Except as contained in this notice, the name(s) of the above copyright # |
| 26 |
# holders shall not be used in advertising or otherwise to promote the sale, # |
| 27 |
# use or other dealings in this Software without prior written # |
| 28 |
# authorization. # |
| 29 |
############################################################################## |
| 30 |
# $Id: tput-colorcube,v 1.3 2025/06/14 14:45:23 tom Exp $ |
| 31 |
# Use this script to print an xterm-style color cube, e.g., as done in |
| 32 |
# the xterm 88colors2.pl and 256colors2.pl scripts. |
| 33 |
|
| 34 |
failed() { |
| 35 |
printf "?? %s\n" "$1" >&2 |
| 36 |
exit 1 |
| 37 |
} |
| 38 |
|
| 39 |
newline() { |
| 40 |
tput op |
| 41 |
printf "\n" |
| 42 |
} |
| 43 |
|
| 44 |
if [ $# = 1 ] |
| 45 |
then |
| 46 |
myterm=$1 |
| 47 |
elif [ $# = 0 ] |
| 48 |
then |
| 49 |
myterm=$TERM |
| 50 |
else |
| 51 |
failed "expected one parameter or none" |
| 52 |
fi |
| 53 |
|
| 54 |
colors=$(tput -T "$myterm" colors 2>/dev/null) |
| 55 |
if [ "${colors:-0}" -le 0 ] |
| 56 |
then |
| 57 |
myterm=${myterm%%-color} |
| 58 |
colors=$(tput -T "$myterm" colors 2>/dev/null) |
| 59 |
fi |
| 60 |
if [ "${colors:-0}" -le 0 ] |
| 61 |
then |
| 62 |
failed "terminal $myterm does not support color" |
| 63 |
fi |
| 64 |
|
| 65 |
printf "System colors:\n" |
| 66 |
|
| 67 |
color=0 |
| 68 |
inrow=$colors |
| 69 |
to_do=$colors |
| 70 |
[ "$colors" -gt 256 ] && colors=256 |
| 71 |
[ "$inrow" -gt 8 ] && inrow=8 |
| 72 |
[ "$to_do" -gt 16 ] && to_do=16 |
| 73 |
while [ "$color" -lt "$to_do" ] |
| 74 |
do |
| 75 |
[ "$color" = "$inrow" ] && newline |
| 76 |
tput setab "$color" |
| 77 |
printf ' ' |
| 78 |
color=$(expr "$color" + 1) |
| 79 |
done |
| 80 |
newline |
| 81 |
|
| 82 |
[ "$colors" -le 16 ] && exit |
| 83 |
|
| 84 |
if [ "$colors" = 256 ] |
| 85 |
then |
| 86 |
cube=6 |
| 87 |
ramp=232 |
| 88 |
elif [ "$colors" -ge 88 ] |
| 89 |
then |
| 90 |
cube=4 |
| 91 |
ramp=80 |
| 92 |
else |
| 93 |
exit |
| 94 |
fi |
| 95 |
|
| 96 |
printf "\n" |
| 97 |
printf "Color cube, %dx%dx%d:\n" $cube $cube $cube |
| 98 |
g=0 |
| 99 |
cube2=$(expr $cube \* $cube) |
| 100 |
while [ "$g" -lt "$cube" ] |
| 101 |
do |
| 102 |
r=0 |
| 103 |
while [ "$r" -lt "$cube" ] |
| 104 |
do |
| 105 |
b=0 |
| 106 |
while [ "$b" -lt "$cube" ] |
| 107 |
do |
| 108 |
color=$(expr 16 + \( "$r" \* "$cube2" \) + \( "$g" \* "$cube" \) + "$b") |
| 109 |
tput setab "$color" |
| 110 |
printf ' ' |
| 111 |
b=$(expr "$b" + 1) |
| 112 |
done |
| 113 |
tput op |
| 114 |
printf ' ' |
| 115 |
r=$(expr "$r" + 1) |
| 116 |
done |
| 117 |
newline |
| 118 |
g=$(expr "$g" + 1) |
| 119 |
done |
| 120 |
|
| 121 |
printf "\n" |
| 122 |
printf "Grayscale ramp:\n" |
| 123 |
color=$ramp |
| 124 |
while [ "$color" -lt "$colors" ] |
| 125 |
do |
| 126 |
tput setab "$color" |
| 127 |
printf ' ' |
| 128 |
color=$(expr "$color" + 1) |
| 129 |
done |
| 130 |
newline |
| 131 |
# vi:ts=4 sw=4 |