Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down
53 changes: 31 additions & 22 deletions src/main/java/de/androidpit/colorthief/ColorThief.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class ColorThief {

private static final int MIN_QUALITY = 1;
private static final int DEFAULT_QUALITY = 10;
private static final boolean DEFAULT_IGNORE_WHITE = true;

Expand Down Expand Up @@ -54,9 +55,9 @@ public static int[] getColor(BufferedImage sourceImage) {
* @param sourceImage
* the source image
* @param quality
* 1 is the highest quality settings. 10 is the default. There is a trade-off between
* quality and speed. The bigger the number, the faster a color will be returned but
* the greater the likelihood that it will not be the visually most dominant color.
* 1 is the highest quality setting. 10 is the default. There is a trade-off between
* quality and speed. Larger values sample fewer pixels and can return a color faster,
* but increase the likelihood that it will not be the visually most dominant color.
* @param ignoreWhite
* if <code>true</code>, white pixels are ignored
*
Expand All @@ -79,9 +80,11 @@ public static int[] getColor(BufferedImage sourceImage, int quality, boolean ign
* @param sourceImage
* the source image
* @param colorCount
* the size of the palette; the number of colors returned
* the size of the palette; the number of colors returned (minimum 2, maximum 256)
*
* @return the palette as array of RGB arrays
* @throws IllegalArgumentException
* if colorCount is outside the range 2..256
*/
public static int[][] getPalette(BufferedImage sourceImage, int colorCount) {
CMap cmap = getColorMap(sourceImage, colorCount);
Expand All @@ -97,17 +100,17 @@ public static int[][] getPalette(BufferedImage sourceImage, int colorCount) {
* @param sourceImage
* the source image
* @param colorCount
* the size of the palette; the number of colors returned
* the size of the palette; the number of colors returned (minimum 2, maximum 256)
* @param quality
* 1 is the highest quality settings. 10 is the default. There is a trade-off between
* quality and speed. The bigger the number, the faster the palette generation but
* the greater the likelihood that colors will be missed.
* 1 is the highest quality setting. 10 is the default. There is a trade-off between
* quality and speed. Larger values sample fewer pixels and make palette generation
* faster, but increase the likelihood that colors will be missed.
* @param ignoreWhite
* if <code>true</code>, white pixels are ignored
*
* @return the palette as array of RGB arrays
* @throws IllegalArgumentException
* if quality is &lt; 1
* if colorCount is outside the range 2..256 or quality is &lt; 1
*/
public static int[][] getPalette(
BufferedImage sourceImage,
Expand All @@ -130,6 +133,8 @@ public static int[][] getPalette(
* the size of the palette; the number of colors returned (minimum 2, maximum 256)
*
* @return the color map
* @throws IllegalArgumentException
* if colorCount is outside the range 2..256
*/
public static CMap getColorMap(BufferedImage sourceImage, int colorCount) {
return getColorMap(sourceImage, colorCount, DEFAULT_QUALITY, DEFAULT_IGNORE_WHITE);
Expand All @@ -143,15 +148,15 @@ public static CMap getColorMap(BufferedImage sourceImage, int colorCount) {
* @param colorCount
* the size of the palette; the number of colors returned (minimum 2, maximum 256)
* @param quality
* 1 is the highest quality settings. 10 is the default. There is a trade-off between
* quality and speed. The bigger the number, the faster the palette generation but
* the greater the likelihood that colors will be missed.
* 1 is the highest quality setting. 10 is the default. There is a trade-off between
* quality and speed. Larger values sample fewer pixels and make palette generation
* faster, but increase the likelihood that colors will be missed.
* @param ignoreWhite
* if <code>true</code>, white pixels are ignored
*
* @return the color map
* @throws IllegalArgumentException
* if quality is &lt; 1
* if colorCount is outside the range 2..256 or quality is &lt; 1
*/
public static CMap getColorMap(
BufferedImage sourceImage,
Expand All @@ -161,9 +166,7 @@ public static CMap getColorMap(
if (colorCount < 2 || colorCount > 256) {
throw new IllegalArgumentException("Specified colorCount must be between 2 and 256.");
}
if (quality < 1) {
throw new IllegalArgumentException("Specified quality should be greater then 0.");
}
validateQuality(quality);

int[][] pixelArray;

Expand All @@ -189,9 +192,9 @@ public static CMap getColorMap(
* @param sourceImage
* the source image
* @param quality
* 1 is the highest quality settings. 10 is the default. There is a trade-off between
* quality and speed. The bigger the number, the faster the palette generation but
* the greater the likelihood that colors will be missed.
* 1 is the highest quality setting. 10 is the default. There is a trade-off between
* quality and speed. Larger values sample fewer pixels and make palette generation
* faster, but increase the likelihood that colors will be missed.
* @param ignoreWhite
* if <code>true</code>, white pixels are ignored
*
Expand Down Expand Up @@ -285,9 +288,9 @@ private static int[][] getPixelsFast(
* @param sourceImage
* the source image
* @param quality
* 1 is the highest quality settings. 10 is the default. There is a trade-off between
* quality and speed. The bigger the number, the faster the palette generation but
* the greater the likelihood that colors will be missed.
* 1 is the highest quality setting. 10 is the default. There is a trade-off between
* quality and speed. Larger values sample fewer pixels and make palette generation
* faster, but increase the likelihood that colors will be missed.
* @param ignoreWhite
* if <code>true</code>, white pixels are ignored
*
Expand Down Expand Up @@ -328,4 +331,10 @@ private static int[][] getPixelsSlow(
return Arrays.copyOfRange(res, 0, numUsedPixels);
}

private static void validateQuality(int quality) {
if (quality < MIN_QUALITY) {
throw new IllegalArgumentException("Specified quality must be at least " + MIN_QUALITY + ".");
}
}

}
9 changes: 6 additions & 3 deletions src/main/java/de/androidpit/colorthief/MMCQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,22 @@ private static VBox vboxFromPixels(int[][] pixels, int[] histo) {

if (rval < rmin) {
rmin = rval;
} else if (rval > rmax) {
}
if (rval > rmax) {
rmax = rval;
}

if (gval < gmin) {
gmin = gval;
} else if (gval > gmax) {
}
if (gval > gmax) {
gmax = gval;
}

if (bval < bmin) {
bmin = bval;
} else if (bval > bmax) {
}
if (bval > bmax) {
bmax = bval;
}
}
Expand Down
Loading