Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Quantize an image with 8 selected colors (JavaScript)

I am writing a code that takes an image and quantizes it with the 8 selected colors that I have chosen. What I am wanting the program to do is to find a color that is closest with one of the 8 colors that I have chosen in my array and replace that pixel with that color that I have in my array. The thing is I currently have the quantized image in grayscale and I am having difficulties getting it to display color. What I have tried is that I used the distance function to get the distance of how far the color on the image is to the pixel of the image. And with that, my code still does not seem to work. I have also tried replacing the RGBA values with the functions but that did not work either. If there are any suggestions to solving my problem please give me an answer!

This is my code:

for(let x = 0; x < image.width; x++){
    for(let y = 0; y < image.height; y++){

        let i = y * image.width + x;

    
        let i_r = i*4+0;
        let i_g = i*4+1;
        let i_b = i*4+2;

        
        let L = 0.2126*rgba[i_r] + 0.7152*rgba[i_g] + 0.0722*rgba[i_b];


        let palette = [ [240, 242,254], [179, 168, 164], [121,192, 246], [103,185,183], [255,201,157], [215,134,94], [67,38,158], [165,104,57]];

        let qL = (L/255) * (palette.length - 1);


        qL = Math.round(qL);

        qL = qL/(palette.length - 1) * 255;

        rgba[i_r] = qL;
        rgba[i_g] = qL;
        rgba[i_b] = qL;

        

    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This might be a fix for your issue:

for(let x = 0; x < image.width; x++){
    for(let y = 0; y < image.height; y++){

        let i = y * image.width + x;

    
        let i_r = i*4+0;
        let i_g = i*4+1;
        let i_b = i*4+2;

        
        let L = 0.2126*rgba[i_r] + 0.7152*rgba[i_g] + 0.0722*rgba[i_b];


        
        // this line should be improved
        let qL = (L/255) * (palette.length - 1);


        qL = Math.round(qL);

        // fix - remove this line
        // qL = qL/(palette.length - 1) * 255;

        // fix
        rgba[i_r] = palette[qL][0];
        rgba[i_g] = palette[qL][1];
        rgba[i_b] = palette[qL][2];
    }
}

Please take a look on enter image description here

graph.add(function(x) { return Math.round(8*x/255.0); });

As you see, the first section (values mapped to 0) has only 16 values on x-axis, the next one has 32... The last one has 16 values too and it's mapped to value 8 - so guess app will fail here (as max index of palette should be 7). Maybe you would like to improve this.

You may need something like (note x - 16 in eq)

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda