Detect Similar Colours From Hex Values
Does anyone know of a way of taking two hex colour values and returning some kind of index to say how similar the colours are? e.g two shades of yellow might return a higher index
Solution 1:
Here could be an algorithm to start with:
var yellow1 = "FFFF99";
var yellow2 = "FFFF00";
var blue = "0000FF";
functionhexColorDelta(hex1, hex2) {
// get red/green/blue int values of hex1var r1 = parseInt(hex1.substring(0, 2), 16);
var g1 = parseInt(hex1.substring(2, 4), 16);
var b1 = parseInt(hex1.substring(4, 6), 16);
// get red/green/blue int values of hex2var r2 = parseInt(hex2.substring(0, 2), 16);
var g2 = parseInt(hex2.substring(2, 4), 16);
var b2 = parseInt(hex2.substring(4, 6), 16);
// calculate differences between reds, greens and bluesvar r = 255 - Math.abs(r1 - r2);
var g = 255 - Math.abs(g1 - g2);
var b = 255 - Math.abs(b1 - b2);
// limit differences between 0 and 1
r /= 255;
g /= 255;
b /= 255;
// 0 means opposit colors, 1 means same colorsreturn (r + g + b) / 3;
}
console.log(hexColorDelta(yellow1, yellow2)); // 0.7999999999999999 console.log(hexColorDelta(yellow1, blue)); // 0.19999999999999998
Post a Comment for "Detect Similar Colours From Hex Values"