Jump to content

Is my algebra right?>


Recommended Posts

I'm working on a plugin to reverse the effects of merging two layers (one being an image, the bottom layer, and the other, the upper layer, being a solid color of some kind).

 

Normal blending uses an equation like this: CompositeColor = UpperLayerColor * UpperLayerAlpha + LowerLayerColor * (255 - UpperLayerAlpha)
                                                                                                        --------------------------------------------------------------------------------------
                                                                                                                                                                    255

 

I've attempted to refactor it, and got this: LowerLayerColor = 255 * CompositeColor - (UpperLayerColor * UpperLayerAlpha)
                                                                                                       ----------------------------------------------------------------
                                                                                                                                              (255 - UpperLayerAlpha)

 

Does that look correct to you? On the handful of images I've tried this reverse opacity plugin on, the colors seemed too white.

Link to comment
Share on other sites

Shouldn't it be something like?:

 

        r = ClampRGBChannel((color.R - (ColorWheelControl_BlendColor.R * opacity))/(1.00 - opacity));
        g = ClampRGBChannel((color.G - (ColorWheelControl_BlendColor.G * opacity))/(1.00 - opacity));
        b = ClampRGBChannel((color.B - (ColorWheelControl_BlendColor.B * opacity))/(1.00 - opacity));

Link to comment
Share on other sites

20 hours ago, lightknight said:

Does that look correct to you? On the handful of images I've tried this reverse opacity plugin on, the colors seemed too white.

 

The algebra appears to be correct. As far as the image being too white, could it possibly be that in the image-blended-with-solid-color case, you aren't taking into account that for the reverse process to work, the image must be able to be produced by blending some image with the solid color. For example, if the solid color is pure red, and the composite image contains pure green, there's no way to produce the composite image unless the alpha for the red layer is 0. The process is, I think, easier to analyze if you think of the lower layer as the color layer, and the upper layer as the image, so that it's the image-layer alpha that must be determined, along with the image-layer color. Otherwise, you're computing the color for the lower layer and the alpha for the upper layer.

 

What you're doing is essentially what my Color Clearer plugin does. I wrote a little article on doing the math exactly with integers. Because of the integer math, it's a bit confusing, but the basic idea is that an alpha is computed for each color component, then the image color is computed based on the minimum of the three alphas.

 

In your follow up comment, you have just substituted opacity for alpha, where opacity = alpha/255.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...