JSFiddle - React, Tailwind, and code Playground
JavaScript
colors_lst = [
["da542f", 3117],
["da5630", 60]
]
console.log(colors_lst);
function hexColorDelta(array) {
function hexColorCalc(hex1, hex2) {
// get red/green/blue int values of hex1
var 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 hex2
var 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 blues
var 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 colors
return (r + g + b) / 3;
}
if (array.length == 1) {
console.log('length of array : 1');
console.log('RETURN ------------');
return array
}
if (array.length == 2) {
console.log('length of array : 2');
console.log('RETURN ------------');
var hex1 = array[0][0];
var hex2 = array[1][0];
if (hexColorCalc(hex1, hex2) > 0.9) {
colors_lst = array.pop();
return colors_lst;
}
}
if (array.length == 3) {
var hex1 = array[0][0];
var hex2 = array[1][0];
var hex3 = array[2][0];
if (hexColorCalc(hex1, hex2) > 0.9) {
array.splice(2, 1);
}
if (hexColorCalc(hex1, hex3) > 0.9) {
array.splice(3, 1);
}
if (hexColorCalc(hex2, hex3) > 0.9) {
array.splice(2, 1);
}
return array
}
}
console.log(hexColorDelta(colors_lst));