summaryrefslogtreecommitdiffstats
path: root/sys/teken/teken.c
diff options
context:
space:
mode:
authored <ed@FreeBSD.org>2009-09-26 15:26:32 +0000
committered <ed@FreeBSD.org>2009-09-26 15:26:32 +0000
commitb69a48a63f54bfcc1c9b1bf8a8828fbaf25e004b (patch)
tree33e925a77e9fa035d0525a951f7a87e23f46271e /sys/teken/teken.c
parentba94d1a005032941ea59b2f361257e1cbd4fa6bd (diff)
downloadFreeBSD-src-b69a48a63f54bfcc1c9b1bf8a8828fbaf25e004b.zip
FreeBSD-src-b69a48a63f54bfcc1c9b1bf8a8828fbaf25e004b.tar.gz
Add 256 color support.
It is quite inconvenient that if an application for xterm uses 256 color mode, text suddenly starts to blink (because of ;5; in the middle). We'd better just implement 256 color mode and add a conversion routine from 256 to 8 color mode, which doesn't seem to be too bad in practice. Remapping colors is done quite simple. If one of the channels is most actively represented, primary colors are used. If two channels are most actively represented, secondary colors are used. If all three channels are equal (gray), it picks between black and white. Reported by: Paul B. Mahol <onemda gmail com>
Diffstat (limited to 'sys/teken/teken.c')
-rw-r--r--sys/teken/teken.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/sys/teken/teken.c b/sys/teken/teken.c
index f44969d..c8d6b09 100644
--- a/sys/teken/teken.c
+++ b/sys/teken/teken.c
@@ -409,4 +409,55 @@ teken_state_numbers(teken_t *t, teken_char_t c)
return (0);
}
+teken_color_t
+teken_256to8(teken_color_t c)
+{
+ unsigned int r, g, b;
+
+ if (c < 16) {
+ /* Traditional color indices. */
+ return (c % 8);
+ } else if (c >= 244) {
+ /* Upper grayscale colors. */
+ return (TC_WHITE);
+ } else if (c >= 232) {
+ /* Lower grayscale colors. */
+ return (TC_BLACK);
+ }
+
+ /* Convert to RGB. */
+ c -= 16;
+ b = c % 6;
+ g = (c / 6) % 6;
+ r = c / 36;
+
+ if (r < g) {
+ /* Possibly green. */
+ if (g < b)
+ return (TC_BLUE);
+ else if (g > b)
+ return (TC_GREEN);
+ else
+ return (TC_CYAN);
+ } else if (r > g) {
+ /* Possibly red. */
+ if (r < b)
+ return (TC_BLUE);
+ else if (r > b)
+ return (TC_RED);
+ else
+ return (TC_MAGENTA);
+ } else {
+ /* Possibly brown. */
+ if (g < b)
+ return (TC_BLUE);
+ else if (g > b)
+ return (TC_BROWN);
+ else if (r < 3)
+ return (TC_BLACK);
+ else
+ return (TC_WHITE);
+ }
+}
+
#include "teken_state.h"
OpenPOWER on IntegriCloud