Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlendSourceOver does not consider the alpha channel of the source color #3179

Closed
11 tasks
25349023 opened this issue Dec 29, 2024 · 1 comment
Closed
11 tasks

Comments

@25349023
Copy link

25349023 commented Dec 29, 2024

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

What feature would you like to be added?

Currently, the ebiten.BlendSourceOver (Note: same issue applied to BlendDestinationOver) is defined as following:

// BlendSourceOver is a preset Blend for the regular alpha blending.
//
//     c_out = c_src + c_dst × (1 - α_src)
//     α_out = α_src + α_dst × (1 - α_src)
BlendSourceOver = Blend{
	BlendFactorSourceRGB:        BlendFactorOne,
	BlendFactorSourceAlpha:      BlendFactorOne,
	BlendFactorDestinationRGB:   BlendFactorOneMinusSourceAlpha,
	BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
	BlendOperationRGB:           BlendOperationAdd,
	BlendOperationAlpha:         BlendOperationAdd,
}

However, it doesn't consider the transparency of the source color. For example, says source color = (0, 1, 1, 0) and dest color = (1, 0, 0, 1), with BlendSourceOver we'll have result color = (1, 1, 1, 1), which does not make sense to me. In fact, as long as the source color is semi-opaque, then the result color would be kind of weird.

Hence, I think that BlendSourceOver could be instead defined as following (or something else that could take the source's alpha channel into account):

// BlendSourceOver is a preset Blend for the regular alpha blending.
//
//     c_out = c_src × α_src + c_dst × (1 - α_src)
//     α_out = α_src + α_dst × (1 - α_src)
BlendSourceOver = Blend{
	BlendFactorSourceRGB:        BlendFactorSourceAlpha,
	BlendFactorSourceAlpha:      BlendFactorOne,
	BlendFactorDestinationRGB:   BlendFactorOneMinusSourceAlpha,
	BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
	BlendOperationRGB:           BlendOperationAdd,
	BlendOperationAlpha:         BlendOperationAdd,
}

If it is not possible to change the original preset variable, adding another new blend mode would be great, too.

The description of the over operator of color composition on the Wikipedia might help: https://en.wikipedia.org/wiki/Alpha_compositing#Description

Why is this needed?

No response

@hajimehoshi
Copy link
Owner

For example, says source color = (0, 1, 1, 0)

This is not premultiplied color value. Note that Ebitengine requires premultiplied color values.

See also https://github.com/tinne26/kage-desk/blob/main/docs/tutorials/premult.md (CC @tinne26)

@hajimehoshi hajimehoshi closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants