13.5 Color Blending

Drawing a design that is not completely opaque at all points allows the previous contents of the drawing plane to show through. The simplest case is drawing a solid design: where the design is opaque, it replaces the previous contents of the drawing plane; where the design is transparent, it leaves the drawing plane unchanged. In the more general case of drawing a translucent design, the resulting color is a blend of the design's color and the previous color of the drawing plane. For purposes of color blending, the drawn design is called the foreground and the drawing plane is called the background. [annotate]

The function compose-over performs a similar operation: it combines two designs to produce a design, rather than combining a design and the contents of the drawing plane to produce the new contents of the drawing plane. For purposes of color blending, the first argument to compose-over is called the foreground and the second argument is called the background. See Chapter 14 for the details of compose-over. [annotate]

Color blending is defined by an ideal function F:(r1,g1,b1,o1,r2,g2,b2,o2)→(r3,g3,b3,o3) that operates on the color and opacity at a single point. (r1,g1,b1,o1) are the foreground color and opacity. (r2,g2,b2,o2) are the background color and opacity. (r3,g3,b3,o3) are the resulting color and opacity. The color blending function F is conceptually applied at every point in the drawing plane. [annotate]

F performs linear interpolation on all four components: [annotate]

o3 = o1 + (1 - o1)*o2
r3 = (o1*r1 + (1 - o1)*o2*r2)/o3
g3 = (o1*g1 + (1 - o1)*o2*g2)/o3
b3 = (o1*b1 + (1 - o1)*o2*b2)/o3

Note that if o3 is zero, these equations would divide zero by zero. In that case r3, g3, and b3 are defined to be zero. [annotate]

CLIM requires that F be implemented exactly if o1 is zero or one or if o2 is zero. If o1 is zero, the result is the background. If o1 is one or o2 is zero, the result is the foreground. For fractional opacity values, an implementation can deviate from the ideal color blending function either because the implementation has limited opacity resolution or because the implementation can compute a different color blending function much more quickly. [annotate]

If a medium's background design is not completely opaque at all points, the consequences are unspecified. Consequently, a drawing plane is always opaque and drawing can use simplified color blending that assumes o2 = 1 and o3 = 1. However, compose-over must handle a non-opaque background correctly. [annotate]

Note that these (r,g,b,o) quadruples of real numbers between 0 and 1 are mathematical and an implementation need not store information in this form. Most implementations are expected to use a different representation. [annotate]