Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!