cmsdk: Add sanity checks in ColorUtils

* Protect against nulls when generating colors from icons.

Change-Id: I0ba9540848f7be485e713301f2c6c804bd4522e1
This commit is contained in:
Steve Kondik 2016-02-19 19:12:03 +01:00
parent 6ad5263eb3
commit ccce292498
1 changed files with 12 additions and 2 deletions

View File

@ -237,6 +237,9 @@ public class ColorUtils {
* @return the dominant Swatch
*/
public static Palette.Swatch getDominantSwatch(Palette palette) {
if (palette == null || palette.getSwatches().size() == 0) {
return null;
}
// find most-represented swatch based on population
return Collections.max(palette.getSwatches(), new Comparator<Palette.Swatch>() {
@Override
@ -273,10 +276,17 @@ public class ColorUtils {
if (bitmap != null) {
Palette p = Palette.from(bitmap).generate();
if (p == null) {
return alertColor;
}
// First try the dominant color
int iconColor = getDominantSwatch(p).getRgb();
alertColor = findPerceptuallyNearestSolidColor(iconColor);
final Palette.Swatch dominantSwatch = getDominantSwatch(p);
int iconColor = alertColor;
if (dominantSwatch != null) {
iconColor = dominantSwatch.getRgb();
alertColor = findPerceptuallyNearestSolidColor(iconColor);
}
// Try the most saturated color if we got white or black (boring)
if (alertColor == Color.BLACK || alertColor == Color.WHITE) {