From ac9a92480b24d76ad9ecdd6dda50b7bbc741dfef Mon Sep 17 00:00:00 2001 From: Boris Faure Date: Thu, 2 Jul 2020 23:01:42 +0200 Subject: [PATCH] termptyesc: handle xterm color format starting with "rgbi:" --- src/bin/termptyesc.c | 62 ++++++++++++++++++++++++++++++++++---- tests/tests.results | 1 + tests/xterm-colors-rgbi.sh | 45 +++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 6 deletions(-) create mode 100755 tests/xterm-colors-rgbi.sh diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c index f4f79408..76e09918 100644 --- a/src/bin/termptyesc.c +++ b/src/bin/termptyesc.c @@ -3762,14 +3762,64 @@ _xterm_parse_color_sharp(Eina_Unicode *p, return 0; } +/* returns len read or -1 in case of error */ static int -_xterm_parse_color_rgbi(Eina_Unicode *p EINA_UNUSED, - unsigned char *r EINA_UNUSED, - unsigned char *g EINA_UNUSED, - unsigned char *b EINA_UNUSED, - int len EINA_UNUSED) +_xterm_parse_intensity(Eina_Unicode *p, unsigned char *c, int len) { - return -1; + int l = 0; + char buf[64]; + char *endptr_double; + double d; + + while (l < 64 && len && p[0] && p[0] != '/' && p[0] != '\007' && p[0] < 128) + { + buf[l++] = p[0]; + len--; + p++; + } + if (l == 0) + return -1; + buf[l] = '\0'; + + d = eina_convert_strtod_c(buf, &endptr_double); + if (endptr_double == buf || d < 0 || d > 1.0 || isnan(d)) + return -1; + + *c = d * 255.0; + return endptr_double - buf; +} + +static int +_xterm_parse_color_rgbi(Eina_Unicode *p, + unsigned char *r, + unsigned char *g, + unsigned char *b, + int len) +{ + int l; + + /* parse r */ + l = _xterm_parse_intensity(p, r, len); + if (l <= 0) + return -1; + p += l; + if (p[0] != '/') + return -1; + p++; + /* parse g */ + l = _xterm_parse_intensity(p, g, len); + if (l <= 0) + return -1; + p += l; + if (p[0] != '/') + return -1; + p++; + /* parse b */ + l = _xterm_parse_intensity(p, b, len); + if (l <= 0) + return -1; + + return 0; } /* returns len read or -1 in case of error */ diff --git a/tests/tests.results b/tests/tests.results index 1d33d208..7977578e 100644 --- a/tests/tests.results +++ b/tests/tests.results @@ -147,3 +147,4 @@ xterm-osc-10.sh b8c23c9c5482b1e9c30d8a261edc29f0 xterm-osc-11.sh 3e02038964b78d948fb599c996bf370d xterm-colors-sharp.sh 79d6f72df04237d76a0fa3e722dcec5b xterm-colors-rgb.sh d9b55817ef8428343105b44dabd535a8 +xterm-colors-rgbi.sh d9b55817ef8428343105b44dabd535a8 diff --git a/tests/xterm-colors-rgbi.sh b/tests/xterm-colors-rgbi.sh new file mode 100755 index 00000000..8508dca0 --- /dev/null +++ b/tests/xterm-colors-rgbi.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +# char width: 7 +# char height: 15 + +# set color +printf '\033[0;31;3m' + +# clear screen +printf '\033[2J' + +# move to 0; 0 +printf '\033[0;0H' + +# set color +printf '\033[0m' + +printf 'The purpose of computing is insight, not numbers.\r\n' +printf 'Richard Hamming\r\n' + +# valid colors +printf '\033]10;rgbi:1.0/0/1.0\007' +printf '\033]10;rgbi:1/0.0/1\007' +printf '\033]10;rgbi:1/0.0/1\007' + +## +# invalid +## + +printf '\033]10;rgbi:1.1/0/1\007' +printf '\033]10;rgbi:f/1.1/1\007' +printf '\033]10;rgbi:f/0/1.1\007' + +printf '\033]10;rgbi:-0.1/0/1\007' +printf '\033]10;rgbi:1/-0.1/1\007' +printf '\033]10;rgbi:1/0/-0.1\007' + +printf '\033]10;rgbi:+Inf/0/1\007' +printf '\033]10;rgbi:1/+Inf/0\007' +printf '\033]10;rgbi:1/0/+Inf\007' + +printf '\033]10;rgbi:1.0|0.0/1.0\007' +printf '\033]10;rgbi:1.0/0.0|1.0\007' + +printf '\033]10;rgbi:1.0/0.0/1.0/0.0\007'