Advertisement
JoshuaDavis

JAVA / remove duplicate colors

Sep 22nd, 2021 (edited)
2,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. int     stageW     = 1920;
  2. int     stageH     = 1080;
  3. color   clrBG      = #CCCCCC;
  4. String  pathDATA   = "../../data/";
  5.  
  6. // ********************************************************************************************************************
  7.  
  8. import java.util.LinkedHashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11.  
  12. PImage  mySourceImg;
  13. IntList usedColors = new IntList();
  14. int     swatchSize = 40;
  15.  
  16. // ********************************************************************************************************************
  17.  
  18. void settings() {
  19.     size(stageW, stageH, P3D);
  20. }
  21.  
  22. void setup() {
  23.     background(clrBG);
  24.     mySourceImg = loadImage(pathDATA + "source.gif"); // load in image
  25.     mySourceImg.loadPixels(); // load in all the pixels of the image in this case, 1,166,400 pixels
  26.     removeDuplicateColors(); // grab all the color pixels in the image and remove duplicates, build a new array
  27. }
  28.  
  29. void draw() {
  30.     background(clrBG);
  31.  
  32.     image(mySourceImg, 0, 0, stageW, stageH); // show image on screen
  33.  
  34.     // show swatches of ONLY unique colors
  35.     for (int i = 0; i < usedColors.size(); ++i) {
  36.         strokeWeight(0);
  37.         noStroke();
  38.         fill(usedColors.get(i));
  39.         rect(i*swatchSize, 0, (swatchSize-1), 100);
  40.     }
  41. }
  42.  
  43. // ********************************************************************************************************************
  44.  
  45. void removeDuplicateColors() {
  46.     List<Integer> colors = new ArrayList<Integer>();
  47.     for (int i = 0; i < mySourceImg.pixels.length; ++i) colors.add(mySourceImg.pixels[i]);
  48.  
  49.     Set<Integer> colorsWithoutDuplicates = new LinkedHashSet<Integer>(colors);
  50.     colors.clear();
  51.     colors.addAll(colorsWithoutDuplicates);
  52.  
  53.     for (int j = 0; j < colors.size(); ++j) usedColors.append( colors.get(j) );
  54.  
  55.     println( usedColors );
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement