Is there a possibility (with plugins or not) of getting the [major] colors or the predominant colors , similar to Adobe Kuler's image?
Example
I have this image:
And I wanted to get the main colors of it, which would be Blue
Is there a possibility (with plugins or not) of getting the [major] colors or the predominant colors , similar to Adobe Kuler's image?
Example
I have this image:
And I wanted to get the main colors of it, which would be Blue
The following code mounts an object containing the histogram and retrieves the most common color using an invisible% element:
//carrega uma imagem
var img = new Image();
img.src = ...
//cria um canvas invisível
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext('2d');
//desenha a imagem no canvas
context.drawImage(img, 0, 0);
//recupera vetor de cores
var map = context.getImageData(0, 0, img.width, img.height).data;
//monta histograma
var hex, r,g,b; //,alpha;
var histograma = {};
for (var i = 0, len = map.length; i < len; i += 4) {
//recupera componentes de um ponto
r = arredonda(map[i]);
g = arredonda(map[i+1]);
b = arredonda(map[i+2]);
//alpha = map[i+2]; //ignora canal alpha
//valor em hexadecimal
hex = rgbToHex(r, g, b);
//adiciona no histograma ou incrementa se já existir
if (histograma[hex] === undefined) {
histograma[hex] = 1;
} else {
histograma[hex]++;
}
}
//recupera cor mais comum
var corMaisComum = null;
var frequenciaCorMaisComum = 0;
for (var cor in histograma) {
if (frequenciaCorMaisComum < histograma[cor]) {
corMaisComum = cor;
frequenciaCorMaisComum = histograma[cor];
}
}
console.log(corMaisComum);
The histogram is nothing more than a map type where the key is the color in hexadecimal notation and the value is the frequency with which it occurs in the image.
In this algorithm, I used the canvas
function to retrieve the color to round it to the nearest multiple of 5. Thus, slight variations in color will not disrupt the final result.
Note that with p histogram we can not only get the most common color, but also the first% w / o% most common colors. For this we would just have to create a vector with colors sorted by frequency.
The above code can be seen running on the jsdo.it site, but I'll post the same complete , if it becomes inaccessible one day:
//"arredonda" o número para o múltiplo de 5 mais próximo
//isso adiciona uma certa tolerância para os tons próximos
function arredonda(v) {
return 5 * (Math.round(v / 5));
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
//carrega uma imagem
var img = new Image();
img.src = 'http://jsrun.it/assets/8/O/3/U/8O3Ux.jpg';
img.onload = function() {
$(document.body).append(img);
//cria um canvas invisível
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext('2d');
//desenha a imagem no canvas
context.drawImage(img, 0, 0);
//recupera vetor de cores
var map = context.getImageData(0, 0, img.width, img.height).data;
var hex, r,g,b; //,alpha;
var histograma = {};
for (var i = 0, len = map.length; i < len; i += 4) {
//recupera componentes de um ponto
r = arredonda(map[i]);
g = arredonda(map[i+1]);
b = arredonda(map[i+2]);
//alpha = map[i+2]; //ignora canal alpha
//valor em hexadecimal
hex = rgbToHex(r, g, b);
//adiciona no histograma ou incrementa se já existir
if (histograma[hex] === undefined) {
histograma[hex] = 1;
} else {
histograma[hex]++;
}
}
//recupera cor mais comum
var corMaisComum = null;
var frequenciaCorMaisComum = 0;
for (var cor in histograma) {
if (frequenciaCorMaisComum < histograma[cor]) {
corMaisComum = cor;
frequenciaCorMaisComum = histograma[cor];
}
}
console.log(corMaisComum);
//adiciona um div como exemplo
$(document.body).append(
$('<div>').css({
'background-color': corMaisComum,
'width': '200px',
'height': '200px',
'border': '1px solid #000'
})
);
};
The visual result looks like the image below:
You can use the Canvas technology that is contained in browsers that support HTML5 (currently most), and with it you can do a JavaScript function that takes your image as a parameter and checks from 5 to 5 pixels which is the given color, and checks the entire image, and then the predominant color is set via R, G, B (red, green, blue) at the bottom of your page.
HTML Exemplary would be:
<span> Background é setado para a cor predominante desta imagem: </span>
<img id="i" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYI
DAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkF
BQkUDQsNFBQUFBQUFBQUFBQUFB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G4NzwVqiJ8NuDuWx4I6p7dRjlnNatT
22+wGJsKUxNiPp52pEdwLQtPqFDoRVQuNztAmsSeR3KHrdtEXa27IaSSmFXMfMk+t+wx
ULqDQOpdKSlRrzYLlbHknHLJirQD9CRgj3HSt7oTYzXe5E1tix6anvtrIBlvMlmOgeqn
VYT/AAzn0Bq25M2OB0WAK+Mi9RIySVODpRpWlrxRZLQxcbm3T9wBToWwF3U8SnhYX6/q
Gi4bOG23bC2V6RIebuWp5yAmZPSnCG0d/hNZ6hOepJ6qIBOMAD1b7boQNFaYnz5ToDEV
sq5QcFxXZKB7k4A+tfbdHfCy6Hs7su4z2oLABCeY5W4f3UJHVR9hVeG9O9Fx3fvYWpK4
dkjrKo0NR+ZR7fEcx0KseOwBwO5JFSMjM1uZ1z18N8z2HrKDNQqEpQJQMMWxW+FPc+s4
4S8XaTqC7zrpNVzy5r65DpHbmUc4HsOwrx1lijFW+lIQkJTsEUitRWoqUbkwUtJR0raN
IK6bRm5WqtvHi5py+zLUFHmUy0vmZWfVTaspJ9yK5kdqK5uNodTgcAI4HOOrbq2VBbai
DxGUP1B419x4jIbeTZpygMfEfirSo/8ABxI+1aq/8XO498aU2iZBtQV0KoMXr+rhXTNU
ULFIkArFqU9ILGt1Epwl9Vvv32x7LzebhqKeqddZ8m5TFd3pTpcVj0BPYewrx0UUWSlK
BhSLCA6lqWSpRuTC0Uho6Gto0j//2Q=="/>
Note: Converted your image into base64 :)
And the function that makes magic would be this:
var rgb = getAverageRGB(document.getElementById('i'));
document.body.style.backgroundColor = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')';
function getAverageRGB(imgEl) {
var blockSize = 5, //checa a cor à cada 5 pixels
defaultRGB = {r:0,g:0,b:0}, // para browsers com incompatibilidade
canvas = document.createElement('canvas'),
context = canvas.getContext && canvas.getContext('2d'),
data, width, height,
i = -4,
length,
rgb = {r:0,g:0,b:0},
count = 0; //zera o contador
if (!context) {
return defaultRGB;
}
height = canvas.height = imgEl.naturalHeight || imgEl.offsetHeight || imgEl.height;
width = canvas.width = imgEl.naturalWidth || imgEl.offsetWidth || imgEl.width;
context.drawImage(imgEl, 0, 0); //desenha a imagem cria o canvas
try {
data = context.getImageData(0, 0, width, height);
} catch(e) {
/* gera um erro de segurança, se a imagem for de dominio diferente */alert('x');
return defaultRGB;//retorna a cor padrão
}
length = data.data.length; //tamanho do array dos dados da imagem
while ( (i += blockSize * 4) < length ) {
++count; //incrementa o contador previamente zerado
rgb.r += data.data[i];
rgb.g += data.data[i+1];
rgb.b += data.data[i+2];
}
// ~~ usado para arrendondar valores para baixo
rgb.r = ~~(rgb.r/count);
rgb.g = ~~(rgb.g/count);
rgb.b = ~~(rgb.b/count);
return rgb;
}
The function is not my own, here is the Reference response in SOEN .