Skip to content

Instantly share code, notes, and snippets.

@weedgrease
Created November 19, 2016 06:12
Show Gist options
  • Save weedgrease/d41ec28617410dbeb37d6e6d8df6262d to your computer and use it in GitHub Desktop.
Save weedgrease/d41ec28617410dbeb37d6e6d8df6262d to your computer and use it in GitHub Desktop.
const chroma = require('chroma-js');
var Q = 255;
var color = chroma("#FF0000").rgb();
var max = Math.max.apply(null, color);
var min = Math.min.apply(null, color);
var K;
var Wo = (min/max < 0.5) ? ((min*max) / (max-min)) : (max);
if(color[0] == color[1] && color[0] == color[2] && color[0] != null) {
K = 1;
} else {
K = (Wo + max) / min;
}
if (K > Q) {
K = Q;
} else if (K < 0) {
K = 0;
}
var Ro, Go, Bo;
Ro = Math.floor(((K * color[0]) - Wo) / Q);
Go = Math.floor(((K * color[1]) - Wo) / Q);
Bo = Math.floor(((K * color[2]) - Wo) / Q);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment