JoshuaDavis icon

JAVA / remove duplicate colors

JoshuaDavis | PRO | 09/22/21 09:28:18 PM UTC (Edited) | 0 ⭐ | 2402 👁️ | Never ⏰ | []
Java |

1.73 KB

|

None

|

0 👍

/

0 👎

int     stageW     = 1920;
int     stageH     = 1080;
color   clrBG      = #CCCCCC;
String  pathDATA   = "../../data/";
 
// ********************************************************************************************************************
 
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
PImage  mySourceImg;
IntList usedColors = new IntList();
int     swatchSize = 40;
 
// ********************************************************************************************************************
 
void settings() {
    size(stageW, stageH, P3D);
}
 
void setup() {
    background(clrBG);
    mySourceImg = loadImage(pathDATA + "source.gif"); // load in image
    mySourceImg.loadPixels(); // load in all the pixels of the image in this case, 1,166,400 pixels
    removeDuplicateColors(); // grab all the color pixels in the image and remove duplicates, build a new array
}
 
void draw() {
    background(clrBG);
 
    image(mySourceImg, 0, 0, stageW, stageH); // show image on screen
 
    // show swatches of ONLY unique colors
    for (int i = 0; i < usedColors.size(); ++i) {
        strokeWeight(0);
        noStroke();
        fill(usedColors.get(i));
        rect(i*swatchSize, 0, (swatchSize-1), 100);
    }
}
 
// ********************************************************************************************************************
 
void removeDuplicateColors() {
    List<Integer> colors = new ArrayList<Integer>();
    for (int i = 0; i < mySourceImg.pixels.length; ++i) colors.add(mySourceImg.pixels[i]);
 
    Set<Integer> colorsWithoutDuplicates = new LinkedHashSet<Integer>(colors);
    colors.clear();
    colors.addAll(colorsWithoutDuplicates);
 
    for (int j = 0; j < colors.size(); ++j) usedColors.append( colors.get(j) );
 
    println( usedColors );
}
 

Comments