1 ## Copyright (C) 2009 VZLU Prague, a.s.
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} colorboard (@var{m}, @var{palette}, @var{options})
18 ## Displays a color board corresponding to a numeric matrix @var{m}.
19 ## @var{m} should contain zero-based indices of colors.
20 ## The available range of indices is given by the @var{palette} argument,
21 ## which can be one of the following:
25 ## Black & white, using reverse video mode. This is the default if @var{m} is logical.
27 ## The standard ANSI 8 color palette. This is the default unless @var{m} is logical.
29 ## The AIXTerm extended 16-color palette. Uses codes 100:107 for bright colors.
31 ## The first 16 system colors of the Xterm 256-color palette.
33 ## The 6x6x6 color cube of the Xterm 256-color palette.
34 ## In this case, matrix can also be passed as a MxNx3 RGB array with values 0..5.
36 ## The 24 grayscale levels of the Xterm 256-color palette.
38 ## The full Xterm 256-color palette. The three above palettes together.
41 ## @var{options} comprises additional options. The recognized options are:
45 ## The number of spaces by which the board is indented. Default 2.
47 ## The number of spaces forming one field. Default 2.
48 ## @item "horizontalseparator"
49 ## The character used for horizontal separation of the table. Default "#".
50 ## @item "verticalseparator"
51 ## The character used for vertical separation of the table. Default "|".
55 function colorboard (m, palette, varargin)
60 nopt = length (varargin);
72 switch (tolower (varargin{nopt-1}))
74 indent = varargin{nopt};
77 case "verticalseparator"
78 vsep = varargin{nopt};
79 case "horizontalseparator"
80 hsep = varargin{nopt};
82 error ("unknown option: %s", varargin{nopt-1});
95 persistent digs = char (48:55); # digits 0..7
99 colors = ["07"; "27"];
102 colors = (["4"(i, 1), digs.']);
105 colors = (["04"(i, :), digs.'; "10"(i, :), digs.']);
107 colors = xterm_palette (0:15);
109 colors = xterm_palette (16:231);
110 if (size (m, 3) == 3)
111 m = (m(:,:,1)*6 + m(:,:,2))*6 + m(:,:,3);
114 colors = xterm_palette (232:255);
116 colors = xterm_palette (0:255);
118 error ("colorboard: invalid palette");
123 persistent esc = char (27);
124 escl = [esc, "["](ones (1, nc), :);
125 escr = ["m", blanks(spc)](ones (1, nc), :);
127 colors = [escl, colors, escr].';
131 if (isreal (m) && max (m(:)) <= 1)
132 m = min (floor (nc * m), nc-1);
136 board = reshape (colors(:, m + 1), [], rm, cm);
138 error ("colorboard: m is not a valid index into palette");
141 board = permute (board, [2, 1, 3])(:, :);
143 persistent reset = [esc, "[0m"];
145 indent = blanks (indent);
146 vline = [indent, hsep(1, ones (1, spc*cm+2))];
147 hlinel = [indent, vsep](ones (1, rm), :);
148 hliner = [reset, vsep](ones (1, rm), :);
150 oldpso = page_screen_output (0);
155 disp ([hlinel, board, hliner]);
159 puts (reset); # reset terminal
161 unwind_protect_cleanup
162 page_screen_output (oldpso);
167 function pal = xterm_palette (r)
169 fmt = "48;5;%02d"; l = 7;
171 fmt = "48;5;%03d"; l = 8;
173 pal = reshape (sprintf (fmt, r), l, length (r)).';