#include <stdbool.h>
#include <stdint.h>
#include "ansi_colours.h"
static uint32_t cube_value(uint8_t r);
static uint32_t cube_index_red(uint8_t r);
static uint32_t cube_index_green(uint8_t g);
static uint32_t cube_index_blue(uint8_t b);
static uint8_t luminance(uint32_t rgb);
static uint32_t distance(uint32_t x, uint32_t y);
#define R(c) (((c) >> 16) & 0xff)
#define G(c) (((c) >> 8) & 0xff)
#define B(c) ( (c) & 0xff)
uint32_t rgb_from_ansi256(uint8_t index) {
static uint32_t system_colours[16] = {
0x000000, 0xce0000, 0x00ce00, 0xcece00,
0x0000ee, 0xce00ce, 0x00cece, 0xefefef,
0x7f7f7f, 0xff0000, 0x00ff00, 0xffff00,
0x5c5cff, 0xff00ff, 0x00ffff, 0xffffff,
};
if (!(index & 0xf0)) {
return system_colours[index];
} else if (index < 232) {
index -= 16;
return ((cube_value(index / 36 ) << 16) |
(cube_value(index / 6 % 6) << 8) |
(cube_value(index % 6)));
} else {
index = (index - 232) * 10 + 8;
return (uint32_t)index * 0x010101;
}
}
uint8_t ansi256_from_rgb(uint32_t rgb) {
static const uint8_t ansi256_from_grey[256] = {
16, 16, 16, 16, 16, 232, 232, 232,
232, 232, 232, 232, 232, 232, 233, 233,
233, 233, 233, 233, 233, 233, 233, 233,
234, 234, 234, 234, 234, 234, 234, 234,
234, 234, 235, 235, 235, 235, 235, 235,
235, 235, 235, 235, 236, 236, 236, 236,
236, 236, 236, 236, 236, 236, 237, 237,
237, 237, 237, 237, 237, 237, 237, 237,
238, 238, 238, 238, 238, 238, 238, 238,
238, 238, 239, 239, 239, 239, 239, 239,
239, 239, 239, 239, 240, 240, 240, 240,
240, 240, 240, 240, 59, 59, 59, 59,
59, 241, 241, 241, 241, 241, 241, 241,
242, 242, 242, 242, 242, 242, 242, 242,
242, 242, 243, 243, 243, 243, 243, 243,
243, 243, 243, 244, 244, 244, 244, 244,
244, 244, 244, 244, 102, 102, 102, 102,
102, 245, 245, 245, 245, 245, 245, 246,
246, 246, 246, 246, 246, 246, 246, 246,
246, 247, 247, 247, 247, 247, 247, 247,
247, 247, 247, 248, 248, 248, 248, 248,
248, 248, 248, 248, 145, 145, 145, 145,
145, 249, 249, 249, 249, 249, 249, 250,
250, 250, 250, 250, 250, 250, 250, 250,
250, 251, 251, 251, 251, 251, 251, 251,
251, 251, 251, 252, 252, 252, 252, 252,
252, 252, 252, 252, 188, 188, 188, 188,
188, 253, 253, 253, 253, 253, 253, 254,
254, 254, 254, 254, 254, 254, 254, 254,
254, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 231,
231, 231, 231, 231, 231, 231, 231, 231,
};
if (R(rgb) == G(rgb) && G(rgb) == B(rgb)) {
return ansi256_from_grey[rgb & 0xff];
}
uint8_t grey_index = ansi256_from_grey[luminance(rgb)];
uint32_t grey_distance = distance(rgb, rgb_from_ansi256(grey_index));
uint32_t cube = cube_index_red(R(rgb)) + cube_index_green(G(rgb)) +
cube_index_blue(B(rgb));
return distance(rgb, cube) < grey_distance ? cube >> 24 : grey_index;
}
#define CUBE_THRESHOLDS(a, b, c, d, e) \
if (v < a) return IDX(0, 0); \
else if (v < b) return IDX(1, 95); \
else if (v < c) return IDX(2, 135); \
else if (v < d) return IDX(3, 175); \
else if (v < e) return IDX(4, 215); \
else return IDX(5, 255);
#define IDX(i, v) ((((uint32_t)i * 36 + 16) << 24) | ((uint32_t)v << 16))
static uint32_t cube_index_red(uint8_t v) {
CUBE_THRESHOLDS(38, 115, 155, 196, 235);
}
#undef IDX
#define IDX(i, v) ((((uint32_t)i * 6) << 24) | ((uint32_t)v << 8))
static uint32_t cube_index_green(uint8_t v) {
CUBE_THRESHOLDS(36, 116, 154, 195, 235);
}
#undef IDX
#define IDX(i, v) (((uint32_t)i << 24) | (uint32_t)v)
static uint32_t cube_index_blue(uint8_t v) {
CUBE_THRESHOLDS(35, 115, 155, 195, 235);
}
#undef IDX
#undef CUBE_THRESHOLDS
static uint32_t cube_value(uint8_t idx) {
static const uint8_t values[] = {0, 95, 135, 175, 215, 255};
return values[idx];
}
static uint8_t luminance(uint32_t rgb) {
return ((uint32_t) 3568058 * R(rgb) +
(uint32_t)11998262 * G(rgb) +
(uint32_t) 1210896 * B(rgb)) >> 24;
}
static uint32_t distance(uint32_t x, uint32_t y) {
int32_t r_sum = R(x) + R(y);
int32_t r = (int32_t)R(x) - (int32_t)R(y);
int32_t g = (int32_t)G(x) - (int32_t)G(y);
int32_t b = (int32_t)B(x) - (int32_t)B(y);
return (1024 + r_sum) * r * r + 2048 * g * g + (1534 - r_sum) * b * b;
}