Skip to content

Instantly share code, notes, and snippets.

@dtudury
Created October 17, 2016 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtudury/e2d97c67a19ea5fd10cc24e8d8f2433f to your computer and use it in GitHub Desktop.
Save dtudury/e2d97c67a19ea5fd10cc24e8d8f2433f to your computer and use it in GitHub Desktop.
map color hues to same luminance
let lum = ([r, g, b]) => r * 0.2126 + g * 0.7152 + b * 0.0722;
let blend = (c0, c1, s, i = 1 - s) => [c0[0] * s + c1[0] * i, c0[1] * s + c1[1] * i, c0[2] * s + c1[2] * i];
let hex = c => c.reduce((p, v) => p + Math.round(0x100 + v * 0xff).toString(16).substr(-2), "");
let colors = {
red: [1, 0, 0],
yellow: [1, 1, 0],
green: [0, 1, 0],
cyan: [0, 1, 1],
blue: [0, 0, 1],
magenta: [1, 0, 1],
white: [1, 1, 1],
black: [0, 0, 0]
};
let t = 0.25;
Object.keys(colors).forEach(key => {
let c = colors[key];
let l = lum(c);
let scaled;
if (l > t) {
scaled = blend(c, colors.black, t / l);
} else {
scaled = blend(colors.white, c, (t - l) / (1 - l));
}
console.log(key, scaled, hex(scaled), lum(scaled));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment