Skip to content

Commit

Permalink
Retrieve all colors of the palette
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Apr 16, 2019
1 parent 9aa486e commit c49283d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.sangupta.colors.Swatch;
Expand All @@ -35,6 +36,8 @@
* @since 1.0.0
*/
public class AndroidColorExtractor {

static final int MAX_WIDTH = 1200;

static final int DEFAULT_RESIZE_BITMAP_AREA = 112 * 112;

Expand Down Expand Up @@ -198,27 +201,33 @@ private int[] getPixelsFromBitmap(Bitmap bitmap) {
* Scale the bitmap down as needed.
*/
private Bitmap scaleBitmapDown(final Bitmap bitmap) {
double scaleRatio = -1;

if (this.resizeArea > 0) {
final int bitmapArea = bitmap.getWidth() * bitmap.getHeight();
if (bitmapArea > this.resizeArea) {
scaleRatio = Math.sqrt(this.resizeArea / (double) bitmapArea);
}
} else if (this.resizeMaxDimension > 0) {
final int maxDimension = Math.max(bitmap.getWidth(), bitmap.getHeight());
if (maxDimension > this.resizeMaxDimension) {
scaleRatio = this.resizeMaxDimension / (double) maxDimension;
}
}

if (scaleRatio <= 0) {
// Scaling has been disabled or not needed so just return the Bitmap
return bitmap;
// double scaleRatio = -1;
//
// if (this.resizeArea > 0) {
// final int bitmapArea = bitmap.getWidth() * bitmap.getHeight();
// if (bitmapArea > this.resizeArea) {
// scaleRatio = Math.sqrt(this.resizeArea / (double) bitmapArea);
// }
// } else if (this.resizeMaxDimension > 0) {
// final int maxDimension = Math.max(bitmap.getWidth(), bitmap.getHeight());
// if (maxDimension > this.resizeMaxDimension) {
// scaleRatio = this.resizeMaxDimension / (double) maxDimension;
// }
// }
//
// if (scaleRatio <= 0) {
// // Scaling has been disabled or not needed so just return the Bitmap
// return bitmap;
// }

if(bitmap.getWidth() > MAX_WIDTH) {
double scale = ((double) MAX_WIDTH) / bitmap.getWidth();
double scaledHeight = bitmap.getHeight() * scale;

return Bitmap.createScaledBitmap(bitmap, MAX_WIDTH, Double.valueOf(scaledHeight).intValue());
}

return Bitmap.createScaledBitmap(bitmap, (int) Math.ceil(bitmap.getWidth() * scaleRatio),
(int) Math.ceil(bitmap.getHeight() * scaleRatio));
return bitmap;
}

/**
Expand Down Expand Up @@ -266,9 +275,16 @@ public static Swatch<RGB> getSwatch(BufferedImage image) {

AndroidColorExtractor extractor = new AndroidColorExtractor(new Bitmap(image));
Palette palette = extractor.generate();

Collection<PaletteSwatch> swatches = palette.getSeleectedSwatches();

Swatch<RGB> swatch = new Swatch<>();
addToSwatch(swatch, palette.getVibrantColor());

for(PaletteSwatch ps : swatches) {
if(ps != null) {
addToSwatch(swatch, ps.getRgb());
}
}


return swatch;
}
Expand All @@ -281,10 +297,6 @@ public static Swatch<RGB> getSwatch(BufferedImage image) {
* @param color
*/
private static void addToSwatch(Swatch<RGB> swatch, int color) {
if (color < 0) {
return;
}

swatch.add(new RGB(color));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.sangupta.colors.extract.android;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -89,6 +90,13 @@ public List<PaletteSwatch> getSwatches() {
return Collections.unmodifiableList(this.swatches);
}

/**
* Returns all of the swatches which make up the palette.
*/
public Collection<PaletteSwatch> getSeleectedSwatches() {
return this.selectedSwatches.values();
}

/**
* Returns the targets used to generate this palette.
*/
Expand Down

0 comments on commit c49283d

Please sign in to comment.