I would like to be able to use the same drawable to represent both:
and
as the same drawable, and re-color the drawable based on some programmatic values, so that the end user could re-theme the interface.
What is the best way to do this? I have tried (and reused the icons from) this previous S.O. question but I can’t represent the change as a simple change of hue, since it also varies in saturation and value..
Is it best to store the icon as all white in the area I want changed? or transparent? or some other solid color?
Is there some method that allows you to figure out the matrix based on the difference between Color of red_icon and Color of blue_icon?
So after a lot of trial and error, reading different articles, and most importantly, going through the API Demos (ColorFilters.java — found in com.example.android.apis.graphics) I found the solution.
For solid images, I have found it is best to use the color filter PorterDuff.Mode.SRC_ATOP because it will overlay the color on top of the source image, allowing you to change the color to the exact color you are looking for.
For images that are more complex, like the one above, I have found the best thing to do is to color the entire image WHITE (FFFFFF) so that when you do PorterDuff.Mode.MULTIPLY, you end up with the correct colors, and all of the black (000000) in your image will remain black.
The colorfilters.java shows you how it’s done if your drawing on a canvas, but if all you need is to color a drawable then this will work:
I created a demo activity using some of the API Demo code to swap between every color filter mode to try them out for different situations and have found it to be invaluable, so I thought I would post it here.
The two other dependencies, GraphicsActivity.java and PictureLayout.java, can be copied directly from the API Demos activity if you would like to test it out.