static int rgb(int r, int g, int b) { const double k = (256.0/6.0); r = R_DIM (r/k, 0, 6); g = R_DIM (g/k, 0, 6); b = R_DIM (b/k, 0, 6); return 16 + (r*36) + (g*6) + b; }
static int rgb(int r, int g, int b) { const double k = (255/6); r = R_DIM (r/k, 0, 5); g = R_DIM (g/k, 0, 5); b = R_DIM (b/k, 0, 5); return 16 + ((r&7)*29) + ((g&7)*12) + (b&7); }
R_API void r_cons_color (int fg, int r, int g, int b) { int k; r = R_DIM (r, 0, 255); g = R_DIM (g, 0, 255); b = R_DIM (b, 0, 255); if (r == g && g == b) { // b&w k = 232 + (int)(((r+g+b)/3)/10.3); } else { r = (int)(r/42.6); g = (int)(g/42.6); b = (int)(b/42.6); k = 16 + (r*36) + (g*6) + b; } r_cons_printf ("\x1b[%d;5;%dm", fg? 48: 38, k); }