Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the color of my text based on the color of the ImageView it overlays?

So I've got transparant buttons with white text labels set up over a user uploaded ImageView. If the user uploads an image that is mostly white, then the buttons are hard to see if not completely invisible.

Does anyone know of a way to get the average color of a ImageView's source picture/drawable? If I can do this, I can compare it to a certain threshold I can trial and error for... If I can get this, then I can change the color of the text on my buttons to the inverted version of this color...or something??

Just idea spitting here..

And of course, if someone knows a better way, more info would be much appreciated, thanks!!

like image 839
Riptyde4 Avatar asked Oct 19 '22 06:10

Riptyde4


1 Answers

You could use the Palette class.

From the developers guide:

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor().

Palette.from() expects a Bitmap param, so you'll have to get it from your ImageView.

Something like this should work:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();

And then you can call the appropriate methods on this Palette instance to get the prominent colors, for example:

int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);

Check out this example screenshot on how the Palette class recognizes colors:

enter image description here

like image 99
earthw0rmjim Avatar answered Oct 31 '22 16:10

earthw0rmjim