java.lang.Object
dev.lukebemish.dynamicassetgenerator.api.colors.Palette
All Implemented Interfaces:
Iterable<Integer>, Collection<Integer>

public class Palette extends Object implements Collection<Integer>
A collection of RGB24 colors sorted from lightest to darkest, with a cutoff for fuzzy equality. Can be "extended" to a given range by creating new colors at the endpoints as needed. Provides transforms between palette colors and "sample numbers", which are integers in the range [0, 255] corresponding to the position of a color in the palette. A palette can contain any number of colors, but the sample numbers will always be in the range [0, 255] - meaning that adding more than 256 colors will be meaningless.
  • Field Details

    • DEFAULT_CUTOFF

      public static final double DEFAULT_CUTOFF
      Default cutoff for fuzzy equality.
      See Also:
  • Constructor Details

    • Palette

      public Palette()
      Creates a new palette with the default cutoff.
    • Palette

      public Palette(double cutoff)
      Creates a new palette with the given cutoff.
      Parameters:
      cutoff - cutoff for fuzzy equality; measures Euclidean distance in RGB24 space
  • Method Details

    • fromColors

      public static Palette fromColors(Collection<Integer> colors, double cutoff)
      Returns:
      a new palette with the given cutoff and colors
    • fromPalette

      public static Palette fromPalette(Palette palette, double cutoff)
      Returns:
      a new palette with the given cutoff and colors from the provided palette
    • fromPalette

      public static Palette fromPalette(Palette palette)
      Returns:
      a new palette with colors and cutoff copied from the provided palette
    • getCutoff

      public double getCutoff()
      Returns:
      the cutoff for fuzzy equality
    • add

      public boolean add(Integer color)
      Specified by:
      add in interface Collection<Integer>
    • add

      public boolean add(int color)
    • addAll

      public boolean addAll(Collection<? extends Integer> colors)
      Specified by:
      addAll in interface Collection<Integer>
    • removeAll

      public boolean removeAll(@NonNull Collection<?> collection)
      Specified by:
      removeAll in interface Collection<Integer>
    • retainAll

      public boolean retainAll(@NonNull Collection<?> collection)
      Specified by:
      retainAll in interface Collection<Integer>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<Integer>
    • extendToWidth

      public void extendToWidth(int targetWidth)
      Extends the high and low ends of the palette until it stretches to a given size.
      Parameters:
      targetWidth - the target Euclidean distance between the lowest and highest colors in the palette
      Throws:
      IllegalStateException - if the palette is empty
    • extendToSize

      public void extendToSize(int targetSize)
      Extends the high and low ends of the palette until it contains a given number of colors.
      Parameters:
      targetSize - the target number of colors in the palette
      Throws:
      IllegalStateException - if the palette is empty
    • extend

      public void extend(Predicate<Palette> isExtended)
      Extends the high and low ends of the palette until it satisfies a given predicate. Extension may be impossible if the palette's entries are too close together and the cutoff for equivalent colors is too broad. If the predicate is never satisfied, extension will stop when 0xFFFFFF and 0x000000 are reached.
      Parameters:
      isExtended - predicate to test whether the palette is extended enough
      Throws:
      IllegalStateException - if the palette is empty
    • originalSize

      public int originalSize()
      Returns:
      the number of colors in the palette before it was extended
    • size

      public int size()
      Specified by:
      size in interface Collection<Integer>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<Integer>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<Integer>
    • iterator

      public @NonNull Iterator<Integer> iterator()
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface Iterable<Integer>
    • toArray

      public Object @NonNull [] toArray()
      Specified by:
      toArray in interface Collection<Integer>
    • toArray

      public <T> T @NonNull [] toArray(T @NonNull [] ts)
      Specified by:
      toArray in interface Collection<Integer>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<Integer>
    • containsAll

      public boolean containsAll(@NonNull Collection<?> collection)
      Specified by:
      containsAll in interface Collection<Integer>
    • originalStart

      public int originalStart()
      Returns:
      the index of the first color in the palette before it was extended
      Throws:
      IllegalStateException - if the palette is empty
    • originalEnd

      public int originalEnd()
      Returns:
      the index of the last color in the palette before it was extended
      Throws:
      IllegalStateException - if the palette is empty
    • originalCenterSample

      public int originalCenterSample()
      Returns:
      a sample number pointing towards the center of the palette before it was extended
      Throws:
      IllegalStateException - if the palette is empty
    • originalStartSample

      public int originalStartSample()
      Returns:
      a sample number pointing towards the start of the palette before it was extended
      Throws:
      IllegalStateException - if the palette is empty
    • originalEndSample

      public int originalEndSample()
      Returns:
      a sample number pointing towards the end of the palette before it was extended
      Throws:
      IllegalStateException - if the palette is empty
    • originalToExtended

      public int originalToExtended(int originalSample)
      Returns:
      a sample number in the extended palette pointing towards the same color that the provided sample number would have before the palette was extended
      Throws:
      IllegalStateException - if the palette is empty
    • extendedToOriginal

      public int extendedToOriginal(int extendedSample)
      Returns:
      a sample number in the original palette pointing towards the same color that the provided sample number would have in the extended palette
      Throws:
      IllegalStateException - if the palette is empty
    • getSample

      public int getSample(int color)
      Returns:
      a sample number pointing towards where the provided color would lie in the palette
    • getColor

      public int getColor(int sample)
      Returns:
      the color in the palette closes to the given sample number
      Throws:
      IllegalArgumentException - if the sample number is not between 0 and 255
    • getColorFromIndex

      public int getColorFromIndex(int index)
      Returns:
      the color in the palette at the given index
    • getClosestColor

      public int getClosestColor(int color)
      Returns:
      the color in the palette closest to the given color
    • getAverage

      public int getAverage()
      Returns:
      the average color of the palette
    • distanceToPolyLine

      public double distanceToPolyLine(int color, ColorCoordinates coordinates)
      Returns:
      the minimum distance between the given color and line segments formed between consecutive colors in the palette, in the given color coordinates