JSFiddle - React, Tailwind, and code Playground
HTML
<input id='cor1' type='text' />
<input id='cor2' type='text' />
<button>Misturar cores</button>
<br>
<h2>Resultado em hexadecimal: <span></span></h2>
<div id='cor_misturada'></div>
CSS
#cor_misturada {
width:100px;
height:100px;
background:#EEE;
}
JavaScript
function tratarInput(arr, degraus) {
arr = arr.toLowerCase();
if (arr.length == 3) {
var partes = arr.split('');
for (var i = 0; i < arr.length; i++) {
if (!~degraus.indexOf(arr[i])) {
alert('formato errado!')
return false;
}
}
arr = [partes[0], partes[0], partes[1], partes[1], partes[2], partes[2]].join('');
} else if (arr.length == 6) {} else {
alert('formato errado!')
return false;
}
return arr;
}
$('button').click(function () {
var degraus = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var cor1 = $('#cor1').val().replace('#', '');
var cor2 = $('#cor2').val().replace('#', '');
cor1 = tratarInput(cor1, degraus);
cor2 = tratarInput(cor2, degraus);
// realiza a mistura
var mistura = [];
for (var i = 0; i < 6; i+=2) {
mistura[i] = Math.round((parseInt(cor1[i] + cor1[i + 1], 16) + parseInt(cor2[i] + cor2[i + 1], 16)) / 2);
mistura[i] = (0x1000000 | mistura[i]).toString(16).substring(5);
}
mistura = '#' + mistura.join('');
$('#cor_misturada').css('background-color', mistura);
$('h2 span').text(mistura);
});