I'm trying to solve a problem with android ColorFilters
. Documentation is very poor, so the main method is trying different variants.
The problem:
There is a Bitmap. Some pixels have alpha=255, others have alpha=0. I'm trying to draw a circle with a specific color. I want alpha channel unchanged in the bitmap, but while drawing I want to multiply a color to the bitmap-alpha.
So, while drawing a circle I want pixels with alpha=0 to be not painted, but pixels with alpha=255 to be painted in color which I want. Alpha channel shouldn't change.
I'm trying to use porter-duff ColorFilter (PorterDuffColorFilter class in android sdk).
there are too many modes and no-understandable description on official site here : http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html
I think I should use DST_ATOP
or SRC_ATOP
, but they don't work as I described.
Also, there is a strange parameter srcColor
in constructor of porter-duff colorfilter.
I can't understand what "Sa" and "Sc" means in formulas [Da, Sc * Da + (1 - Sa) * Dc]
. It can be from color which was passed into colorfilter constructor and also it can be color set by "paint.setColor".
Anybody knows, how it works?
Sa
and Sc
are shorts for "source alpha" and "source color", respectively. The srcColor
parameter in the PorterDuffColorFilter
constructor is the color used for these values.
For your case the Mode.MULTIPLY
would probably work best.
Note that Porter-Duff modes are only defined to work properly with premultiplied alpha. That means that none of the R, G or B components can exceed the alpha value.
GitHub project for the Android project which shows off all the Porter-Duff modes. The Android App is also available on Playstore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With