├── 24-bit-color.sh ├── README.md ├── bar-long.sh ├── bar-short.sh ├── colorspaces ├── terminalcolors.py └── xterm_color_chart.py /24-bit-color.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This file was originally sourced from https://github.com/JohnMorales/dotfiles/blob/master/colors/24-bit-color.sh 3 | # 4 | # Was modified by @bitcrazed to write '_' chars instead of spaces to support re-flow in Windows Console which sees 5 | # lines containing nothing but spaces as empty whitespace that can be deleted! 6 | # 7 | # This file echoes a bunch of 24-bit color codes 8 | # to the terminal to demonstrate its functionality. 9 | # The foreground escape sequence is ^[38;2;;;m 10 | # The background escape sequence is ^[48;2;;;m 11 | # range from 0 to 255 inclusive. 12 | # The escape sequence ^[0m returns output to default 13 | 14 | setBackgroundColor() 15 | { 16 | #printf '\x1bPtmux;\x1b\x1b[48;2;%s;%s;%sm' $1 $2 $3 17 | printf '\x1b[48;2;%s;%s;%sm' $1 $2 $3 18 | } 19 | 20 | resetOutput() 21 | { 22 | echo -en "\x1b[0m\n" 23 | } 24 | 25 | # Gives a color $1/255 % along HSV 26 | # Who knows what happens when $1 is outside 0-255 27 | # Echoes "$red $green $blue" where 28 | # $red $green and $blue are integers 29 | # ranging between 0 and 255 inclusive 30 | rainbowColor() 31 | { 32 | let h=$1/43 33 | let f=$1-43*$h 34 | let t=$f*255/43 35 | let q=255-t 36 | 37 | if [ $h -eq 0 ] 38 | then 39 | echo "255 $t 0" 40 | elif [ $h -eq 1 ] 41 | then 42 | echo "$q 255 0" 43 | elif [ $h -eq 2 ] 44 | then 45 | echo "0 255 $t" 46 | elif [ $h -eq 3 ] 47 | then 48 | echo "0 $q 255" 49 | elif [ $h -eq 4 ] 50 | then 51 | echo "$t 0 255" 52 | elif [ $h -eq 5 ] 53 | then 54 | echo "255 0 $q" 55 | else 56 | # execution should never reach here 57 | echo "0 0 0" 58 | fi 59 | } 60 | 61 | for i in `seq 0 127`; do 62 | setBackgroundColor $i 0 0 63 | echo -en "_" 64 | done 65 | 66 | resetOutput 67 | 68 | for i in `seq 255 -1 128`; do 69 | setBackgroundColor $i 0 0 70 | echo -en "_" 71 | done 72 | 73 | resetOutput 74 | 75 | for i in `seq 0 127`; do 76 | setBackgroundColor 0 $i 0 77 | echo -n "_" 78 | done 79 | 80 | resetOutput 81 | 82 | for i in `seq 255 -1 128`; do 83 | setBackgroundColor 0 $i 0 84 | echo -n "_" 85 | done 86 | 87 | resetOutput 88 | 89 | for i in `seq 0 127`; do 90 | setBackgroundColor 0 0 $i 91 | echo -n "_" 92 | done 93 | 94 | resetOutput 95 | 96 | for i in `seq 255 -1 128`; do 97 | setBackgroundColor 0 0 $i 98 | echo -n "_" 99 | done 100 | 101 | resetOutput 102 | 103 | for i in `seq 0 127`; do 104 | setBackgroundColor `rainbowColor $i` 105 | echo -n "_" 106 | done 107 | 108 | resetOutput 109 | 110 | for i in `seq 255 -1 128`; do 111 | setBackgroundColor `rainbowColor $i` 112 | echo -n "_" 113 | done 114 | resetOutput 115 | 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 24bit-color 2 | Selection of Bash/*NIX scripts for generating 24-bit color tests 3 | 4 | All scripts are (C) Copyright, original author (review file contents for details). 5 | -------------------------------------------------------------------------------- /bar-long.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk 'BEGIN{ 4 | s="/\\/\\/\\/\\/\\"; s=s s s s s s s s s s s s s s s s s s s s s s s; 5 | 6 | for (colnum = 0; colnum<256; colnum++) { 7 | r = 255-(colnum*255/255); 8 | g = (colnum*510/255); 9 | b = (colnum*255/255); 10 | if (g>255) g = 510-g; 11 | printf "\033[48;2;%d;%d;%dm", r,g,b; 12 | printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b; 13 | printf "%s\033[0m", substr(s,colnum+1,1); 14 | } 15 | printf "\n"; 16 | }' 17 | -------------------------------------------------------------------------------- /bar-short.sh: -------------------------------------------------------------------------------- 1 | awk 'BEGIN{ 2 | s="/\\/\\/\\/\\/\\"; s=s s s s s s s s; 3 | for (colnum = 0; colnum<77; colnum++) { 4 | r = 255-(colnum*255/76); 5 | g = (colnum*510/76); 6 | b = (colnum*255/76); 7 | if (g>255) g = 510-g; 8 | printf "\033[48;2;%d;%d;%dm", r,g,b; 9 | printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b; 10 | printf "%s\033[0m", substr(s,colnum+1,1); 11 | } 12 | printf "\n"; 13 | }' 14 | 15 | -------------------------------------------------------------------------------- /colorspaces: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # Author: Todd Larason 3 | # $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.1 1999/07/11 08:49:54 dawes Exp $ 4 | 5 | print "256 color mode\n\n"; 6 | 7 | # display back ground colors 8 | 9 | for ($fgbg = 38; $fgbg <= 48; $fgbg +=10) { 10 | 11 | # first the system ones: 12 | print "System colors:\n"; 13 | for ($color = 0; $color < 8; $color++) { 14 | print "\x1b[${fgbg};5;${color}m::"; 15 | } 16 | print "\x1b[0m\n"; 17 | for ($color = 8; $color < 16; $color++) { 18 | print "\x1b[${fgbg};5;${color}m::"; 19 | } 20 | print "\x1b[0m\n\n"; 21 | 22 | # now the color cube 23 | print "Color cube, 6x6x6:\n"; 24 | for ($green = 0; $green < 6; $green++) { 25 | for ($red = 0; $red < 6; $red++) { 26 | for ($blue = 0; $blue < 6; $blue++) { 27 | $color = 16 + ($red * 36) + ($green * 6) + $blue; 28 | print "\x1b[${fgbg};5;${color}m::"; 29 | } 30 | print "\x1b[0m "; 31 | } 32 | print "\n"; 33 | } 34 | 35 | # now the grayscale ramp 36 | print "Grayscale ramp:\n"; 37 | for ($color = 232; $color < 256; $color++) { 38 | print "\x1b[${fgbg};5;${color}m::"; 39 | } 40 | print "\x1b[0m\n\n"; 41 | 42 | } 43 | 44 | print "Examples for the 3-byte color mode\n\n"; 45 | 46 | for ($fgbg = 38; $fgbg <= 48; $fgbg +=10) { 47 | 48 | # now the color cube 49 | print "Color cube\n"; 50 | for ($green = 0; $green < 256; $green+=51) { 51 | for ($red = 0; $red < 256; $red+=51) { 52 | for ($blue = 0; $blue < 256; $blue+=51) { 53 | print "\x1b[${fgbg};2;${red};${green};${blue}m::"; 54 | } 55 | print "\x1b[0m "; 56 | } 57 | print "\n"; 58 | } 59 | 60 | # now the grayscale ramp 61 | print "Grayscale ramp:\n"; 62 | for ($gray = 8; $gray < 256; $gray+=10) { 63 | print "\x1b[${fgbg};2;${gray};${gray};${gray}m::"; 64 | } 65 | print "\x1b[0m\n\n"; 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /terminalcolors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright (C) 2006 by Johannes Zellner, 3 | # modified by mac@calmar.ws to fit my output needs 4 | # modified by crncosta@carloscosta.org to fit my output needs 5 | 6 | import sys 7 | import os 8 | 9 | def echo(msg): 10 | os.system('echo -n "' + str(msg) + '"') 11 | 12 | def out(n): 13 | os.system("tput setab " + str(n) + "; echo -n " + ("\"% 4d\"" % n)) 14 | os.system("tput setab 0") 15 | 16 | # normal colors 1 - 16 17 | os.system("tput setaf 16") 18 | for n in range(8): 19 | out(n) 20 | echo("\n") 21 | for n in range(8, 16): 22 | out(n) 23 | 24 | echo("\n") 25 | echo("\n") 26 | 27 | y=16 28 | while y < 231: 29 | for z in range(0,6): 30 | out(y) 31 | y += 1 32 | 33 | echo("\n") 34 | 35 | 36 | echo("\n") 37 | 38 | for n in range(232, 256): 39 | out(n) 40 | if n == 237 or n == 243 or n == 249: 41 | echo("\n") 42 | 43 | echo("\n") 44 | os.system("tput setaf 7") 45 | os.system("tput setab 0") 46 | 47 | -------------------------------------------------------------------------------- /xterm_color_chart.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | XTerm Colour Chart 5 | 6 | Ian Ward, 2007-2012 7 | This file is in the Public Domain, do with it as you wish. 8 | """ 9 | 10 | import sys 11 | from optparse import OptionParser 12 | 13 | __version__ = "2.1" 14 | 15 | # Colour charts 16 | # ------------- 17 | # Anm - colour cube colour where A is a letter between "a" and "f" and 18 | # n and m are numbers between 0 and 5. eg. "a00" is the one corner 19 | # of the cube and "f55" is the opposite corner. The first coordinate 20 | # is given as a letter to help distinguish the boundaries between 21 | # colours in the charts. In 88-colour mode only values "a" through 22 | # "d" and 0 through 3 are used. 23 | # .nn - basic colour where nn is between 00 and 15. 24 | # +nn - gray colour where nn is between 01 and 24 for 256-colour mode 25 | # or between 01 and 08 for 88-colour mode. 26 | 27 | whale_shape_left = """ 28 | e04d04c04b04 29 | e03d03c03b03 30 | e02d02c02b02 31 | e01d01c01b01 32 | e00d00c00b00a00a01a02a03a04a05b05c05d05e05f05f04f03f02f01f00 33 | e10d10c10b10a10a11a12a13a14a15b15c15d15e15f15f14f13f12f11f10 34 | e20d20c20b20a20a21a22a23a24a25b25c25d25e25f25f24f23f22f21f20 35 | e30d30c30b30a30a31a32a33a34a35b35c35d35e35f35f34f33f32f31f30 36 | e40d40c40b40a40a41a42a43a44a45b45c45d45e45f45f44f43f42f41f40 37 | e50d50c50b50a50a51a52a53a54a55b55c55d55e55f55f54f53f52f51f50 38 | b54c54d54e54 39 | b53c53d53e53 40 | .00.01.02.03.04.05.06.07 b52c52d52e52 41 | .08.09.10.11.12.13.14.15 b51c51d51e51 42 | """ 43 | 44 | whale_shape_right = """ 45 | d13c13 46 | d12c12 47 | d11c11b11b12b13b14c14d14e14e13e12e11 48 | d21c21b21b22b23b24c24d24e24e23e22e21 49 | d31c31b31b32b33b34c34d34e34e33e32e31 50 | d41c41b41b42b43b44c44d44e44e43e42e41 51 | c43d43 52 | c42d42 53 | c22c23d23d22 54 | c32c33d33d32 55 | 56 | 57 | +12+11+10+09+08+07+06+05+04+03+02+01 58 | +13+14+15+16+17+18+19+20+21+22+23+24 59 | """ 60 | # join left and right whales 61 | whale_shape = "\n".join([l.ljust(63)+r for l,r in 62 | zip(whale_shape_left.split("\n"), whale_shape_right.split("\n"))]) 63 | 64 | whale_shape_88 = """ 65 | c02b02 b11b12c12c11 66 | c01b01 b21b22c22c21 67 | c00b00a00a01a02a03b03c03d03d02d01d00 68 | c10b10a10a11a12a13b13c13d13d12d11d10 +08+07+06+05+04+03+02+01 69 | c20b20a20a21a22a23b23c23d23d22d21d20 70 | c30b30a30a31a32a33b33c33d33d32d31d30 71 | b32c32 .00.01.02.03.04.05.06.07 72 | b31c31 .08.09.10.11.12.13.14.15 73 | 74 | """ 75 | 76 | cloud_shape = """ 77 | .00.01.02.03.04.05.06.07 c12c13 d13d12 78 | .08.09.10.11.12.13.14.15 d11c11b11b12b13b14c14d14e14e13e12e11 79 | d21c21b21b22b23b24c24d24e24e23e22e21 80 | e31d31c31b31b32b33b34c34d34e34e33e32 81 | c22c23d23d22 e41d41c41b41b42b43b44c44d44e44e43e42 +01+24 82 | d32c32c33d33 d42c42 c43d43 +02+23 83 | +03+22 84 | c02c03 d03d02 +04+21 85 | d01c01 b01b02b03b04 c04d04 e04e03e02e01 +05+20 86 | e00d00c00b00a00a01a02a03a04a05b05c05d05e05f05f04f03f02f01f00 +06+19 87 | e10d10c10b10a10a11a12a13a14a15b15c15d15e15f15f14f13f12f11f10 +07+18 88 | e20d20c20b20a20a21a22a23a24a25b25c25d25e25f25f24f23f22f21f20 +08+17 89 | f30e30d30c30b30a30a31a32a33a34a35b35c35d35e35f35f34f33f32f31 +09+16 90 | f40e40d40c40b40a40a41a42a43a44a45b45c45d45e45f45f44f43f42f41 +10+15 91 | f50e50d50c50b50a50a51a52a53a54a55b55c55d55e55f55f54f53f52f51 +11+14 92 | e51d51c51b51 b52b53 b54c54d54e54 e53e52 +12+13 93 | d52c52 c53d53 94 | """ 95 | 96 | cloud_shape_88 = """ 97 | b11b12c12c11 98 | c21b21b22c22 b01b02 c02c01 99 | c00b00a00a01a02a03b03c03d03d02d01d00 100 | +08+07+06+05+04+03+02+01 c10b10a10a11a12a13b13c13d13d12d11d10 101 | d20c20b20a20a21a22a23b23c23d23d22d21 102 | .00.01.02.03.04.05.06.07 d30c30b30a30a31a32a33b33c33d33d32d31 103 | .08.09.10.11.12.13.14.15 c31b31 b32c32 104 | """ 105 | 106 | slices = """ 107 | a00a01a02a03a04a05 c05c04c03c02c01c00 e00e01e02e03e04e05 +01+24 .00.08 108 | a10a11a12a13a14a15 c15c14c13c12c11c10 e10e11e12e13e14e15 +02+23 .01.09 109 | a20a21a22a23a24a25 c25c24c23c22c21c20 e20e21e22e23e24e25 +03+22 .02.10 110 | a30a31a32a33a34a35 c35c34c33c32c31c30 e30e31e32e33e34e35 +04+21 .03.11 111 | a40a41a42a43a44a45 c45c44c43c42c41c40 e40e41e42e43e44e45 +05+20 .04.12 112 | a50a51a52a53a54a55 c55c54c53c52c51c50 e50e51e52e53e54e55 +06+19 .05.13 113 | b50b51b52b53b54b55 d55d54d53d52d51d50 f50f51f52f53f54f55 +07+18 .06.14 114 | b40b41b42b43b44b45 d45d44d43d42d41d40 f40f41f42f43f44f45 +08+17 .07.15 115 | b30b31b32b33b34b35 d35d34d33d32d31d30 f30f31f32f33f34f35 +09+16 116 | b20b21b22b23b24b25 d25d24d23d22d21d20 f20f21f22f23f24f25 +10+15 117 | b10b11b12b13b14b15 d15d14d13d12d11d10 f10f11f12f13f14f15 +11+14 118 | b00b01b02b03b04b05 d05d04d03d02d01d00 f00f01f02f03f04f05 +12+13 119 | """ 120 | 121 | 122 | slices_88 = """ 123 | a00a01a02a03 c03c02c01c00 +01 .00.08 124 | a10a11a12a13 c13c12c11c10 +02 .01.09 125 | a20a21a22a23 c23c22c21c20 +03 .02.10 126 | a30a31a32a33 c33c32c31c30 +04 .03.11 127 | b30b31b32b33 d33d32d31d30 +05 .04.12 128 | b20b21b22b23 d23d22d21d20 +06 .05.13 129 | b10b11b12b13 d13d12d11d10 +07 .06.14 130 | b00b01b02b03 d03d02d01d00 +08 .07.15 131 | """ 132 | 133 | 134 | ribbon_left = """ 135 | a00a01a02a03a04a05b05c05d05e05f05f04f03f02f01f00e00d00c00b00 136 | a10a11a12a13a14a15b15c15d15e15f15f14f13f12f11f10e10d10c10b10 137 | a20a21a22a23a24a25b25c25d25e25f25f24f23f22f21f20e20d20c20b20 138 | a30a31a32a33a34a35b35c35d35e35f35f34f33f32f31f30e30d30c30b30 139 | a40a41a42a43a44a45b45c45d45e45f45f44f43f42f41f40e40d40c40b40 140 | a50a51a52a53a54a55b55c55d55e55f55f54f53f52f51f50e50d50c50b50 141 | 142 | .00.01.02.03.04.05.06.07 +01+02+03+04+05+06+07+08+09+10+11 143 | .08.09.10.11.12.13.14.15 144 | """ 145 | 146 | ribbon_right = """ 147 | b01c01d01e01e02e03e04d04c04b04b03c03d03d02c02b02 148 | b11c11d11e11e12e13e14d14c14b14b13c13d13d12c12b12 149 | b21c21d21e21e22e23e24d24c24b24b23c23d23d22c22b22 150 | b31c31d31e31e32e33e34d34c34b34b33c33d33d32c32b32 151 | b41c41d41e41e42e43e44d44c44b44b43c43d43d42c42b42 152 | b51c51d51e51e52e53e54d54c54b54b53c53d53d52c52b52 153 | 154 | +12+13+14+15+16+17+18+19+20+21+22+23+24 155 | 156 | """ 157 | 158 | ribbon = "\n".join([l+r for l,r in 159 | zip(ribbon_left.split("\n"), ribbon_right.split("\n"))]) 160 | 161 | ribbon_88 = """ 162 | a00a01a02a03b03c03d03d02d01d00c00c01c02b02b01b00 163 | a10a11a12a13b13c13d13d12d11d10c10c11c12b12b11b10 164 | a20a21a22a23b23c23d23d22d21d20c20c21c22b22b21b20 165 | a30a31a32a33b33c33d33d32d31d30c30c31c32b32b31b30 166 | 167 | .00.01.02.03.04.05.06.07 +01+02+03+04+05+06+07+08 168 | .08.09.10.11.12.13.14.15 169 | """ 170 | 171 | cow_shape_left = """ 172 | +13+14+15+16+17+18+19+20+21+22+23+24 c01 e01 173 | +12+11+10+09+08+07+06+05+04+03+02+01 b02c02d02e02f02 174 | b03c03d03e03f03f13f23 175 | d01 b01 b04c04d04e04f04f14f24 176 | f01f00e00d00c00b00a00a01a02a03a04a05b05c05d05e05f05f15f25 177 | f12f11f10e10d10c10b10a10a11a12a13a14a15b15c15d15e15 178 | f32f22f21f20e20d20c20b20a20a21a22a23a24a25b25c25d25e25 179 | f42 f31f30e30d30c30b30a30a31a32a33a34a35b35c35d35e35f35 180 | f41f40e40d40c40b40a40a41a42a43a44a45b45c45d45e45f45 181 | f51f50e50d50c50b50a50a51a52a53a54a55b55c55d55e55f55 182 | f52 e51d51c51b51 b54c54 f54 183 | f53 e52d52c52b52 b53c53 f44 184 | f43 e53 d53 f34 185 | f33 e54 d54 186 | """ 187 | 188 | cow_shape_right = """ 189 | c23d23d22 190 | c32c33d33 191 | c22 d32 c12d12e12 192 | c13d13e13e23 193 | e11d11c11b11b12b13b14c14d14e14e24 194 | e22e21d21c21b21b22b23b24c24d24 195 | e32e31d31c31b31b32b33b34c34d34 196 | e33 d41c41b41 b44 d44 197 | e34 d42c42b42 c44 d43 198 | e44 e42 c43 e43 199 | e41 b43 200 | 201 | .00.01.02.03.04.05.06.07 202 | .08.09.10.11.12.13.14.15 203 | """ 204 | # join left and right cows 205 | cow_shape = "\n".join([l.ljust(66)+r for l,r in 206 | zip(cow_shape_left.split("\n"), cow_shape_right.split("\n"))]) 207 | 208 | cow_shape_88 = """ 209 | .00.01.02.03.04.05.06.07 b12c12c11 210 | .08.09.10.11.12.13.14.15 b21b22c22 211 | b11 c21 212 | +01+02+03+04+05+06+07+08 213 | b01c01d01 214 | b02c02d02d12 215 | d00c00b00a00a01a02a03b03c03d03d13 216 | d11d10c10b10a10a11a12a13b13c13 217 | d21d20c20b20a20a21a22a23b23c23 218 | d22 c30b30a30 a33 c33 219 | d23 c31b31a31 b33 c32 220 | d33 d31 b32 d32 221 | d30 a32 222 | """ 223 | 224 | charts = { 225 | 88: { 226 | 'cows': cow_shape_88, 227 | 'whales': whale_shape_88, 228 | 'slices': slices_88, 229 | 'ribbon': ribbon_88, 230 | 'clouds': cloud_shape_88,}, 231 | 256: { 232 | 'cows': cow_shape, 233 | 'whales': whale_shape, 234 | 'slices': slices, 235 | 'ribbon': ribbon, 236 | 'clouds': cloud_shape,}} 237 | 238 | # global settings 239 | 240 | basic_start = 0 # first index of basic colours 241 | cube_start = 16 # first index of colour cube 242 | cube_size = 6 # one side of the colour cube 243 | gray_start = cube_size ** 3 + cube_start 244 | colours = 256 245 | # values copied from xterm 256colres.h: 246 | cube_steps = 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff 247 | gray_steps = (0x08, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e, 0x58, 0x62, 248 | 0x6c, 0x76, 0x80, 0x84, 0x94, 0x9e, 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 249 | 0xda, 0xe4, 0xee) 250 | # values copied from X11/rgb.txt and XTerm-col.ad: 251 | basic_colours = ((0,0,0), (205, 0, 0), (0, 205, 0), (205, 205, 0), 252 | (0, 0, 238), (205, 0, 205), (0, 205, 205), (229, 229, 229), 253 | (127, 127, 127), (255, 0, 0), (0, 255, 0), (255, 255, 0), 254 | (0x5c, 0x5c, 0xff), (255, 0, 255), (0, 255, 255), (255, 255, 255)) 255 | 256 | def set_88_colour_mode(): 257 | """Switch to 88-colour mode.""" 258 | global cube_size, gray_start, colours, cube_steps, gray_steps 259 | cube_size = 4 260 | gray_start = cube_size ** 3 + cube_start 261 | colours = 88 262 | # values copied from xterm 88colres.h: 263 | cube_steps = 0x00, 0x8b, 0xcd, 0xff 264 | gray_steps = 0x2e, 0x5c, 0x73, 0x8b, 0xa2, 0xb9, 0xd0, 0xe7 265 | 266 | 267 | def error(e): 268 | """Report an error to the user.""" 269 | sys.stderr.write(e+"\n") 270 | 271 | def cube_vals(n): 272 | """Return the cube coordinates for colour-number n.""" 273 | assert n>=cube_start and n= gray_start: 299 | return "+%02d" % (n-gray_start+1) 300 | elif n >= cube_start: 301 | a, b, c = cube_vals(n) 302 | return "%s%s%s" % (chr(ord('a')+a), chr(ord('0')+b), chr(ord('0')+c)) 303 | else: 304 | return ".%02d not found" % (n-basic_start) 305 | 306 | def prt_to_n(prt): 307 | """Convert a colour chart cell to a colour number.""" 308 | assert len(prt)==3 309 | if prt == ' ': 310 | n = -1 311 | elif prt[0] == '.': 312 | val = int(prt[1:]) 313 | assert val>=0 and val=0 and val=0 and a=0 and b=0 and c=gray_start: 353 | return -1 354 | if n2=gray_start: 355 | return -1 356 | a1, b1, c1 = cube_vals(n1) 357 | a2, b2, c2 = cube_vals(n2) 358 | return abs(a1-a2)+abs(b1-b2)+abs(c1-c2) 359 | 360 | def parse_chart(chart): 361 | """Parse a colour chart passed in as a string.""" 362 | chart = chart.rstrip() 363 | found = set() 364 | 365 | oall = [] # the complete chart output 366 | for ln in chart.split('\n'): 367 | oln = [] # the current line of output 368 | ln = ln.rstrip() 369 | if not oall and not ln: 370 | # remove blank lines from top of chart 371 | continue 372 | 373 | for loff in range(0, len(ln), 3): 374 | prt = ln[loff:loff+3] 375 | if not prt: 376 | continue 377 | n = prt_to_n(prt) 378 | if n>=0 and n in found: 379 | error("duplicate entry %s found" % prt) 380 | found.add(n) 381 | if oall and len(oall[-1])>len(oln): # compare distance above 382 | nabove = oall[-1][len(oln)] 383 | if distance(nabove, n)>1: 384 | error("entry %s found above %s" % (n_to_prt(nabove), prt)) 385 | if oln: # compare distance to left 386 | nleft = oln[-1] 387 | if distance(nleft, n)>1: 388 | error("entry %s found left of %s" % (n_to_prt(nleft), prt)) 389 | oln.append(n) 390 | oall.append(oln) 391 | 392 | # make sure all colours were included in the chart 393 | for n in range(colours): 394 | if n in found: 395 | continue 396 | error("entry %s not found" % n_to_prt(n)) 397 | 398 | return oall 399 | 400 | 401 | def draw_chart(chart, origin, angle, hexadecimal, decimal, urwidmal, cell_cols, 402 | cell_rows): 403 | """Draw a colour chart on the screen. 404 | 405 | chart -- chart data parsed by parse_chart() 406 | origin -- 0..7 origin of colour cube 407 | angle -- 0..5 rotation angle of colour cube 408 | hexadecimal -- if True display hex palette numbers on the chart 409 | decimal -- if True display decimal palette numbers on the chart 410 | urwidmal -- if True display urwid palette colour on the chart 411 | cell_cols -- number of screen columns per cell 412 | cell_rows -- number of screen rows per cell 413 | """ 414 | amap = [(0,1,2), (1,2,0), (2,0,1), (0,2,1), (1,0,2), (2,1,0)][angle] 415 | omap = [(1,1,1), (1,1,-1), (1,-1,-1), (1,-1,1), 416 | (-1,-1,1), (-1,-1,-1), (-1,1,-1), (-1,1,1)][origin] 417 | 418 | if hexadecimal and cell_cols < 2: 419 | cell_cols = 2 420 | elif decimal and cell_cols < 3: 421 | cell_cols = 3 422 | elif urwidmal and cell_cols < 4: 423 | cell_cols = 4 424 | cell_pad = " "*cell_cols 425 | 426 | def transform_block(n, row): 427 | v = cube_vals(n) 428 | v = [(int(om/2) + om * n) % cube_size for n, om in zip(v, omap)] 429 | r, g, b = v[amap[0]], v[amap[1]], v[amap[2]] 430 | vtrans = (r*cube_size + g)*cube_size + b + cube_start 431 | return block(vtrans, row) 432 | 433 | def block(n, row): 434 | if not any((hexadecimal, decimal, urwidmal)) or row!=cell_rows-1: 435 | return "\x1b[48;5;%dm%s" % (n, cell_pad) 436 | y = n_to_gray(n) 437 | if y>0x30: 438 | # use black text 439 | if hexadecimal: 440 | return "\x1b[48;5;%d;30m%02x%s" % (n, n, cell_pad[2:]) 441 | elif decimal: 442 | return "\x1b[48;5;%d;30m%03d%s" % (n, n, cell_pad[3:]) 443 | elif urwidmal: 444 | return "\x1b[48;5;%d;30m%4s%s" % (n, urwidify(n), cell_pad[4:]) 445 | # else use gray text 446 | if hexadecimal: 447 | return "\x1b[48;5;%d;37m%02x%s" % (n, n, cell_pad[2:]) 448 | elif decimal: 449 | return "\x1b[48;5;%d;37m%03d%s" % (n, n, cell_pad[3:]) 450 | elif urwidmal: 451 | return "\x1b[48;5;%d;37m%4s%s" % (n, urwidify(n), cell_pad[4:]) 452 | 453 | def blank(): 454 | return "\x1b[0m%s" % (cell_pad,) 455 | 456 | for ln in chart: 457 | for row in range(cell_rows): 458 | out = [] 459 | for n in ln: 460 | if n<0: 461 | out.append(blank()) 462 | elif n7: 516 | error("Invalid origin value specified!") 517 | sys.exit(2) 518 | if options.angle<0 or options.angle>5: 519 | error("Invalid angle value specified!") 520 | sys.exit(2) 521 | if options.columns < 1: 522 | error("Invalid number of columns specified!") 523 | if options.rows < 1: 524 | error("Invalid number of rows specified!") 525 | if options.colours_88: 526 | set_88_colour_mode() 527 | if options.list_charts: 528 | print "Charts available in %d-colour mode:" % colours 529 | for cname in charts[colours].keys(): 530 | print " "+cname 531 | sys.exit(0) 532 | if options.reset_palette: 533 | reset_palette() 534 | 535 | if not args: 536 | args = ["whales"] # default chart 537 | first = True 538 | for cname in args: 539 | if not first: 540 | print 541 | first = False 542 | if cname not in charts[colours]: 543 | error("Chart %r not found!" % cname) 544 | continue 545 | chart = parse_chart(charts[colours][cname]) 546 | draw_chart(chart, options.origin, options.angle, options.hexadecimal, 547 | options.decimal, options.urwidmal, options.columns, options.rows) 548 | 549 | 550 | 551 | if __name__ == '__main__': 552 | main() 553 | 554 | --------------------------------------------------------------------------------