From c49283d1cd63a1ba748ea7943d1239c3016a88de Mon Sep 17 00:00:00 2001 From: Sandeep Gupta Date: Mon, 15 Apr 2019 17:07:31 -0700 Subject: [PATCH] Retrieve all colors of the palette --- .../android/AndroidColorExtractor.java | 62 +++++++++++-------- .../colors/extract/android/Palette.java | 8 +++ 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/sangupta/colors/extract/android/AndroidColorExtractor.java b/src/main/java/com/sangupta/colors/extract/android/AndroidColorExtractor.java index 2550ce7..b33d7ff 100644 --- a/src/main/java/com/sangupta/colors/extract/android/AndroidColorExtractor.java +++ b/src/main/java/com/sangupta/colors/extract/android/AndroidColorExtractor.java @@ -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; @@ -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; @@ -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; } /** @@ -266,9 +275,16 @@ public static Swatch getSwatch(BufferedImage image) { AndroidColorExtractor extractor = new AndroidColorExtractor(new Bitmap(image)); Palette palette = extractor.generate(); - + Collection swatches = palette.getSeleectedSwatches(); + Swatch swatch = new Swatch<>(); - addToSwatch(swatch, palette.getVibrantColor()); + + for(PaletteSwatch ps : swatches) { + if(ps != null) { + addToSwatch(swatch, ps.getRgb()); + } + } + return swatch; } @@ -281,10 +297,6 @@ public static Swatch getSwatch(BufferedImage image) { * @param color */ private static void addToSwatch(Swatch swatch, int color) { - if (color < 0) { - return; - } - swatch.add(new RGB(color)); } } diff --git a/src/main/java/com/sangupta/colors/extract/android/Palette.java b/src/main/java/com/sangupta/colors/extract/android/Palette.java index 5376f85..1fc6fd3 100644 --- a/src/main/java/com/sangupta/colors/extract/android/Palette.java +++ b/src/main/java/com/sangupta/colors/extract/android/Palette.java @@ -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; @@ -89,6 +90,13 @@ public List getSwatches() { return Collections.unmodifiableList(this.swatches); } + /** + * Returns all of the swatches which make up the palette. + */ + public Collection getSeleectedSwatches() { + return this.selectedSwatches.values(); + } + /** * Returns the targets used to generate this palette. */