uint32_t val_rgb(float v) { uint32_t rgb = 0; if (rainbow) { float r = v * fminf(fmaxf(4.0f * v - 2.0f, 0.0f), 1.0f); float g = v * fminf(fmaxf(1.0f - 4.0f * fabsf(v - 0.5f), 0.0f), 1.0f); float b = v * fminf(fmaxf(2.0f - 4.0f * v, 0.0f), 1.0f); rgb = srgb(r, g, b); } else { rgb = srgb(v, v, v); } return rgb; }
uint32_t color(float v) { float r = 6.0f * fabsf(v - 3.0f / 6.0f) - 1.0f; float g = 2.0f - 6.0f * fabsf(v - 2.0f / 6.0f); float b = 2.0f - 6.0f * fabsf(v - 4.0f / 6.0f); return srgb(r, g, b); }
// Converts an HSV color to RGB and writes it to the output location with sRGB // encoding. Assumes 'h', 's', and 'v' are on the interval [0,1]. static void hsv(unsigned char *out, double h, double s, double v) { static int map[6][3] = { {0, 3, 1}, {2, 0, 1}, {1, 0, 3}, {1, 2, 0}, {3, 1, 0}, {0, 1, 2} }; double h6 = h * 6; double f = h6 - (int)h6; double vars[4] = { v, v * (1 - s), v * (1 - (s * f)), v * (1 - (s * (1 - f))) }; int i = ((int)h6 % 6 + 6) % 6; out[0] = srgb(vars[map[i][0]]); out[1] = srgb(vars[map[i][1]]); out[2] = srgb(vars[map[i][2]]); }
void grayscale(unsigned char *out, double v) { unsigned char g = srgb(v); out[0] = g; out[1] = g; out[2] = g; }