[cairo] Color-Tinting a surface with alpha channel

Simon Schneegans code at simonschneegans.de
Thu Dec 24 20:10:14 UTC 2020


Hi Uli,

thank you for the kind reply!
> Could you explain a bit more what exactly you want to achieve? E.g.,
> what should happen when tinting with r=0.5, g=1, b=0, a=0.5 to colors
> such as full red and... basically all the other colors.

I think you understood my issue very well, but I will try to explain it 
with more examples. Here are some example images (the checkerboard 
visualizes the alpha channel). All colors below are given in [r,g,b,a] 
in non-premultiplied form.


  * (a) This is my input image, which I want to colorize. It contains
    two linear gradients, the top from [1, 1, 1, 1] to [1, 1, 1, 0], the
    bottom one from [0, 0, 0, 1] to [0, 0, 0, 0, 0].
  * (b) By colorizing (a) with [1, 0, 0, 1], I want to achieve (b). All
    color components should be multiplied pair-wise; the alpha component
    of the tint-color does not really matter, but would be nice-to-have
    to support this as well.
  * (c) If I use the code I posted earlier (basically first paint input
    image, then paint tint-color with Cairo.Operator.MULTIPLY masked by
    the input image's alpha channel) I get (c). Note how the top
    gradient actually fades to transparent white and the bottom one to
    transparent red.
  * (d) If I paint the tint-color over the entire image and then use
    Cairo.Operator.DEST_IN to restore the input image's alpha channel, I
    get (d). The upper gradient looks good, but the bottom one still fails.
  * (e) If I initialize my surface with [0,0,0,1] before executing the
    code for (d), I get (e). This fixes the bottom gradient but now the
    top one fades to transparent black.

> Do I understand correctly that you want to transform a given (r,g,b,a)
> into (0.5*r, 1*g, 0*b, 0.5*a)? (numbers for my example above)

Exactly that.

> Is the alpha part really needed or could you live with no support for an
> alpha component for the color "to tint with"?
It is not strictly required. The input image has an alpha channel which 
should be preserved if the tint-color is only RGB. However, it would be 
a nice addition if the tint-color could be RGBA as well! In this case a 
tint-alpha of 0.5 would mean that all alpha values of the input image 
are multiplied with 0.5.
> Also, do you know the following page? It explains precisely what each of
> the operators does.
>
> https://www.cairographics.org/operators/
>
Yes, I looked into them but failed to figure out a combination which 
could solve my issue.

Does this explain my problem?


Thank you,

Simon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20201224/eabcc6a6/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fknjodbniennbolb.jpeg
Type: image/jpeg
Size: 41997 bytes
Desc: not available
URL: <https://lists.cairographics.org/archives/cairo/attachments/20201224/eabcc6a6/attachment-0001.jpeg>


More information about the cairo mailing list