stevesketch
8/20/2016 - 11:34 PM

#PaletteTools

// USAGE
// sorts an array of colors and spits out a new array
// palette = sortColoursB(palette); // by brightness
// palette = sortColoursH(palette); // by hue

import java.util.*;
import java.io.*;

color[]  sortColoursH(color[] pal) {
  int[] bri = new int[pal.length]; 

  for (int i = 0; i < pal.length; i++) {  
    bri[i] = int(hue(pal[i]));
  }
  //println("Bri Array: "+ Arrays.toString(bri) );
  ArrayIndexComparator comparator = new ArrayIndexComparator(bri);
  Integer[] indexref = comparator.createIndexArray();
  Arrays.sort(indexref, comparator);
  color[] output = new color[pal.length];
  //println("Index SORTED: "+ Arrays.toString(indexref) );
  for (int i = 0; i < pal.length; i++) {  
    output[i] = pal[indexref[i]];
  }
  return output;
}

color[]  sortColoursB(color[] pal) {
  int[] bri = new int[pal.length]; 

  for (int i = 0; i < pal.length; i++) {  
    bri[i] = int(getBrightness(pal[i]));
  }
  //println("Bri Array: "+ Arrays.toString(bri) );
  ArrayIndexComparator comparator = new ArrayIndexComparator(bri);
  Integer[] indexref = comparator.createIndexArray();
  Arrays.sort(indexref, comparator);
  color[] output = new color[pal.length];
  //println("Index SORTED: "+ Arrays.toString(indexref) );
  for (int i = 0; i < pal.length; i++) {  
    output[i] = pal[indexref[i]];
  }
  return output;
}

public class ArrayIndexComparator implements Comparator<Integer>
{
  private final int[] array;

  public ArrayIndexComparator(int[] array)
  {
    this.array = array;
  }

  public Integer[] createIndexArray()
  {
    Integer[] indexes = new Integer[array.length];
    for (int i = 0; i < array.length; i++)
    {
      indexes[i] = i; // Autoboxing
    }
    return indexes;
  }

  @Override
    public int compare(Integer index1, Integer index2)
  {
    // Autounbox from Integer to int to use as array indexes
    return Integer.compare(array[index1], array[index2]);
  }
}

public class GenericExtFilter implements FilenameFilter {
  private String[] exts;

  public GenericExtFilter(String... exts) {
    this.exts = exts;
  }

  @Override
    public boolean accept(File dir, String name) {
    for (String ext : exts) {
      if (name.endsWith(ext)) {
        return true;
      }
    }

    return false;
  }
}
color matchBestHB(color[] pal, color col, float randomizer) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  int nextnearest = 0;
  float bestDistanceFoundYet = 1;
  float HBav = (hue(col)+brightness(col))/2;


  for (int i=0; i<pal.length; i++) {
    allB[i] = (hue(pal[i])+brightness(pal[i]))/2;
  }
  for (int i = 0; i < allB.length; i++) {
    
    if (allB[i] == HBav) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(HBav - allB[i]);
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
         nextnearest = constrain(nearest,0,allB.length);
        nearest = i;
      }
      
    }
  }
  //if (randomizer < palaltPercent) return pal[nextnearest];
  return pal[nearest];
}

color matchBestHS(color[] pal, color col, float randomizer) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  int nextnearest = 0;
  float bestDistanceFoundYet = 1;
float Hsav = (saturation(col)+hue(col))/2;


  for (int i=0; i<pal.length; i++) {
    allB[i] = (saturation(pal[i])+hue(pal[i]))/2;
  }
  for (int i = 0; i < allB.length; i++) {
    if (allB[i] == Hsav) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(Hsav - allB[i]);
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
        nextnearest = constrain(nearest,0,allB.length);
        nearest = i;
      }
      
    }
  }
  
  return pal[nearest];
}

color matchBestBS(color[] pal, color col, float randomizer) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  int nextnearest = 0;
  float bestDistanceFoundYet = 1;
  float HBav = (saturation(col)+brightness(col))/2;


  for (int i=0; i<pal.length; i++) {
    allB[i] = (saturation(pal[i])+brightness(pal[i]))/2;
  }
  for (int i = 0; i < allB.length; i++) {
    if (allB[i] == HBav) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(HBav - allB[i]);
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
        nextnearest = constrain(nearest,0,allB.length);
        nearest = i;
      }
      
    }
  }
  
  return pal[nearest];
}

color matchBestAll(color[] pal, color col) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  float bestDistanceFoundYet = 1;
  float HBSav = (hue(col)+brightness(col)+saturation(col))/3;

  for (int i=0; i<pal.length; i++) {
    allB[i] = (hue(pal[i])+brightness(pal[i])+saturation(pal[i]))/3;
  }
  for (int i = 0; i < allB.length; i++) {
    if (allB[i] == HBSav) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(HBSav - allB[i]);
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
        nearest = i;
      }
    }
  }
  return pal[nearest];
}

color matchBestH(color[] pal, color col, float randomizer) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  int nextnearest = 0;
  float bestDistanceFoundYet = 1;

  for (int i=0; i<pal.length; i++) {
    allB[i] = hue(pal[i]);
  }
  for (int i = 0; i < allB.length; i++) {
    //if (saturation(pal[i]) < 10) continue;
    if (allB[i] == hue(col)) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(hue(col) - allB[i]);
      
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
        nextnearest = constrain(nearest,0,allB.length);
        nearest = i;
      }
      
    }
  }
  
  return pal[nearest];
}

color matchBestB(color[] pal, color col, float randomizer) {
  int ind = 0;
  float[] allB = new float[pal.length];
  int nearest = -1;
  int nextnearest = 0;
  float bestDistanceFoundYet = 1;

  for (int i=0; i<pal.length; i++) {
    allB[i] = brightness(pal[i]);
  }
  for (int i = 0; i < allB.length; i++) {
    if (allB[i] == brightness(col)) {
      return pal[i];
    } else {
      // else, we consider the difference between the desired number and the current number in the array.
      float d = abs(brightness(col) - allB[i]);
      if (d < bestDistanceFoundYet) {
        bestDistanceFoundYet = d; 
        nextnearest = constrain(nearest,0,allB.length);
        nearest = i;
      }
    }
  }
  
  return pal[nearest];
}