[R] color blending and transparency

Duncan Murdoch murdoch at stats.uwo.ca
Wed Feb 3 15:17:41 CET 2010


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
>



More information about the R-help mailing list