Skip to content

Instantly share code, notes, and snippets.

@asvny
Last active May 18, 2017 07:14
Show Gist options
  • Save asvny/44153999e1991b9293412fe325462f8d to your computer and use it in GitHub Desktop.
Save asvny/44153999e1991b9293412fe325462f8d to your computer and use it in GitHub Desktop.
Random color
// http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
function hsv_to_rgb(h, s = 0.5, v = 0.95) {
let _h = ( Math.random() + 0.618033988749895 ) % 1;
h = h || _h;
let h_i = Number((h*6).toFixed(0));
let f = h*6 - h_i;
let p = v * (1 - s);
let q = v * (1 - f*s);
let t = v * (1 - (1 - f) * s);
let r=0,g=0,b=0;
if(h_i === 0) {
[r, g, b] = [v, t, p]
}
else if(h_i === 1) {
[r, g, b] = [q, v, p]
}
else if(h_i === 2) {
[r, g, b] = [p, v, t]
}
else if(h_i === 3) {
[r, g, b] = [p, q, v]
}
else if(h_i === 4) {
[r, g, b] = [t, p, v]
}
else if(h_i === 5) {
[r, g, b] = [v, p, q]
}
return `rgb(${(r*256).toFixed(0)},${(g*256).toFixed(0)},${(b*256).toFixed(0)})`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment