Skip to content

Instantly share code, notes, and snippets.

@evolutionxbox
Created May 3, 2016 10:04
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 evolutionxbox/750e68816b67ade2807b82e998c23f68 to your computer and use it in GitHub Desktop.
Save evolutionxbox/750e68816b67ade2807b82e998c23f68 to your computer and use it in GitHub Desktop.
Calculate the average alpha needed to get a foreground colour to be as close to a target colour, when used with transparency above a background colour.
var rgb = (function(red, blue, green) {
return {
red: red,
blue: blue,
green: green
};
});
var rgb1 = rgb(0, 0, 0); // colour to use
var rgb2 = rgb(255, 255, 255); // background colour
var rgb3 = rgb(219, 230, 226); // target colour
// calculate alphas
var alpha1 = (rgb3.red-rgb2.red) / (rgb1.red-rgb2.red),
alpha2 = (rgb3.green-rgb2.green) / (rgb1.green-rgb2.green),
alpha3 = (rgb3.blue-rgb2.blue) / (rgb1.blue-rgb2.blue);
// the average alpha to use to get rgb2 as close to rgb3 as possible
var averageAlpha = ((alpha1 + alpha2 + alpha3) / 3).toFixed(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment