Improve color handling

- document style bits in 16, 32 and 64-bit style models
- add `color_y(QEColor)` to compute the weighted light value
- publish `color_dist()`
- add `colors_init()` to initialize color support structures
- improve list of named colors:
  * use CSS color names
  * add X11 color names
  * make grey a generic synonym for gray
- improve `qe_color_map` using ramp lookup arrays
- fix `qe_unmap_color()` bug on 4K color cube
- add custom palette to store extra named user defined colors
- improve `css_lookup_color()` and `css_get_color()`
- enumerate palette entries on `p` and `#`
- improve color scripts in `tests/`
- add `tests/getpalette.sh` to dump the current terminal palette colors
- add `tests/term-palette.sh` to compare palette colors and rgb colors
This commit is contained in:
Charlie Gordon 2024-09-07 17:37:35 +02:00
commit 9f2d282aa3
12 changed files with 1831 additions and 596 deletions

1540
color.c

File diff suppressed because it is too large Load diff

71
color.h
View file

@ -2,7 +2,7 @@
* Color / CSS Utilities for qemacs.
*
* Copyright (c) 2000-2001 Fabrice Bellard.
* Copyright (c) 2000-2023 Charlie Gordon.
* Copyright (c) 2000-2024 Charlie Gordon.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -39,7 +39,7 @@ typedef int (CSSAbortFunc)(void *);
#define CSS_MEDIA_ALL 0xffff
typedef unsigned int QEColor;
typedef uint32_t QEColor;
#define QEARGB(a,r,g,b) (((unsigned int)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
#define QERGB(r,g,b) QEARGB(0xff, r, g, b)
#define QERGB25(r,g,b) QEARGB(1, r, g, b)
@ -55,16 +55,14 @@ typedef unsigned int QEColor;
* - font style size
*
* Styles are applied to text in successive phases:
* - the current syntax mode computes style numbers for each character
* on the line. These nubers are stored into the high bits of the
* 32-bit code point.
* - these style numbers are then extracted into an array of 32-bit composite
* - the current syntax mode computes the composite style for each
* character displayed on the line. These styles are stored into the
* array `sbuf` passed to the `ColorizeFunc` of the mode structure.
* - the styles from the optional style buffer are combined into these
* style values.
* - the styles from the optional style buffer are combined into these style
* values.
* - search matches and selection styles are applied if relevant.
* - the bidirectional algorithm is applied to compute the display order
* - sequences of code-point with the same compsite style are formed into
* - sequences of codepoints with the same composite style are formed into
* display units, ligatures are applied.
* - the composite style is expanded and converted into display attributes
* to trace the display unit on the device surface
@ -78,12 +76,17 @@ typedef unsigned int QEColor;
*/
#if 0 /* 25-bit color for FG and BG */
/* 64-bit style layout:
* x: unused, b: background color, f: foreground color,
* K: blink, I: italics, B: bold, U: underline, C: composite,
* S: selection, N: no-selection
* xxxxxxxb bbbbbbbb bbbbbbbb bbbbbbbb NKIBUCSf ffffffff ffffffff ffffffff
*/
#define QE_TERM_STYLE_BITS 64
typedef uint64_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x02000000 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x04000000 /* special bit to indicate qe-term composite style */
#define QE_TERM_COMPOSITE 0x04000000 /* style is composite, not just a style number */
/* XXX: reversed as attribute? */
/* XXX: faint? */
#define QE_TERM_UNDERLINE 0x08000000
@ -96,12 +99,16 @@ typedef uint64_t QETermStyle;
#define QE_TERM_FG_SHIFT 0
#elif 1 /* 8K colors for FG and BG */
/* 32-bit style layout:
* x: unused, b: background color, f: foreground color,
* K: blink, I: italics, B: bold, U: underline, C: composite, S: selection
* bbbbbbbb bbbbbKIB UCSfffff ffffffff
*/
#define QE_TERM_STYLE_BITS 32
typedef uint32_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x02000 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x04000 /* special bit to indicate qe-term composite style */
#define QE_TERM_COMPOSITE 0x04000 /* style is composite, not just a style number */
#define QE_TERM_UNDERLINE 0x08000
#define QE_TERM_BOLD 0x10000
#define QE_TERM_ITALIC 0x20000
@ -112,12 +119,16 @@ typedef uint32_t QETermStyle;
#define QE_TERM_FG_SHIFT 0
#elif 1 /* 256 colors for FG and BG */
/* 32-bit style layout for 256-color terminals:
* x: unused, b: background color, f: foreground color,
* K: blink, I: italics, B: bold, U: underline, C: composite, S: selection
* xxxxxxxx bbbbbbbb xxKIBUCS ffffffff
*/
#define QE_TERM_STYLE_BITS 32
typedef uint32_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x0100 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x0200 /* special bit to indicate qe-term composite style */
#define QE_TERM_COMPOSITE 0x0200 /* style is composite, not just a style number */
#define QE_TERM_UNDERLINE 0x0400
#define QE_TERM_BOLD 0x0800
#define QE_TERM_ITALIC 0x1000
@ -128,12 +139,16 @@ typedef uint32_t QETermStyle;
#define QE_TERM_FG_SHIFT 0
#else /* 16 colors for FG and 16 color BG */
/* 16-bit style layout:
* x: unused, b: background color, f: foreground color,
* K: blink, I: italics, B: bold, U: underline, C: composite, S: selection
* xxKIBUCS bbbbffff
*/
#define QE_TERM_STYLE_BITS 16
typedef uint16_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x0100 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x0200 /* special bit to indicate qe-term composite style */
#define QE_TERM_COMPOSITE 0x0200 /* style is composite, not just a style number */
#define QE_TERM_UNDERLINE 0x0400
#define QE_TERM_BOLD 0x0800
#define QE_TERM_ITALIC 0x1000
@ -154,12 +169,12 @@ typedef uint16_t QETermStyle;
#define QE_TERM_MAKE_COLOR(fg, bg) (((QETermStyle)(fg) << QE_TERM_FG_SHIFT) | ((QETermStyle)(bg) << QE_TERM_BG_SHIFT))
#define QE_TERM_SET_FG(col, fg) ((col) = ((col) & ~QE_TERM_FG_MASK) | ((QETermStyle)(fg) << QE_TERM_FG_SHIFT))
#define QE_TERM_SET_BG(col, bg) ((col) = ((col) & ~QE_TERM_BG_MASK) | ((QETermStyle)(bg) << QE_TERM_BG_SHIFT))
#define QE_TERM_GET_FG(color) (((color) & QE_TERM_FG_MASK) >> QE_TERM_FG_SHIFT)
#define QE_TERM_GET_BG(color) (((color) & QE_TERM_BG_MASK) >> QE_TERM_BG_SHIFT)
#define QE_TERM_GET_FG(color) (int)(((color) & QE_TERM_FG_MASK) >> QE_TERM_FG_SHIFT)
#define QE_TERM_GET_BG(color) (int)(((color) & QE_TERM_BG_MASK) >> QE_TERM_BG_SHIFT)
typedef struct ColorDef {
const char *name;
unsigned int color;
const char *name; /* color name */
QEColor color; /* 32-bit ARGB color */
} ColorDef;
extern QEColor const xterm_colors[];
@ -169,7 +184,7 @@ extern int nb_qe_colors;
/* XXX: should have a more generic API with precomputed mapping scales */
/* Convert RGB triplet to a composite color */
unsigned int qe_map_color(QEColor color, QEColor const *colors, int count, int *dist);
/* Convert a composite color to an RGB triplet */
/* Convert a composite color to an RGBA triplet with alpha channel */
QEColor qe_unmap_color(int color, int count);
int css_define_color(const char *name, const char *value);
@ -177,6 +192,10 @@ int css_get_color(QEColor *color_ptr, const char *p);
void css_free_colors(void);
int css_get_font_family(const char *str);
int css_get_enum(const char *str, const char *enum_str);
int color_dist(QEColor c1, QEColor c2);
static inline int color_y(QEColor c) {
return 30 * ((c >> 16) & 0xff) + 59 * ((c >> 8) & 0xff) + 11 * (c & 0xff);
}
typedef struct CSSRect {
int x1, y1, x2, y2;
@ -194,12 +213,12 @@ static inline void css_set_rect(CSSRect *a, int x1, int y1, int x2, int y2) {
}
/* return true if a and b intersect */
static inline int css_is_inter_rect(const CSSRect *a, const CSSRect *b) {
return (!(a->x2 <= b->x1 ||
a->x1 >= b->x2 ||
a->y2 <= b->y1 ||
a->y1 >= b->y2));
return (!(a->x2 <= b->x1 || a->x1 >= b->x2 ||
a->y2 <= b->y1 || a->y1 >= b->y2));
}
int colors_init(void);
/*---- Font definitions ----*/
#define QE_FONT_STYLE_NORM 0x0001

View file

@ -2521,7 +2521,7 @@ int color_print_entry(CompleteState *cp, EditState *s, const char *name) {
s->b->cur_style = style2;
len += eb_puts(s->b, "[ Sample ]");
s->b->cur_style = QE_STYLE_DEFAULT;
len2 = eb_printf(s->b, " %-4d", bg);
len2 = eb_printf(s->b, " p%-4d", bg);
if (dist != 0)
len2 += eb_printf(s->b, " dist=%-4d", dist);
if (color != rgb)

11
qe.c
View file

@ -703,10 +703,9 @@ void do_cd(EditState *s, const char *path)
static void color_complete(CompleteState *cp, CompleteFunc enumerate) {
const char *name = cp->current;
int i;
if (*name == '#') {
int i;
for (i = 0; i < 8192; i++) {
char buf[32];
QEColor rgb = qe_unmap_color(i, 8192);
@ -718,7 +717,6 @@ static void color_complete(CompleteState *cp, CompleteFunc enumerate) {
#if 0
if (name[0] == 'g' && name[1] == 'r' && (name[2] == 'a' || name[2] == 'e') && name[3] == 'y') {
char buf[32];
int i;
snprintf(buf, sizeof buf, "%.4s", name);
enumerate(cp, buf, CT_GLOB);
for (i = 0; i < 100; i++) {
@ -735,6 +733,13 @@ static void color_complete(CompleteState *cp, CompleteFunc enumerate) {
def++;
count--;
}
if (name[0] == 'p' && !qe_isalpha(name[1])) {
for (i = 0; i < 8192; i++) {
char buf[32];
snprintf(buf, sizeof buf, "p%d", i);
enumerate(cp, buf, CT_GLOB);
}
}
}
}

View file

@ -6,7 +6,7 @@
# reproduction of the standard ANSI colors, but possibly more
# pleasing shades
# display the colors
# display the colors using CSI 48;5;palette number m
# first the system ones:
print "Background colors:\n";

View file

@ -6,7 +6,7 @@
# reproduction of the standard ANSI colors, but possibly more
# pleasing shades
# display the colors
# display the colors using CSI 3x/4x/9x/10x m sequences
# first the system ones:
print "Background colors:\n";

View file

@ -6,58 +6,146 @@
# reproduction of the standard ANSI colors, but possibly more
# pleasing shades
# colors 16-231 are a 6x6x6 color cube
for ($red = 0; $red < 6; $red++) {
for ($green = 0; $green < 6; $green++) {
for ($blue = 0; $blue < 6; $blue++) {
printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
16 + ($red * 36) + ($green * 6) + $blue,
($red ? ($red * 40 + 55) : 0),
($green ? ($green * 40 + 55) : 0),
($blue ? ($blue * 40 + 55) : 0));
}
# rgb cube axis is 40*comp + 55 except for 0
@rgbaxis = (0,95,135,175,215,255);
# gray scale intentionally leaves out black and white
@grayaxis = (8,18,28,38,48,58,68,78,88,98,108,118,128,138,148,158,168,178,188,198,208,218,228,238,248);
if (false) {
# redefine color 16 to 255
# colors 16-231 are a 6x6x6 color cube
$n = 16;
for ($red = 0; $red < 6; $red++) {
for ($green = 0; $green < 6; $green++) {
for ($blue = 0; $blue < 6; $blue++, $n++) {
printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
$n, $rgbaxis[$red], $rgbaxis[$green], $rgbaxis[$blue]);
}
}
}
# colors 232-255 are a grayscale ramp, intentionally leaving out
# black and white
$n = 232;
for ($gray = 0; $gray < 24; $gray++, $n++) {
$level = ($gray * 10) + 8;
printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
$n, $level, $level, $level);
}
}
# colors 232-255 are a grayscale ramp, intentionally leaving out
# black and white
for ($gray = 0; $gray < 24; $gray++) {
$level = ($gray * 10) + 8;
printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
232 + $gray, $level, $level, $level);
}
# display the colors
$height = 2;
# first the system ones:
print "System colors:\n";
for ($color = 0; $color < 8; $color++) {
print "\x1b[48;5;${color}m ";
if ($height != 2) {
for ($color = 0; $color < 8; $color++) {
printf(" %02d ", $color);
}
print "\n";
}
print "\x1b[0m\n";
for ($color = 8; $color < 16; $color++) {
print "\x1b[48;5;${color}m ";
for ($i = 0; $i < $height; $i++) {
for ($color = 0; $color < 8; $color++) {
$textcolor = ($color == 0 || $color == 8) ? 7 : 0;
if ($i + 2 == $height) {
printf("\x1b[38;5;${color}m %02d ", $color);
} else {
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm %02d ", $color, $color);
}
}
print "\x1b[0m\n";
}
print "\x1b[0m\n\n";
if ($height != 2) {
for ($color = 8; $color < 16; $color++) {
printf(" %02d ", $color);
}
print "\n";
}
for ($i = 0; $i < $height; $i++) {
for ($color = 8; $color < 16; $color++) {
$textcolor = ($color == 0 || $color == 8) ? 7 : 0;
if ($i + 2 == $height) {
printf("\x1b[38;5;${color}m %02d ", $color);
} else {
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm %02d ", $color, $color);
}
}
print "\x1b[0m\n";
}
print "\n";
# now the color cube
print "Color cube, 6x6x6:\n";
for ($green = 0; $green < 6; $green++) {
for ($red = 0; $red < 6; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
print "\x1b[48;5;${color}m ";
}
print "\x1b[0m ";
for ($i = 0; $i < $height; $i++) {
for ($red = 0; $red < 3; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
$y = 20 * $rgbaxis[$red] + 59 * $rgbaxis[$green] + 11 * $rgbaxis[$blue];
$textcolor = $y >= 12800 ? 0 : 15;
if ($i + 2 == $height) {
printf("\x1b[38;5;%dm%4d", $color, $color);
} else {
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm%4d", $color, $color);
}
}
print "\x1b[0m ";
}
print "\n";
}
print "\n";
}
print "\n";
for ($green = 0; $green < 6; $green++) {
for ($i = 0; $i < $height; $i++) {
for ($red = 3; $red < 6; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
#$textcolor = 16 + (($red + 3) % 6 * 36) + (($green + 3) % 6 * 6) + ($blue + 3) % 6;
$y = 20 * $rgbaxis[$red] + 59 * $rgbaxis[$green] + 11 * $rgbaxis[$blue];
$textcolor = $y >= 12800 ? 0 : 15;
if ($i + 2 == $height) {
printf("\x1b[38;5;%dm%4d", $color, $color);
} else {
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm%4d", $color, $color);
}
}
print "\x1b[0m";
if ($red < 5) { print " "; }
}
print "\n";
}
}
print "\n";
# now the grayscale ramp
print "Grayscale ramp:\n";
for ($color = 232; $color < 256; $color++) {
print "\x1b[48;5;${color}m ";
for ($i = 0; $i < $height; $i++) {
for ($color = 232; $color < 244; $color++) {
if ($i + 2 == $height) {
printf("\x1b[38;5;${color}m%4d", $color);
} else {
$y = 100 * $grayaxis[$color - 232];
$textcolor = $y >= 12800 ? 0 : 15;
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm%4d", $color, $color);
}
}
print "\x1b[0m\n";
}
for ($i = 0; $i < $height; $i++) {
for ($color = 244; $color < 256; $color++) {
if ($i + 2 == $height) {
printf("\x1b[38;5;${color}m%4d", $color);
} else {
$y = 100 * $grayaxis[$color - 232];
$textcolor = $y >= 12800 ? 0 : 15;
printf("\x1b[38;5;%dm", $textcolor);
printf("\x1b[48;5;%dm%4d", $color, $color);
}
}
print "\x1b[0m\n";
}
print "\x1b[0m\n";

View file

@ -11,6 +11,8 @@
#export out=""
check="$1"
output() {
echo -ne "$1"
#export out="$out$1"
@ -25,7 +27,10 @@ setBackgroundColor() {
# printf '\x1bPtmux;\x1b\x1b[48;2;%s;%s;%sm' $1 $2 $3
# printf '\x1b[48;2;%s;%s;%sm%s' $1 $2 $3 "$4"
#echo -ne "\033[48;2;$1;$2;$3m$4"
output "\033[48;2;$1;$2;$3m$4"
let rr="$1"
let gg="$2"
let bb="$3"
output "\033[48;2;${rr};${gg};${bb}m$4"
}
resetColor() {
@ -69,6 +74,28 @@ rainbowColor() {
fi
}
paletteRamp() {
let end=$1+$2-1
for i in `seq $1 $end`; do
output "\033[48;5;${i}m$3"
done
resetColor $'\n';
}
paletteEmulate() {
let end=$1+$2-1
for i in `seq $1 $end`; do
let rr6="$i/36%6"
let gg6="$i/6%6"
let bb6="$i%6"
let rr="$rr6?$rr6*40+55:0"
let gg="$gg6?$gg6*40+55:0"
let bb="$bb6?$bb6*40+55:0"
output "\033[48;2;${rr};${gg};${bb}m$3"
done
resetColor $'\n';
}
colorRamp() {
for i in `seq 0 255`; do
if [ $i -lt 64 ] ; then let v=$i ;
@ -141,24 +168,87 @@ rainbowRamp() {
resetColor $'\n'
}
output $'\n'
output $'Color cube 4x4x4:\n';
for i in `seq 0 3`; do
for g in `seq 0 15`; do
for r in `seq 0 3`; do
for b in `seq 0 15`; do
let r1=$r+$i*4
let rr=$r1*17
let gg=$g*17
let bb=$b*17
setBackgroundColor $rr $gg $bb $' '
colorCube4x4x4() {
for i in `seq 0 3`; do
for g in `seq 0 15`; do
for r in `seq 0 3`; do
for b in `seq 0 15`; do
let r1=$r+$i*4
let rr=$r1*17
let gg=$g*17
let bb=$b*17
setBackgroundColor $rr $gg $bb "$1"
done
resetColor $' '
done
resetColor $' '
resetColor $'\n'
done
resetColor $'\n'
done
resetColor $'\n'
done
}
xtermPalette() {
paletteRamp 0 16 $' '
if [ "$check" == "xxx" ] ; then
setBackgroundColor 0x00 0x00 0x00 $' '
setBackgroundColor 0xbb 0x00 0x00 $' '
setBackgroundColor 0x00 0xbb 0x00 $' '
setBackgroundColor 0xbb 0xbb 0x00 $' '
setBackgroundColor 0x00 0x00 0xbb $' '
setBackgroundColor 0xbb 0x00 0xbb $' '
setBackgroundColor 0x00 0xbb 0xbb $' '
setBackgroundColor 0xbb 0xbb 0xbb $' '
setBackgroundColor 0x55 0x55 0x55 $' '
setBackgroundColor 0xff 0x55 0x55 $' '
setBackgroundColor 0x55 0xff 0x55 $' '
setBackgroundColor 0xff 0xff 0x55 $' '
setBackgroundColor 0x55 0x55 0xff $' '
setBackgroundColor 0xff 0x55 0xff $' '
setBackgroundColor 0x55 0xff 0xff $' '
setBackgroundColor 0xff 0xff 0xff $' '
resetColor $'\n'
fi
if [ "$check" == "check" ] ; then
setBackgroundColor 0x00 0x00 0x00 $' ' Black
setBackgroundColor 0x80 0x00 0x00 $' ' Maroon
setBackgroundColor 0x00 0x80 0x00 $' ' Green
setBackgroundColor 0x80 0x80 0x00 $' ' Olive
setBackgroundColor 0x00 0x00 0x80 $' ' Navy
setBackgroundColor 0x80 0x00 0x80 $' ' Purple
setBackgroundColor 0x00 0x80 0x80 $' ' Teal
setBackgroundColor 0xc0 0xc0 0xc0 $' ' Silver
setBackgroundColor 0x80 0x80 0x80 $' ' Grey
setBackgroundColor 0xff 0x00 0x00 $' ' Red
setBackgroundColor 0x00 0xff 0x00 $' ' Lime
setBackgroundColor 0xff 0xff 0x00 $' ' Yellow
setBackgroundColor 0x00 0x00 0xff $' ' Blue
setBackgroundColor 0xff 0x00 0xff $' ' Fuchsia
setBackgroundColor 0x00 0xff 0xff $' ' Aqua
setBackgroundColor 0xff 0xff 0xff $' ' White
resetColor $'\n'
fi
for i in `seq 0 5`; do
let start=16+$i*36
let startE=$i*36
paletteRamp $start 36 $' '
if [ "$check" == "check" ] ; then
paletteEmulate $startE 36 $' '
fi
done
paletteRamp 232 24 $' '
if [ "$check" == "check" ] ; then
for i in 0x08 0x12 0x1c 0x26 0x30 0x3a 0x44 0x4e \
0x58 0x62 0x6c 0x76 0x80 0x8a 0x94 0x9e \
0xa8 0xb2 0xbc 0xc6 0xd0 0xda 0xe4 0xee ; do
setBackgroundColor $i $i $i $' '
done
resetColor $'\n'
fi
}
output $'\n'
output $'Color cube 4x4x4:\n';
colorCube4x4x4 $' '
output $'\n'
output $'Color ramps: fade to black\n';
@ -193,14 +283,6 @@ rainbowRamp $' '
output $'\n'
output $'xterm palette:\n';
{
for i in `seq 0 127`; do
output "\033[48;5;${i}m "
done
resetColor $'\n'
for i in `seq 128 255`; do
output "\033[48;5;${i}m "
done
resetColor $'\n'
}
xtermPalette
flush;

View file

@ -2,81 +2,81 @@
# Author: Todd Larason <jtl@molehill.org>
# $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.1 1999/07/11 08:49:54 dawes Exp $
print "256 color mode\n\n";
$s = " ";
print "Compare direct 256-color palette and RGB color modes\n\n";
# display foreground and background colors
for ($fgbg = 38; $fgbg <= 48; $fgbg +=10) {
for ($fgbg = 48; $fgbg <= 48; $fgbg += 10) {
# first the system ones:
print "System colors:\n";
print "Palette: ";
for ($color = 0; $color < 8; $color++) {
print "\x1b[${fgbg};5;${color}m::";
print "\x1b[${fgbg};5;${color}m${s}";
}
print "\x1b[0m\n";
print "\x1b[0m ";
for ($color = 8; $color < 16; $color++) {
print "\x1b[${fgbg};5;${color}m::";
print "\x1b[${fgbg};5;${color}m${s}";
}
print "\x1b[0m\n\n";
# now the color cube
print "Color cube, 6x6x6:\n";
for ($green = 0; $green < 6; $green++) {
for ($red = 0; $red < 6; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
print "\x1b[${fgbg};5;${color}m::";
if (0) {
# now the color cube
print "Color cube, 6x6x6:\n";
for ($green = 0; $green < 6; $green++) {
for ($red = 0; $red < 6; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
print "\x1b[${fgbg};5;${color}m${s}";
}
print "\x1b[0m ";
}
print "\x1b[0m ";
print "\n";
}
print "\n";
# now the grayscale ramp
print "Grayscale ramp:\n";
print "\x1b[${fgbg};5;16m${s}";
for ($color = 232; $color < 256; $color++) {
print "\x1b[${fgbg};5;${color}m${s}";
}
print "\x1b[${fgbg};5;231m${s}";
print "\x1b[0m\n\n";
}
# now the grayscale ramp
print "Grayscale ramp:\n";
print "\x1b[${fgbg};5;16m::";
for ($color = 232; $color < 256; $color++) {
print "\x1b[${fgbg};5;${color}m::";
}
print "\x1b[${fgbg};5;231m::";
print "\x1b[0m\n\n";
}
if (0) {
print "Examples for the 3-byte color mode\n\n";
print "Examples for the 3-byte color mode\n\n";
for ($fgbg = 38; $fgbg <= 48; $fgbg +=10) {
# now the color cube
print "Color cube\n";
for ($green = 0; $green < 256; $green += 51) {
for ($red = 0; $red < 256; $red += 51) {
for ($blue = 0; $blue < 256; $blue += 51) {
print "\x1b[${fgbg};2;${red};${green};${blue}m::";
for ($fgbg = 38; $fgbg <= 48; $fgbg += 10) {
# now the color cube
print "Color cube\n";
for ($green = 0; $green < 256; $green += 51) {
for ($red = 0; $red < 256; $red += 51) {
for ($blue = 0; $blue < 256; $blue += 51) {
print "\x1b[${fgbg};2;${red};${green};${blue}m${s}";
}
print "\x1b[0m ";
}
print "\x1b[0m ";
print "\n";
}
print "\n";
}
# now the grayscale ramp
print "Grayscale ramp:\n";
print "\x1b[${fgbg};2;0;0;0m::";
for ($gray = 8; $gray < 256; $gray += 10) {
print "\x1b[${fgbg};2;${gray};${gray};${gray}m::";
# now the grayscale ramp
print "Grayscale ramp:\n";
print "\x1b[${fgbg};2;0;0;0m${s}";
for ($gray = 8; $gray < 256; $gray += 10) {
print "\x1b[${fgbg};2;${gray};${gray};${gray}m${s}";
}
print "\x1b[${fgbg};2;255;255;255m${s}";
print "\x1b[0m\n\n";
}
print "\x1b[${fgbg};2;255;255;255m::";
print "\x1b[0m\n\n";
}
}
print "Compare direct palette and RGB color modes\n\n";
$s = "::";
for ($fgbg = 48; $fgbg <= 48; $fgbg +=10) {
for ($fgbg = 48; $fgbg <= 48; $fgbg += 10) {
# the color cube
print "RGB color cube\n";
print "Color cube\n";
for ($green = 0; $green < 6; $green += 1) {
print " RGB: ";
for ($red = 0; $red < 6; $red += 1) {
for ($blue = 0; $blue < 6; $blue += 1) {
$r = $red * 51;
@ -87,7 +87,28 @@ for ($fgbg = 48; $fgbg <= 48; $fgbg +=10) {
print "\x1b[0m ";
}
print "\n";
print " xTerm: ";
for ($red = 0; $red < 6; $red += 1) {
for ($blue = 0; $blue < 6; $blue += 1) {
$r = $red ? $red * 40 + 55 : 0;
$g = $green ? $green * 40 + 55 : 0;
$b = $blue ? $blue * 40 + 55 : 0;
print "\x1b[${fgbg};2;${r};${g};${b}m${s}";
}
print "\x1b[0m ";
}
print "\n";
print "Palette: ";
for ($red = 0; $red < 6; $red += 1) {
for ($blue = 0; $blue < 6; $blue += 1) {
$color = 16 + $red * 36 + $green * 6 + $blue;
print "\x1b[${fgbg};5;${color}m${s}";
}
print "\x1b[0m ";
}
print "\n";
}
if (0) {
print "Xterm palette color cube\n";
for ($green = 0; $green < 6; $green += 1) {
for ($red = 0; $red < 6; $red += 1) {
@ -112,22 +133,25 @@ for ($fgbg = 48; $fgbg <= 48; $fgbg +=10) {
}
print "\n";
}
}
# the grayscale ramp
# the grayscale ramps
print "Grayscale ramp:\n";
print " RGB: ";
print "\x1b[${fgbg};2;0;0;0m${s}";
for ($gray = 8; $gray < 256; $gray += 10) {
for ($gray = 8; $gray <= 238; $gray += 10) {
print "\x1b[${fgbg};2;${gray};${gray};${gray}m${s}";
}
print "\x1b[${fgbg};2;255;255;255m${s}";
print "\x1b[0m\n";
print "Palette: ";
print "\x1b[${fgbg};2;0;0;0m${s}";
for ($gray = 8; $gray < 256; $gray += 10) {
print "\x1b[${fgbg};2;${gray};${gray};${gray}m${s}";
print "\x1b[${fgbg};5;16m${s}";
for ($gray = 232; $gray < 256; $gray++) {
print "\x1b[${fgbg};5;${gray}m${s}";
}
print "\x1b[${fgbg};2;255;255;255m${s}";
print "\x1b[${fgbg};5;231m${s}";
print "\x1b[0m\n";
print "\n";
}
@ -141,10 +165,10 @@ if (0) {
for ($blue = 0; $blue < 256; $blue += 51) {
print "\x1b[0mr:${red}\tg:${green}:\tb:${blue}\t";
print "c:${color}\t";
print "\x1b[${fgbg};2;${red};${green};${blue}m::";
print "\x1b[${fgbg};2;${red};${green};${blue}m${s}";
print " xxXX\x1b[0m";
$color = 16 + ($red / 51) * 36 + ($green / 51) * 6 + ($blue / 51);
print " \x1b[${fgbg};5;${color}m::";
print " \x1b[${fgbg};5;${color}m${s}";
print " xxXX\x1b[0m";
print "\n";
}

22
tests/getpalette.sh Executable file
View file

@ -0,0 +1,22 @@
#!/bin/bash
echo "256 color palette:" > $1
for i in `seq 0 9`; do printf "\033]4;$i;?\007"; read -n 25 pal; echo $pal >> $1; printf "\r"; done
for i in `seq 10 99`; do printf "\033]4;$i;?\007"; read -n 26 pal; echo $pal >> $1; printf "\r"; done
for i in `seq 100 255`; do printf "\033]4;$i;?\007"; read -n 27 pal; echo $pal >> $1; printf "\r"; done
echo "special colors as palette entries:" >> $1
# Pc = 0 ⇐ resource colorBD (BOLD).
# Pc = 1 ⇐ resource colorUL (UNDERLINE).
# Pc = 2 ⇐ resource colorBL (BLINK).
# Pc = 3 ⇐ resource colorRV (REVERSE).
# Pc = 4 ⇐ resource colorIT (ITALIC).
for i in `seq 256 260`; do printf "\033]4;$i;?\007"; read -n 27 pal; echo $pal >> $1; printf "\r"; done
# not supported by iTerm2
#echo "special colors:" >> $1
#for i in `seq 0 4`; do printf "\033]5;$i;?\007"; read -n 25 pal; echo $pal >> $1; printf "\r"; done
echo "dynamic colors:" >> $1
# text default foreground and background colors
for i in `seq 10 11`; do printf "\033]$i;?\007"; read -n 24 pal; echo $pal >> $1; printf "\r"; done
# other dynamic colors (not supported by iTerm2)
#for i in `seq 10 19`; do printf "\033]$i;?\007"; read -n 24 pal; echo $pal >> $1; printf "\r"; done
printf "\033[K"

313
tests/term-palette.sh Executable file
View file

@ -0,0 +1,313 @@
#!/bin/bash
#
# This file tests support for the 7936 color system
# a full 4x4x4 4096-color cube is drawn using 24-bit
# set background color escape sequences.
# various color ramps are drawn:
# - 256 level fade to back from pure colors
# - 256 level fade to white from pure colors
# - 256 level gray scales
# - 256 shade fully saturated rainbow colors
#export out=""
check="$1"
output() {
echo -ne "$1"
#export out="$out$1"
}
flush() {
true;
#echo -ne "$out"
}
setBackgroundColor() {
# printf '\x1bPtmux;\x1b\x1b[48;2;%s;%s;%sm' $1 $2 $3
# printf '\x1b[48;2;%s;%s;%sm%s' $1 $2 $3 "$4"
#echo -ne "\033[48;2;$1;$2;$3m$4"
let rr="$1"
let gg="$2"
let bb="$3"
output "\033[48;2;${rr};${gg};${bb}m$4"
}
resetColor() {
# printf '\x1b[0m%s' "$1"
#echo -ne "\033[0m$1"
output "\033[0m$1"
}
# Gives a color $1/255 % along HSV
# Who knows what happens when $1 is outside 0-255
# Echoes "$red $green $blue" where
# $red $green and $blue are integers
# ranging between 0 and 255 inclusive
rainbowColor() {
let h=$1/43
let f=$1-43*$h
let t=$f*255/43
let q=255-t
if [ $h -eq 0 ]
then
echo "255 $t 0"
elif [ $h -eq 1 ]
then
echo "$q 255 0"
elif [ $h -eq 2 ]
then
echo "0 255 $t"
elif [ $h -eq 3 ]
then
echo "0 $q 255"
elif [ $h -eq 4 ]
then
echo "$t 0 255"
elif [ $h -eq 5 ]
then
echo "255 0 $q"
else
# execution should never reach here
echo "0 0 0"
fi
}
paletteRamp() {
let end=$1+$2-1
for i in `seq $1 $end`; do
output "\033[48;5;${i}m$3"
done
resetColor "$4";
}
paletteEmulate() {
let end=$1+$2-1
for i in `seq $1 $end`; do
let rr6="$i/36%6"
let gg6="$i/6%6"
let bb6="$i%6"
let rr="$rr6?$rr6*40+55:0"
let gg="$gg6?$gg6*40+55:0"
let bb="$bb6?$bb6*40+55:0"
output "\033[48;2;${rr};${gg};${bb}m$3"
done
resetColor $'\n';
}
colorRamp() {
for i in `seq 0 255`; do
if [ $i -lt 64 ] ; then let v=$i ;
elif [ $i -lt 128 ] ; then let v=192-$i ;
elif [ $i -lt 192 ] ; then let v=$i ;
else let v=255+192-$i ; fi
let r=$v*$1
let g=$v*$2
let b=$v*$3
setBackgroundColor $r $g $b "$4"
if [ $i -eq 63 ] ; then resetColor $'\n' ; fi
if [ $i -eq 127 ] ; then resetColor $'\n' ; fi
if [ $i -eq 191 ] ; then resetColor $'\n' ; fi
done
resetColor $'\n'
}
colorRamp1() {
for i in `seq 0 255`; do
if [ $i -lt 128 ] ; then let v=i ; else let v=255+128-i ; fi
let r=$v*$1
let g=$v*$2
let b=$v*$3
setBackgroundColor $r $g $b "$4"
if [ $i -eq 127 ] ; then resetColor $'\n' ; fi
done
resetColor $'\n'
}
colorRamp2() {
for i in `seq 0 255`; do
if [ $i -lt 64 ] ; then let v=$i ;
elif [ $i -lt 128 ] ; then let v=192-$i ;
elif [ $i -lt 192 ] ; then let v=$i ;
else let v=255+192-$i ; fi
let r="($v*$4+(255-$v)*$1)/255"
let g="($v*$5+(255-$v)*$2)/255"
let b="($v*$6+(255-$v)*$3)/255"
setBackgroundColor $r $g $b " "
if [ $i -eq 63 ] ; then resetColor $'\n' ; fi
if [ $i -eq 127 ] ; then resetColor $'\n' ; fi
if [ $i -eq 191 ] ; then resetColor $'\n' ; fi
done
resetColor $'\n'
}
colorRamp3() {
for i in `seq 0 255`; do
if [ $i -lt 128 ] ; then let v=i ; else let v=255+128-i ; fi
let r="($v*$4+(255-$v)*$1)/255"
let g="($v*$5+(255-$v)*$2)/255"
let b="($v*$6+(255-$v)*$3)/255"
setBackgroundColor $r $g $b "$7"
if [ $i -eq 127 ] ; then resetColor $'\n' ; fi
done
resetColor $'\n'
}
rainbowRamp() {
for i in `seq 0 255`; do
if [ $i -lt 64 ] ; then let v=$i ;
elif [ $i -lt 128 ] ; then let v=127+64-$i ;
elif [ $i -lt 192 ] ; then let v=$i ;
else let v=255+192-$i ; fi
setBackgroundColor `rainbowColor $v` ' '
if [ $i -eq 63 ] ; then resetColor $'\n' ; fi
if [ $i -eq 127 ] ; then resetColor $'\n' ; fi
if [ $i -eq 191 ] ; then resetColor $'\n' ; fi
done
resetColor $'\n'
}
colorCube4x4x4() {
for i in `seq 0 3`; do
for g in `seq 0 15`; do
for r in `seq 0 3`; do
for b in `seq 0 15`; do
let r1=$r+$i*4
let rr=$r1*17
let gg=$g*17
let bb=$b*17
setBackgroundColor $rr $gg $bb "$1"
done
resetColor $' '
done
resetColor $'\n'
done
resetColor $'\n'
done
}
xtermPalette() {
output $'System colors:\n'
if [ "$check" == "" ] ; then
setBackgroundColor 0x00 0x00 0x00 "$1"
setBackgroundColor 0xBB 0x00 0x00 "$1"
setBackgroundColor 0x00 0xBB 0x00 "$1"
setBackgroundColor 0xBB 0xBB 0x00 "$1"
setBackgroundColor 0x00 0x00 0xBB "$1"
setBackgroundColor 0xBB 0x00 0xBB "$1"
setBackgroundColor 0x00 0xBB 0xBB "$1"
setBackgroundColor 0xBB 0xBB 0xBB "$1"
setBackgroundColor 0x55 0x55 0x55 "$1"
setBackgroundColor 0xFF 0x55 0x55 "$1"
setBackgroundColor 0x55 0xFF 0x55 "$1"
setBackgroundColor 0xFF 0xFF 0x55 "$1"
setBackgroundColor 0x55 0x55 0xFF "$1"
setBackgroundColor 0xFF 0x55 0xFF "$1"
setBackgroundColor 0x55 0xFF 0xFF "$1"
setBackgroundColor 0xFF 0xFF 0xFF "$1"
resetColor $' iTerm2 Default (00 55 BB FF)\n'
fi
paletteRamp 0 16 "$1" $' 0..15 terminal colors\n'
if [ "$check" == "" ] ; then
setBackgroundColor 0x00 0x00 0x00 "$1"
setBackgroundColor 0xc9 0x1b 0x00 "$1"
setBackgroundColor 0x00 0xc2 0x00 "$1"
setBackgroundColor 0xc7 0xc4 0x00 "$1"
setBackgroundColor 0x02 0x25 0xc7 "$1"
setBackgroundColor 0xc9 0x30 0xc7 "$1"
setBackgroundColor 0x00 0xc5 0xc7 "$1"
setBackgroundColor 0xc7 0xc7 0xc7 "$1"
setBackgroundColor 0x67 0x67 0x67 "$1"
setBackgroundColor 0xff 0x6d 0x67 "$1"
setBackgroundColor 0x5f 0xf9 0x67 "$1"
setBackgroundColor 0xfe 0xfb 0x67 "$1"
setBackgroundColor 0x68 0x71 0xff "$1"
setBackgroundColor 0xff 0x76 0xff "$1"
setBackgroundColor 0x5f 0xfd 0xff "$1"
setBackgroundColor 0xff 0xfe 0xff "$1"
resetColor $' iTerm2 Default colors (measured)\n'
fi
paletteRamp 0 16 "$1" $' 0..15 terminal colors\n'
if [ "$check" == "" ] ; then
setBackgroundColor 0x00 0x00 0x00 "$1" Black
setBackgroundColor 0x80 0x00 0x00 "$1" Maroon
setBackgroundColor 0x00 0x80 0x00 "$1" Green
setBackgroundColor 0x80 0x80 0x00 "$1" Olive
setBackgroundColor 0x00 0x00 0x80 "$1" Navy
setBackgroundColor 0x80 0x00 0x80 "$1" Purple
setBackgroundColor 0x00 0x80 0x80 "$1" Teal
setBackgroundColor 0xc0 0xc0 0xc0 "$1" Silver
setBackgroundColor 0x80 0x80 0x80 "$1" Grey
setBackgroundColor 0xff 0x00 0x00 "$1" Red
setBackgroundColor 0x00 0xff 0x00 "$1" Lime
setBackgroundColor 0xff 0xff 0x00 "$1" Yellow
setBackgroundColor 0x00 0x00 0xff "$1" Blue
setBackgroundColor 0xff 0x00 0xff "$1" Fuchsia
setBackgroundColor 0x00 0xff 0xff "$1" Aqua
setBackgroundColor 0xff 0xff 0xff "$1" White
resetColor $' CGA palette\n'
fi
output $'Color cube, 6x6x6:\n'
for jj in 0 18; do
for ii in 0 1 2 3 4 5; do
let start=16+$ii*36+$jj
let startE=$ii*36+$jj
paletteRamp $start 18 "$1" $'\n'
if [ "$check" == "" ] ; then
paletteEmulate $startE 18 "$1"
fi
done
done
output $'Grayscale ramp:\n'
paletteRamp 232 24 "$1" $' 232..256\n'
if [ "$check" == "" ] ; then
for i in 0x08 0x12 0x1c 0x26 0x30 0x3a 0x44 0x4e \
0x58 0x62 0x6c 0x76 0x80 0x8a 0x94 0x9e \
0xa8 0xb2 0xbc 0xc6 0xd0 0xda 0xe4 0xee ; do
setBackgroundColor $i $i $i "$1"
done
resetColor $' 8 18...228 238\n'
fi
}
#output $'\n'
#output $'Color cube 4x4x4:\n';
#colorCube4x4x4 $' '
#
#output $'\n'
#output $'Color ramps: fade to black\n';
#colorRamp1 1 0 0 $' '
#colorRamp1 0 1 0 $' '
#colorRamp1 0 0 1 $' '
#colorRamp1 0 1 1 $' '
#colorRamp1 1 0 1 $' '
#colorRamp1 1 1 0 $' '
#
## output $'Color edges\n';
## colorRamp3 255 0 0 255 0 255 $' '
## colorRamp3 0 255 0 255 255 0 $' '
## colorRamp3 0 0 255 0 255 255 $' '
#
#output $'\n'
#output $'Color ramps: fade to white\n';
#colorRamp3 255 0 0 255 255 255 $' '
#colorRamp3 0 255 0 255 255 255 $' '
#colorRamp3 0 0 255 255 255 255 $' '
#colorRamp3 0 255 255 255 255 255 $' '
#colorRamp3 255 0 255 255 255 255 $' '
#colorRamp3 255 255 0 255 255 255 $' '
#
#output $'\n'
#output $'Grayscale ramp:\n';
#colorRamp3 0 0 0 255 255 255 $' '
#
#output $'\n'
#output $'Rainbow ramp:\n';
#rainbowRamp $' '
output $'\n'
output $'xterm palette:\n';
xtermPalette $' '
flush;

View file

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
"""
XTerm Colour Chart 2.0
@ -269,28 +269,28 @@ def error(e):
def cube_vals(n):
"""Return the cube coordinates for colour-number n."""
assert n>=cube_start and n<gray_start
val = n-cube_start
assert n >= cube_start and n < gray_start
val = n - cube_start
c = val % cube_size
val = val / cube_size
val = val // cube_size
b = val % cube_size
a = val / cube_size
a = val // cube_size
return a, b, c
def n_to_rgb(n):
"""Return the red, green and blue components of colour-number n.
Components are between 0 and 255."""
if n<cube_start:
return basic_colours[n-basic_start]
if n<gray_start:
if n < cube_start:
return basic_colours[n - basic_start]
if n < gray_start:
return [cube_steps[v] for v in cube_vals(n)]
return (gray_steps[n-gray_start],) * 3
return (gray_steps[n - gray_start],) * 3
def n_to_gray(n):
"""Return an approximate desaturated value for colour-number n.
Value is between 0.0 and 255.0."""
r, g, b = n_to_rgb(n)
return 0.3*r + 0.59*g + 0.11*b
return 0.30*r + 0.59*g + 0.11*b
def n_to_prt(n):
"""Convert a colour number to the format used in the colour charts."""
@ -428,7 +428,7 @@ def draw_chart(chart, origin, angle, numbers, cell_cols, cell_rows):
out.append(transform_block(n, row))
else:
out.append(block(n, row))
print "".join(out) + "\x1b[0m"
print("".join(out) + "\x1b[0m")
def reset_palette():
"""Reset the terminal palette."""
@ -469,10 +469,10 @@ def main():
"modes in xterm")
options, args = parser.parse_args()
if options.origin<0 or options.origin>7:
if options.origin < 0 or options.origin > 7:
error("Invalid origin value specified!")
sys.exit(2)
if options.angle<0 or options.angle>5:
if options.angle < 0 or options.angle > 5:
error("Invalid angle value specified!")
sys.exit(2)
if options.columns < 1:
@ -482,9 +482,9 @@ def main():
if options.colours_88:
set_88_colour_mode()
if options.list_charts:
print "Charts available in %d-colour mode:" % colours
print("Charts available in %d-colour mode:" % colours)
for cname in charts[colours].keys():
print " "+cname
print(" " + cname)
sys.exit(0)
if options.reset_palette:
reset_palette()
@ -494,7 +494,7 @@ def main():
first = True
for cname in args:
if not first:
print
print()
first = False
if cname not in charts[colours]:
error("Chart %r not found!" % cname)