views:

82

answers:

2

If I have a bottom layer color and an alpha value (C&A) and want to create a custom C&A on the screen, what is the function to determine what C&A has to be added as a layer on top of the bottom layer?

edit:

I want to duplicate photoshop's "normal" mode so that I match a designer's graphic design.

For example:

BASE LAYER rgb: 255-0-0 alpha: 51/256

+

NEW LAYER rgb: ???  alpha: ???

=

DESIRED LAYER rgb: 114-0-141 alpha: 92/256

P.S.: the answer is 0-0-255 alpha: 51/256... but I only know this because I wrote the problem and verified it in photoshop.

+2  A: 

This would depend on the Blend Mode used between the two layers (bottom and top). The wiki page lists some formulae that may be of interest to you.

The PDF Reference manual has a nice explanation too:

αrCr = [(1 - αs) * αb * Cb] + [(1 - αb) * αs * Cs] + αb * αs * B(Cb, Cs)

where

  C = color, α = alpha value 

and the subscripts

r = result, b = backdrop, s = source

Also,

B(Cb, Cs) = blend mode function

In case of Normal Blend Mode: B(Cb, Cs) = Cs

The blended alpha is given by:

αr = Union(αb, αs) and

Union(b, s) = b + s - (b * s )

dirkgently
thx for that link. I edited the OP. I am trying to match photoshop's "normal" mode (hmm.. now to figure out what that is)
jedierikb
That will be Normal Blend Mode (the most common blend mode). The normal blend mode does no blending and 100% opaque pixels on the top layer totally block out pixels on the layers below. A 50% transparent pixel lets you see a partially transparent view of the pixels below. It's the default mode.
dirkgently
Alas, the one sentence description of normal mode on wikipedia is not too helpful in regard to handling alpha... sorry to be dense.
jedierikb
Which part of alpha blending are you having difficulty with?
dirkgently
converted into code here: http://forum.processing.org/topic/normal-blend-mode
jedierikb
A: 

I think that photoshop's "normal" mode implements Porter Duff "over" compositing. Then the wikipedia page is useful and lists the formulas, expecially for handling alpha.

Note that not all solutions are possible. I.e., the alpha of the desired layer must be larger than the alpha of the base layer.

The solution could go like this:

  • determine alpha of new layer as (alpha_desired - alpha_base) / (1 - alpha_base). Note that (alpha_desired - alpha_base) must be positive.
  • determine color of new layer, which may be outside the available range -- then the operation is impossible.
Lale