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