简体   繁体   English

Android ColorFilter - Porter-Duff模式

[英]Android ColorFilter - Porter-Duff Modes

I'm trying to solve a problem with android ColorFilters . 我正在尝试解决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. 一些像素具有alpha = 255,其他像素具有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. 我想在位图中不改变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 = 0的像素,但是alpha = 255的像素要涂上我想要的颜色。 Alpha channel shouldn't change. Alpha通道不应该改变。

I'm trying to use porter-duff ColorFilter (PorterDuffColorFilter class in android sdk). 我正在尝试使用porter-duff ColorFilter(android sdk中的PorterDuffColorFilter类)。

there are too many modes and no-understandable description on official site here : http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html 官方网站上有太多的模式和不可理解的描述: 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. 我想我应该使用DST_ATOPSRC_ATOP ,但它们不能像我描述的那样工作。

Also, there is a strange parameter srcColor in constructor of porter-duff colorfilter. 此外,还有一个奇怪的参数srcColor波特-达夫colorfilter的构造。

I can't understand what "Sa" and "Sc" means in formulas [Da, Sc * Da + (1 - Sa) * Dc] . 我无法理解公式中的“Sa”和“Sc”是什么意思[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". 它可以来自传递给colorfilter构造函数的颜色,也可以通过“paint.setColor”进行颜色设置。

Anybody knows, how it works? 谁知道,它是如何工作的?

Sa and Sc are shorts for "source alpha" and "source color", respectively. SaSc分别是“源alpha”和“源颜色”的短路。 The srcColor parameter in the PorterDuffColorFilter constructor is the color used for these values. PorterDuffColorFilter构造函数中的srcColor参数是用于这些值的颜色。

For your case the Mode.MULTIPLY would probably work best. 对于您的情况, Mode.MULTIPLY可能效果最好。

Note that Porter-Duff modes are only defined to work properly with premultiplied alpha. 请注意,Porter-Duff模式仅定义为与预乘alpha一起正常工作。 That means that none of the R, G or B components can exceed the alpha value. 这意味着R,G或B组分都不能超过α值。

GitHub project for the Android project which shows off all the Porter-Duff modes. Android项目的GitHub项目 ,展示了所有Porter-Duff模式。 The Android App is also available on Playstore. Android应用程序也可在Playstore上使用。

After some research I came to following conclusions regarding PorterDuff Color modes in Android, 经过一些研究后,我得出了关于Android中PorterDuff Color模式的结论,

  • Sa and Sc are Source Alpha and Source Color, eg if I am setting ColorFilter to a drawable like myDrawable.setColorFilter(desiredColor,Mode.SRC_ATOP) the Sa and Sc values will be taken from desiredColor not from myDrawable. Sa和Sc是源Alpha和源颜色,例如,如果我将ColorFilter设置为可绘制的像myDrawable.setColorFilter(desiredColor,Mode.SRC_ATOP),Sa和Sc值将从desiredColor而不是myDrawable中获取。 Da and Dc will be original alpha and color values of myDrawable. Da和Dc将是myDrawable的原始alpha和颜色值。
  • I had an Image and I want to apply certain color with alpha values from user input. 我有一个图像,我想从用户输入应用某些颜色与alpha值。 I tried to use PorterDuff Modes, My original myDrawable was a nine patch drawable and it had transparent background and trangular corners like messaging bubble triangles. 我尝试使用PorterDuff模式,我的原始myDrawable是一个九个可绘制的补丁,它有透明的背景和trangular角落,如消息气泡三角形。 When I used 我用的时候

    public static final PorterDuff.Mode SRC [Sa, Sc] public static final PorterDuff.Mode SRC [Sa,Sc]

which applies Source color and Source Alpha, which I supposed Color and Alpha values of above setColorFilter method desiredColor, I got color values correct ie Color and alpha was correctly applied to new Drawable but triangle on that image was completely washed/removed. 应用Source颜色和Source Alpha,我认为上面的setColorFilter方法desiredColor的颜色和Alpha值,我得到的颜色值正确,即颜色和alpha正确应用于新的Drawable,但该图像上的三角形被完全清洗/删除。 So I had to use ColorMatrixFilter and setup matrix and set alpha value separately to drawable. 所以我不得不使用ColorMatrixFilter并设置矩阵并将alpha值分别设置为drawable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM