Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ColorFilter - Porter-Duff Modes

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?

like image 754
Alexander Taran Avatar asked Apr 20 '12 11:04

Alexander Taran


2 Answers

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.

like image 138
Jave Avatar answered Sep 28 '22 02:09

Jave


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.

like image 40
Lawrence D'Oliveiro Avatar answered Sep 28 '22 02:09

Lawrence D'Oliveiro