K. Sopheak K. Sopheak - 1 month ago 8
Android Question

How to detect bitmap picture contain muted or vibrant color?

I use

pallete
class to set random background color to
textview
, and some images cannot generate color and return color like gray. I find that some image file work with
getMutedColor
and some other work with
getVibrantColor
and others.

Here is my layout. I use
ImageView
to show bitmap and a
TextView
below it to show name with random background.

How can we detect whether image file work with `getMutedColor` or `getVibrantColor` or others?



Here is my code I try so far:

public void generateColor(Bitmap mypic){
mImageView.setImageBitmap(result);
Palette p = Palette.from(result).generate();
mTextView.setBackgroundColor(p.getVibrantColor(default_color));
}


I am appreciate for any help.

Answer

I'd simply check for null.

public int getSwatch(Bitmap b){
    Palette p = Palette.from(b).generate();
    Palette.Swatch swatch;
    if((swatch = p.getVibrantSwatch()) != null){
        return swatch.getRgb();
    }
    if((swatch = p.getLightMutedSwatch()) != null){
        return swatch.getRgb();
    }
    .
    .
    .
    return Color.WHITE;
}

Color.WHITE at the end is the fallback color. Should Palette be unable to find a color, you will still get WHITE as a result. You can swap it with any color you wish.

Comments