Skip to content

Instantly share code, notes, and snippets.

@saitoha
Created June 21, 2017 17:49
Show Gist options
  • Save saitoha/ab42b07119f76cb4100a650b34261233 to your computer and use it in GitHub Desktop.
Save saitoha/ab42b07119f76cb4100a650b34261233 to your computer and use it in GitHub Desktop.
xterm: fix wrong HLS-to-RGB conversion
diff --git a/graphics.c b/graphics.c
index 4bd50b4..b6f9e54 100644
--- a/graphics.c
+++ b/graphics.c
@@ -1089,7 +1089,7 @@ outline_refresh(TScreen const *screen,
void
hls2rgb(int h, int l, int s, short *r, short *g, short *b)
{
- const int hs = ((h + 240) / 60) % 6;
+ const int hs = ((h = (h + 240) % 360) / 60) % 6;
const double lv = l / 100.0;
const double sv = s / 100.0;
double c, x, m, c2;
@@ -1110,33 +1110,33 @@ hls2rgb(int h, int l, int s, short *r, short *g, short *b)
switch (hs) {
case 0:
r1 = c;
- g1 = x;
+ g1 = c * h / 60.0;
b1 = 0.0;
break;
case 1:
- r1 = x;
+ r1 = c * (120 - h) / 60.0;
g1 = c;
b1 = 0.0;
break;
case 2:
r1 = 0.0;
g1 = c;
- b1 = x;
+ b1 = c * (h - 120) / 60.0;
break;
case 3:
r1 = 0.0;
- g1 = x;
+ g1 = c * (240 - h) / 60.0;
b1 = c;
break;
case 4:
- r1 = x;
+ r1 = c * (h - 240) / 60.0;
g1 = 0.0;
b1 = c;
break;
case 5:
r1 = c;
g1 = 0.0;
- b1 = x;
+ b1 = c * (360 - h) / 60.0;
break;
default:
TRACE(("Bad HLS input: [%d,%d,%d], returning white\n", h, l, s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment