int init() { int c; for (c = 0; c < 256; c++) { COLOR_TABLE[c] = xterm_to_rgb(c); } return 0; }
/** * * Quantize RGB values to an xterm 256-color ID * */ int rgb_to_xterm(int r, int g, int b) { int best_match = 0; int smallest_distance = 1000000000; int c, d; if(rgb_init == false) { for (c = 0; c < 256; c++) { COLOR_TABLE[c] = xterm_to_rgb(c); } rgb_init = true; } for (c = 16; c < 256; c++) { d = sqr(COLOR_TABLE[c].r - r) + sqr(COLOR_TABLE[c].g - g) + sqr(COLOR_TABLE[c].b - b); if (d < smallest_distance) { smallest_distance = d; best_match = c; } } return best_match; }
/** * * This function provides a quick and dirty way to serialize an rgb_t * * struct to an int which can be decoded by our Python code using * * ctypes. * */ int xterm_to_rgb_i(int xcolor) { rgb_t res = xterm_to_rgb(xcolor); return (res.r << 16) | (res.g << 8) | (res.b << 0); }