Improved accuracy for blue colors.

This commit is contained in:
2017-03-19 22:22:12 -07:00
parent a01a1fe563
commit 97ccf969c0

View File

@ -13,31 +13,31 @@ void hsl2rgb(struct Rgb *dest, const struct Hls const *src) {
return; return;
} }
float r, g, b; float r, g, b;
float hue = src -> h; float hue = (src -> h)/60.0f;
float chroma = (1 - fabs(2 * (src -> l) - 1)) * (src -> s); const float chroma = (1 - fabs((2 * (src -> l)) - 1)) * (src -> s);
float x = chroma * (1 - fabs((int)(hue/60) % 2 - 1)); float x = chroma * (1 - fabs(fmod(hue, 2) - 1));
float m = (src -> l) - (chroma/2); float m = (src -> l) - (chroma/2);
if (0 <= hue && hue < 60) { if (0 <= hue && hue < 1) {
r = chroma; r = chroma;
g = x; g = x;
b = 0; b = 0;
} else if (60 <= hue && hue < 120) { } else if (1 <= hue && hue < 2) {
r = x; r = x;
g = chroma; g = chroma;
b = 0; b = 0;
} else if (120 <= hue && hue < 180) { } else if (2 <= hue && hue < 3) {
r = 0; r = 0;
g = chroma; g = chroma;
b = x; b = x;
} else if (180 <= hue && hue < 240) { } else if (3 <= hue && hue < 4) {
r = 0; r = 0;
g = x; g = x;
b = chroma; b = chroma;
} else if (240 <= hue && hue < 300) { } else if (4 <= hue && hue < 5) {
r = x; r = x;
g = 0; g = 0;
b = chroma; b = chroma;
} else if (300 <= hue && hue < 360) { } else if (5 <= hue && hue < 6) {
r = chroma; r = chroma;
g = 0; g = 0;
b = x; b = x;
@ -55,22 +55,22 @@ void rgb2hsl(struct Hls *dest, const struct Rgb const *src) {
if (src == NULL || dest == NULL) { if (src == NULL || dest == NULL) {
return; return;
} }
float r = src -> r / 255.0f; float r = (src -> r) / 255.0f;
float g = src -> g / 255.0f; float g = (src -> g) / 255.0f;
float b = src -> b / 255.0f; float b = (src -> b) / 255.0f;
float min = fmin(fmin(r, g), b); float min = fmin(fmin(r, g), b);
float max = fmax(fmax(r, g), b); float max = fmax(fmax(r, g), b);
float chroma = max - min;
dest -> l = (max + min)/2; //bi-hexcone model. dest -> l = (max + min)/2; //bi-hexcone model.
if (max == min) { // neutral color. if (chroma == 0) { // neutral color.
dest -> h = 0; // undefined. dest -> h = 0; // undefined.
dest -> s = 0.0f; dest -> s = 0.0f;
} else { } else {
float chroma = max - min;
dest -> s = (dest -> l > 0.5) ? (chroma/(2 - max - min)) : (chroma/(max + min)); dest -> s = (dest -> l > 0.5) ? (chroma/(2 - max - min)) : (chroma/(max + min));
//piecewise function.. //piecewise function..
float hue; float hue;
if (max == r) { if (max == r) {
hue = (int)((g - b)/chroma) + (g < b ? 6 : 0); // % 6 will break magenta (255, 0, 255) hue = (int)((g - b)/chroma) + (g < b ? 6 : 0); // % 6 will break magenta (255, 0, 255)
} else if (max == g) { } else if (max == g) {
hue = ((b - r)/chroma) + 2.0f; hue = ((b - r)/chroma) + 2.0f;
} else { } else {
@ -120,9 +120,11 @@ static void hexspan(const char *str) {
const char h2[2] = {str[1], '\0'}; const char h2[2] = {str[1], '\0'};
const char h3[2] = {str[2], '\0'}; const char h3[2] = {str[2], '\0'};
struct Rgb nrgb; struct Rgb nrgb;
nrgb.r = strtol(h1, NULL, 16); // max value for a color code in darkplaces = 0xF
nrgb.g = strtol(h2, NULL, 16); // 0xF * 0xF = 255, max value for rgb
nrgb.b = strtol(h3, NULL, 16); nrgb.r = strtol(h1, NULL, 16) * 0xF;
nrgb.g = strtol(h2, NULL, 16) * 0xF;
nrgb.b = strtol(h3, NULL, 16) * 0xF;
struct Hls nhls; struct Hls nhls;
rgb2hsl(&nhls, &nrgb); rgb2hsl(&nhls, &nrgb);
if (nhls.l < MIN_CONTRAST) { if (nhls.l < MIN_CONTRAST) {
@ -143,6 +145,7 @@ static void b(char * const str) {
if (strlen(token) > 1) { if (strlen(token) > 1) {
printf("%s", token + 1); printf("%s", token + 1);
} }
printf("</span>");
} else if ((c == 'x' && strlen(token) > 3) && } else if ((c == 'x' && strlen(token) > 3) &&
(isxdigit(token[1]) && (isxdigit(token[1]) &&
isxdigit(token[2]) && isxdigit(token[2]) &&