implement quantize_rgb8() for linux console #288 #4

pull/265/head
nick black 5 years ago
parent 48025cd794
commit 22f71c9a80
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -249,8 +249,7 @@ cell_egc_copy(const ncplane* n, const cell* c){
// ~2.4-bit 6x6x6 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
// otherwise, c / 42.7 to map to 6 values.
static inline int
rgb_quantize_256(unsigned r, unsigned g, unsigned b){
const unsigned GREYMASK = 0xf8;
@ -273,6 +272,40 @@ rgb_quantize_256(unsigned r, unsigned g, unsigned b){
return r * 36 + g * 6 + b + 16;
}
// We get black, red, green, yellow, blue, magenta, cyan, and white. Ugh. Under
// an additive model: G + R -> Y, G + B -> C, R + B -> M, R + G + B -> W
static inline int
rgb_quantize_8(unsigned r, unsigned g, unsigned b){
static const int BLACK = 0;
static const int RED = 1;
static const int GREEN = 2;
static const int YELLOW = 3;
static const int BLUE = 4;
static const int MAGENTA = 5;
static const int CYAN = 6;
static const int WHITE = 7;
if(r < 128){ // we have no red
if(g < 128){ // we have no green
if(b < 128){
return BLACK;
}
return BLUE;
} // we have green
if(b < 128){
return GREEN;
}
return CYAN;
}else if(g < 128){ // we have red, but not green
if(b < 128){
return RED;
}
return MAGENTA;
}else if(b < 128){ // we have red and green
return YELLOW;
}
return WHITE;
}
static inline int
term_emit(const char* name __attribute__ ((unused)), const char* seq,
FILE* out, bool flush){

@ -572,6 +572,8 @@ term_bg_rgb8(notcurses* nc, FILE* out, unsigned r, unsigned g, unsigned b){
// interpolation. I have no idea what to do for 88 colors. FIXME
if(nc->colors >= 256){
term_emit("setab", tiparm(nc->setab, rgb_quantize_256(r, g, b)), out, false);
}else if(nc->colors >= 8){
return term_emit("setab", tiparm(nc->setab, rgb_quantize_8(r, g, b)), out, false);
}
}
return 0;
@ -596,6 +598,8 @@ term_fg_rgb8(notcurses* nc, FILE* out, unsigned r, unsigned g, unsigned b){
// interpolation. I have no idea what to do for 88 colors. FIXME
if(nc->colors >= 256){
return term_emit("setaf", tiparm(nc->setaf, rgb_quantize_256(r, g, b)), out, false);
}else if(nc->colors >= 8){
return term_emit("setaf", tiparm(nc->setaf, rgb_quantize_8(r, g, b)), out, false);
}
}
return 0;

Loading…
Cancel
Save