# [R] color blending and transparency

Wed Feb 3 15:38:43 CET 2010

```That makes perfect sense, thank you, except that I'm not sure where
the white comes from when I set the background to transparent?

png("testingOrder.png", bg = "transparent")
plot.new()
par(bg="transparent")
rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5))
rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5))

rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5))
rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5))

dev.off()

Still produces two different overlap colours, although I *think* only
two colours are involved. What I have I missed here?

Thanks,

baptiste

On 3 February 2010 15:17, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> On 03/02/2010 8:50 AM, Ken Knoblauch wrote:
>>
>> baptiste auguie <baptiste.auguie <at> googlemail.com> writes:
>>>
>>> Adding two semi-transparent colours results in non-intuitive colour
>>> mixing (a mystery for me anyway). Is it additive (light), substractive
>>> (paint), or something else? Consider the following example, depending
>>> on the order of the two "layers" the overlap region is either purple
>>> or dark red. I have no idea why.
>>>
>>> png("testingOrder.png")
>>> plot.new()
>>>
>>> # Red below
>>> rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=0.5))
>>> rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5))
>>>
>>> # Blue below
>>> rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=0.5))
>>> rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5))
>
> I think it's a fairly simple calculation.  In the first example: We are
> writing red (1,0,0) at alpha=0.5 onto white (1,1,1), so we get a mixture of
> half existing and half new, i.e. (1,0.5,0.5).  Then we write blue (0,0,1) at
> alpha 0.5 onto that, giving (0.5, 0.25, 0.75).
>
> In the second pair, the first write yields (0.5,0.5,1), and the second
> yields (0.75, 0.25, 0.5).
>
> So this is like mixing paints:  you don't get the same colour if you mix
> equal parts red and white, then take equal parts of that mixture with blue,
> as you get if you put the blue in first.  You've got less red in the first
> mixture than in the second.
>
> You would get the same color in both mixtures if you didn't mix the white
> in:
>
> # Red below
> rect(0.3, 0.5, 1, 1, col=rgb(1, 0, 0, alpha=1))
> rect(0, 0.5, 0.7, 1, col=rgb(0, 0, 1, alpha=0.5))
>
> # Blue below
> rect(0, 0, 0.7, 0.5, col=rgb(0, 0, 1, alpha=1))
> rect(0.3, 0, 1, 0.5, col=rgb(1, 0, 0, alpha=0.5))
>
>
> Duncan Murdoch
>
>>>
>>> dev.off()
>>
>> I would expect overlaid transparencies to act like filters and
>> multiply, producing so-called subtractive color mixing,
>> so blue and yellow gives green.  Interestingly, however,
>> overlaying filters is not necessarily a commutative operation, since a
>> transparent filter can yield an
>> additive component (through scatter, for example)
>> though I suspect that the non-commutativity comes
>> about in R because these rules apply to physical lights,
>> filters and surfaces and in R, it is some uncalibrated combination
>> of frame buffer values that is being used.
>>>
>>> Best,
>>>
>>> baptiste
>>>
>>
>> Ken
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help