# TiKz/PS Blending at the pixel level

To do proper blending easily in TiKz, I need to do be able to have a special blend mode. (this is relevant to anything dealing with graphics and not tiks/tex)

• When you plot a pixel with an alpha channel the graphics package or graphics card will mix the pixel color below it with the new one. eg., suppose we have (100,0,0,255) and we plot (50,50,0,50) on top of it. The new pixel will be (100*(255 - 50)/255 + 50*50/255, 50*50/255, 0, 255) = (80, 10, 0, 255).

• Now when you have a white background you always end up mixing with white for the initial pixel. So if you plot any pixel for the first time with an alpha channel it will get blended with white and become washed out. (255, 255, 255, 255) blended with (255, 0, 0, 50) = (255, 205, 205, 255)

• This is the default behavior of tikz and most graphics packages that plot with alpha channel(you just mix pixels)

• I would like, instead, to plot where the first color does not mix. Basically if it is the first pixel to be plotted then it's alpha channel will always be set to 255. This prevents colors from being washed out and allows for complex geometrical conditions that break others.

• Anyone know if TiKz can do this or be modified to do it? Basically one has to keep a bit buffer where each bit represents if a pixel has been plotted for the first time or not(sort of a like a z-buffer). Blending mode - This mode is basically "Set Alpha channel to 255 for new pixels"

• One can do the test on the primitive level in tikz BUT this does not get partial overlapped objects. Think of two circles that overlap but not totally. One circle is "new"(in the sense that all pixels are new) and the other is partially new(some pixels, the overlapped ones) are not new while the others are. Using tikz's fill opacity cannot handle this case(although you could do some clipping and stuff that becomes very complex quickly).

One way to see this at http://www.texample.net/tikz/examples/venn-diagram/

notice the last diagram and how washed out all the colors are. In my mode they will not be washed out because none of the circles pixels will be blended with white but all blend with themselves.

Again, one can achieve my effect in a very complex manner by using clipping and changing alpha values but it can be achieved much easier given the method I have described = if new pixel then force alpha channel = 255.

• Why do you think that things get complex quickly? Please check the transparency group in the manual. Besides, can you put very simple examples that shows what you wanted. Because I don't follow what is new and what is not? Can you please rephrase in terms of layer top-bottom analogy? Also in the last example there is an opacity setting in effect. – percusse Mar 21 '12 at 23:43
• @percusse Did you read what I said? Did you go to the venn diagram example? Can you reproduce that(without washout describe in my post) easily wish transparency groups without using clipping? I see nothing about transparency groups that work from the examples given at texample.net/tikz/examples/pgf-version-2 – Uiy Mar 22 '12 at 0:01
• If you are having trouble thinking about what I am saying then just think of pixels. This is a pixel issue, and alpha blending issue and has nothing to do with higher level objects(except insomuch as they are made up of pixels). Do you understand why normal alpha blending at the pixel level produces "washout" if not then please work on that first as that is where the problem comes from. – Uiy Mar 22 '12 at 0:04
• Your last assumption is false ("I imagine TikZ does all the blending"). TikZ does nothing in this regard: it is all passed on to the output format. If you can figure out a way to do this in the PDF specification, then it might be possible to get TikZ to output the correct parameters. What you are asking for (as far as I can understand) is that the graphics system use the alpha channel for constructing the picture and then throw that away when placing the picture on the page. But this isn't what happens: each element is placed on the page individually. – Loop Space Mar 22 '12 at 9:34
• Right, but my main point is that this is handled by the renderer (usually PDF, but not necessary) and so is out of the control of TeX. TikZ does not work at the pixel level. The thing that does pixels is the program that actually displays the picture on your screen, or prints it out. PDF is a vector format so TeX cannot know what actually corresponds to a pixel. Therefore, what you ask for cannot be done in the method that you prescribe. It may be possible to achieve the effect by some means, would that be good enough? – Loop Space Mar 22 '12 at 9:55

As far as I know, this is not possible because the output formats do not implement it. The most common output formats for TikZ/PGF are vector formats. This means that they do not specify graphic elements by pixels but by paths, with instructions on what to do with those paths. This has definite advantages in that they are typically smaller (admittedly, for types of drawing particularly suited to this format) and they scale perfectly. It is the job of the renderer or viewer to interpret these commands into something on the screen or paper and it is only at that stage where pixels come into play. By this point, TeX is out of play and it is not possible to feed back information from the renderer to the original tex process.

For more on what a vector format is, take a look at the Wikipedia page on PDF.

However, that's not to say that what you ask for is impossible. It is certainly possible to simulate the effect that you want. The most straightforward way that I can think of is the following: you want the "base" colour at each point to be the colour that is first drawn there. So we start by creating a copy of the picture where each colour is its initial colour. Then we draw the real picture on top of this with the alphas as desired. This should have the correct effect since when each element gets rendered then if it is "first" it will get blended with an opaque copy of itself, whence be the opaque version of itself, but if it is not first then it blends with what's already there as it ought to.

To create the copy where each colour is its initial colour then we draw the image in reverse with each colour set to fully opaque. This might take a little planning for a particular picture, of course.

Here's the example of the Venn diagram that you linked to.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/48905/86}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(45:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}
\begin{document}
\begin{tikzpicture}
\begin{scope}[fill opacity=0.5,text opacity=1]
\fill[red] \firstcircle;
\fill[green] \secondcircle;
\fill[blue] \thirdcircle;
\draw \firstcircle node[below] {$A$};
\draw \secondcircle node [above] {$B$};
\draw \thirdcircle node [below] {$C$};
\end{scope}
\begin{scope}[yshift=-5cm]
\fill[blue] \thirdcircle;
\fill[green] \secondcircle;
\fill[red] \firstcircle;
\begin{scope}[fill opacity=0.5,text opacity=1,every node/.style={ellipse,fill=white}]
\fill[red] \firstcircle;
\fill[green] \secondcircle;
\fill[blue] \thirdcircle;
\draw \firstcircle node[below] {$A$};
\draw \secondcircle node [above] {$B$};
\draw \thirdcircle node [below] {$C$};
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}


Result (original on top):

• The 2nd case is very close but I am not sure if it is is truely the same. It may work though but does require one to keep a "history" of all the objects and I'm not sure if more complex geometrical configurations would work. I'll have to think about it some more but SamB's adobe link does have some compositing/blending stuff about pdf's at the pixel level so it might be possible to do what I want. – Uiy Mar 22 '12 at 23:14
• My second case is exactly what you describe. I've taken a look at the link in Sam's post. You cannot do what you want from the formula in Section 3: there is no way to adjust the numbers to get what you describe. So my statement about this not being supported stands. However, it does show that you can do more interesting things that I thought possible and not all of those are realised in TikZ (yet). – Loop Space Mar 23 '12 at 13:19

I don't quite understand the precise details of what you're looking for, but I suspect that you would find these links informative:

• In the adobe version, section 3 they have the computation for blending they use. I am not sure if my formula will work or not as I just glanced over it(got other things to do ATM). The question is, pdf's seem to support it but how difficult would it be to get into tikz? – Uiy Mar 22 '12 at 23:12
• That second document was a very interesting read about PDF and transparency. Nice sleuth work. – Loop Space Mar 23 '12 at 13:20