Skip to content

Instantly share code, notes, and snippets.

@andylshort
Created March 23, 2018 23:10
Show Gist options
  • Save andylshort/7c9a86deb2c1507d9b81116b640d8da6 to your computer and use it in GitHub Desktop.
Save andylshort/7c9a86deb2c1507d9b81116b640d8da6 to your computer and use it in GitHub Desktop.
RGB to HSV
var rgbToHsv = function(r, g, b) {
var rp = r / 255;
var gp = g / 255;
var bp = b / 255;
var cmax = Math.max(rp, gp, bp);
var cmin = Math.min(rp, gp, bp);
var delta = cmax - cmin;
var h = 0;
if (delta === 0) {
h = 0;
} else if (cmax == rp) {
h = 0.6 * (((gp - bp) / delta) % 6);
} else if (cmax == gp) {
h = 0.6 * (((bp - rp) / delta) + 2);
} else if (cmax == bp) {
h = 0.6 * (((rp - gp) / delta) + 4);
}
var s = cmax == 0 ? 0 : delta / cmax;
var v = cmax;
return [h, s, v];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment