• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2##############################################################################
3# Copyright 2020 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.2 2020/02/02 23:34:34 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
34failed() {
35	printf "?? $*\n" >&2
36	exit 1
37}
38
39newline() {
40	tput op
41	printf "\n"
42}
43
44if [ $# = 1 ]
45then
46	myterm=$1
47elif [ $# = 0 ]
48then
49	myterm=$TERM
50else
51	failed "expected one parameter or none"
52fi
53
54colors=$(tput -T $myterm colors 2>/dev/null)
55if [ ${colors:-0} -le 0 ]
56then
57	myterm=${myterm%%-color}
58	colors=$(tput -T $myterm colors 2>/dev/null)
59fi
60if [ ${colors:-0} -le 0 ]
61then
62	failed "terminal $myterm does not support color"
63fi
64
65printf "System colors:\n"
66
67color=0
68inrow=$colors
69to_do=$colors
70[ $colors -gt 256 ] && colors=256
71[ $inrow  -gt   8 ] && inrow=8
72[ $to_do  -gt  16 ] && to_do=16
73while [ $color -lt $to_do ]
74do
75	[ $color = $inrow ] && newline
76	tput setab $color
77	printf '  '
78	color=$(expr $color + 1)
79done
80newline
81
82[ $colors -le 16 ] && exit
83
84if [ $colors = 256 ]
85then
86	cube=6
87	ramp=232
88elif [ $colors -ge 88 ]
89then
90	cube=4
91	ramp=80
92else
93	exit
94fi
95
96printf "\n"
97printf "Color cube, ${cube}x${cube}x${cube}:\n"
98g=0
99cube2=$(expr $cube \* $cube)
100while [ $g -lt $cube ]
101do
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)
119done
120
121printf "\n"
122printf "Grayscale ramp:\n"
123color=$ramp
124while [ $color -lt $colors ]
125do
126	tput setab $color
127	printf '  '
128	color=$(expr $color + 1)
129done
130newline
131# vi:ts=4 sw=4
132