# How is the "Overlay" Blend Function working (Algorithm)?

## Recommended Posts

I am trying to wrap my head around it, but I can't find a function that blends these colors

(122, 141, 153)

and

(0, 0, 0)

into this one

(0, 27, 51)

Edited by IceCave
##### Share on other sites

Here's the function for Overlay blend, with your example values:

```ColorBgra lhs = ColorBgra.FromBgr(153, 141, 122);
ColorBgra rhs = ColorBgra.FromBgr(0, 0, 0);

byte r = lhs.R < 128
? Int32Util.ClampToByte(2 * rhs.R * lhs.R / byte.MaxValue)
: Int32Util.ClampToByte(byte.MaxValue - 2 * (byte.MaxValue - rhs.R) * (byte.MaxValue - lhs.R) / byte.MaxValue);

byte g = lhs.G < 128
? Int32Util.ClampToByte(2 * rhs.G * lhs.G / byte.MaxValue)
: Int32Util.ClampToByte(byte.MaxValue - 2 * (byte.MaxValue - rhs.G) * (byte.MaxValue - lhs.G) / byte.MaxValue);

byte b = lhs.B < 128
? Int32Util.ClampToByte(2 * rhs.B * lhs.B / byte.MaxValue)
: Int32Util.ClampToByte(byte.MaxValue - 2 * (byte.MaxValue - rhs.B) * (byte.MaxValue - lhs.B) / byte.MaxValue);

ColorBgra overlayColor = ColorBgra.FromBgr(b, g, r);```

More here:

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

##### Share on other sites

So it takes the destination color in order to decide between Screen and Overlay and not the source color. Interesting.

Thank you very much. Very helpful.

##### Share on other sites

2 minutes ago, IceCave said:

So it takes the destination color in order to decide between Screen and Overlay and not the source color.

Well, you decide which color is the source and which is the destination.

The Screen blend mode uses a totally different algorithm, and is not involved in the code I posted.

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

##### Share on other sites

Well, yeah, true... anyone finding this topic over Google:

rhs is the pixel color of the top layer (the one set to overlay)

lhs is the blend color of the bottom layers

The Screen blend mode uses a totally different algorithm, and is not involved in the code I posted.

I was referring to the documentation, I haven't actually checked

Edited by IceCave
##### Share on other sites

For the float version of the overlay blending mode formula -

a-base

b-blend

f(a,b)= a<.5 ? 2ab : 1-2(1-a)(1-b) ;

This implies that the ranges has to be normalized to 0-1 ranges before calculation.

Edited by Reptillian

G'MIC Filter Developer

I am away from this forum for undetermined amount of time: If you really need anything related to my PDN plugin or my G'MIC filter within G'MIC plugin, then you can contact me via Paint.NET discord, and mention me.

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×