Jump to content

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


Recommended Posts

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:

https://github.com/toehead2001/pdn-blendmodes-plus/blob/master/BlendModesPlus/EffectPlugin.cs#L589

  • Like 1
Link to comment
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.

Link to comment
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

 

17 minutes ago, toe_head2001 said:

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

https://www.getpaint.net/doc/latest/BlendModes.html#16

Edited by IceCave
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.

 Share

×
×
  • Create New...