Class Palette
java.lang.Object
dev.lukebemish.dynamicassetgenerator.api.colors.Palette
- All Implemented Interfaces:
Iterable<Integer>,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 Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDefault cutoff for fuzzy equality. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int color) booleanbooleanaddAll(Collection<? extends Integer> colors) voidclear()booleanbooleancontainsAll(@NonNull Collection<?> collection) doubledistanceToPolyLine(int color, ColorCoordinates coordinates) voidExtends the high and low ends of the palette until it satisfies a given predicate.intextendedToOriginal(int extendedSample) voidextendToSize(int targetSize) Extends the high and low ends of the palette until it contains a given number of colors.voidextendToWidth(int targetWidth) Extends the high and low ends of the palette until it stretches to a given size.static PalettefromColors(Collection<Integer> colors, double cutoff) static PalettefromPalette(Palette palette) static PalettefromPalette(Palette palette, double cutoff) intintgetClosestColor(int color) intgetColor(int sample) intgetColorFromIndex(int index) doubleintgetSample(int color) booleanisEmpty()iterator()intintintintintintintoriginalToExtended(int originalSample) booleanbooleanremoveAll(@NonNull Collection<?> collection) booleanretainAll(@NonNull Collection<?> collection) intsize()Object @NonNull []toArray()<T> T @NonNull []toArray(T @NonNull [] ts) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
DEFAULT_CUTOFF
public static final double DEFAULT_CUTOFFDefault 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
- Returns:
- a new palette with the given cutoff and colors
-
fromPalette
- Returns:
- a new palette with the given cutoff and colors from the provided palette
-
fromPalette
- Returns:
- a new palette with colors and cutoff copied from the provided palette
-
getCutoff
public double getCutoff()- Returns:
- the cutoff for fuzzy equality
-
add
- Specified by:
addin interfaceCollection<Integer>
-
add
public boolean add(int color) -
addAll
- Specified by:
addAllin interfaceCollection<Integer>
-
removeAll
- Specified by:
removeAllin interfaceCollection<Integer>
-
retainAll
- Specified by:
retainAllin interfaceCollection<Integer>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<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
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:
sizein interfaceCollection<Integer>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<Integer>
-
contains
- Specified by:
containsin interfaceCollection<Integer>
-
iterator
-
toArray
- Specified by:
toArrayin interfaceCollection<Integer>
-
toArray
public <T> T @NonNull [] toArray(T @NonNull [] ts) - Specified by:
toArrayin interfaceCollection<Integer>
-
remove
- Specified by:
removein interfaceCollection<Integer>
-
containsAll
- Specified by:
containsAllin interfaceCollection<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
- Returns:
- the minimum distance between the given color and line segments formed between consecutive colors in the palette, in the given color coordinates
-