From 8b664a14ef5c4b69175d43fe9ac2b3991d9e5947 Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 15 Dec 2019 21:13:56 -0500 Subject: [PATCH] move RGBtoANSI into internal header --- src/lib/internal.h | 21 +++++++++++++++++++++ src/lib/notcurses.c | 21 --------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/lib/internal.h b/src/lib/internal.h index e17e835ca..1cab091b2 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -167,6 +167,27 @@ flash_damage_map(unsigned char* damage, int count, bool val){ // mark all lines of the notcurses object touched by this plane as damaged void ncplane_updamage(ncplane* n); +// For our first attempt, O(1) uniform conversion from 8-bit r/g/b down to +// ~2.4-bit 6x6x6 ANSI cube + greyscale (assumed on entry; I know no way to +// even semi-portably recover the palette) proceeds via: map each 8-bit to +// a 5-bit target grey. if all 3 components match, select that grey. +// otherwise, c / 42.7 to map to 6 values. this never generates pure black +// nor white, though, lame...FIXME +static inline int +rgb_to_ansi256(unsigned r, unsigned g, unsigned b){ + const unsigned GREYMASK = 0xf8; + r &= GREYMASK; + g &= GREYMASK; + b &= GREYMASK; + if(r == g && g == b){ // 5 MSBs match, return grey + return 216 + (r >> 3u); + } + r /= 43; + g /= 43; + b /= 43; + return r * 36 + g * 6 + b; +} + #define NANOSECS_IN_SEC 1000000000 #ifdef __cplusplus diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 93941a5f4..232cbd547 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -929,27 +929,6 @@ term_esc_rgb(FILE* out, int esc, unsigned r, unsigned g, unsigned b){ return 0; } -// For our first attempt, O(1) uniform conversion from 8-bit r/g/b down to -// ~2.4-bit 6x6x6 ANSI cube + greyscale (assumed on entry; I know no way to -// even semi-portably recover the palette) proceeds via: map each 8-bit to -// a 5-bit target grey. if all 3 components match, select that grey. -// otherwise, c / 42.7 to map to 6 values. this never generates pure black -// nor white, though, lame...FIXME -static inline int -rgb_to_ansi256(unsigned r, unsigned g, unsigned b){ - const unsigned GREYMASK = 0xf8; - r &= GREYMASK; - g &= GREYMASK; - b &= GREYMASK; - if(r == g && g == b){ // 5 MSBs match, return grey - return 216 + (r >> 3u); - } - r /= 43; - g /= 43; - b /= 43; - return r * 36 + g * 6 + b; -} - static int term_bg_rgb8(notcurses* nc, FILE* out, unsigned r, unsigned g, unsigned b){ // We typically want to use tputs() and tiperm() to acquire and write the