Skip to content

Instantly share code, notes, and snippets.

@Keyes
Created November 6, 2017 13:14
Show Gist options
  • Save Keyes/b564529a6fdf43b7c7c94e1b4311a196 to your computer and use it in GitHub Desktop.
Save Keyes/b564529a6fdf43b7c7c94e1b4311a196 to your computer and use it in GitHub Desktop.
function hsi2rgbw(H, S, I) {
let rgbw = {}, cos_h, cos_1047_h;
H = H % 360;
H = Math.PI * H / 180;
S = S > 0 ? (S < 1 ? S : 1) : 0;
I = I > 0 ? (I < 1 ? I : 1) : 0;
if(H < 2.09439) {
cos_h = Math.cos(H);
cos_1047_h = Math.cos(1.047196667 - H);
rgbw.r = S * 255 * I / 3 * (1 + cos_h / cos_1047_h);
rgbw.g = S * 255 * I / 3 * (1 + (1 - cos_h / cos_1047_h));
rgbw.b = 0;
rgbw.w = 255 * (1 - S) * I;
} else if(H < 4.188787) {
H = H - 2.09439;
cos_h = Math.cos(H);
cos_1047_h = Math.cos(1.047196667 - H);
rgbw.r = 0;
rgbw.g = S * 255 * I / 3 * (1 + cos_h / cos_1047_h);
rgbw.b = S * 255 * I / 3 * (1 + (1 - cos_h / cos_1047_h));
rgbw.w = 255 * (1 - S) * I;
} else {
H = H - 4.188787;
cos_h = Math.cos(H);
cos_1047_h = Math.cos(1.047196667 - H);
rgbw.r = S * 255 * I / 3 * (1 + (1 - cos_h / cos_1047_h));
rgbw.g = 0;
rgbw.b = S * 255 * I / 3 * (1 + cos_h / cos_1047_h);
rgbw.w = 255 * (1 - S) * I;
}
rgbw.r = +rgbw.r.toFixed(0);
rgbw.g = +rgbw.g.toFixed(0);
rgbw.b = +rgbw.b.toFixed(0);
rgbw.w = +rgbw.w.toFixed(0);
return rgbw;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment