Comments (8)
from pygame-ce.
I did try that, but couldn't find the specific magic combination to get it to work right. I got very similar (broken) results. Would you be so kind to adjust the demo code I posted to correctly use premultiplied blending?
from pygame-ce.
I did try that, but couldn't find the specific magic combination to get it to work right. I got very similar (broken) results. Would you be so kind to adjust the demo code I posted to correctly use premultiplied blending?
Sure, I added a bunch of comments to try and help you understand a bit more where I was bothering to pre-multiply or not. In the average application it is safe just to pre-multiply everything and always blit with premultiplication, but as it happens here you have some alpha'd black and solid white pixels which are not affected by an alpha pre-multiplication operation (multiply a 0 colour by anything and it is still 0, multiply 255 by 1 and it is still 255):
Anyway here is the working program:
import pygame
import pygame as pg
pg.init()
screen = pg.display.set_mode((400, 400))
clock = pg.time.Clock()
black_circle_surface = pg.Surface((100, 100), pg.SRCALPHA)
pg.draw.circle(black_circle_surface, (0, 0, 0), (50, 50), 25)
black_circle_surface = pg.transform.gaussian_blur(
black_circle_surface, radius=10
) # no need to pre-multiply as colour is zeros will not change whatever we multiply it by
white_circle_surface = pg.Surface((100, 100), pg.SRCALPHA)
white_circle_surface.fill(
(0, 0, 0, 0)
) # no need to pre-multiply, alpha is zero and colour is zero
pg.draw.circle(
white_circle_surface, (255, 255, 255), (50, 50), 25
) # no need to pre-multiply alpha and colour are the same (either 0 or 255)
circle_and_shadow_surface = pg.Surface((100, 100), pg.SRCALPHA)
circle_and_shadow_surface.fill((0, 0, 0, 0)) # no need to pre-multiply alpha is zero
circle_and_shadow_surface.blit(black_circle_surface, (0, 0))
circle_and_shadow_surface.blit(white_circle_surface, (0, 0))
semi_transparent_surface = pg.Surface((200, 200), pg.SRCALPHA)
semi_transparent_surface.fill(
(255, 255, 255, 1) # change the 1 to a 0 here to make the issue disappear
)
semi_transparent_surface = (
semi_transparent_surface.convert_alpha().premul_alpha()
) # need to pre-multiply RGB values will all be changed to 1
semi_transparent_surface.blit(
circle_and_shadow_surface, (50, 50), special_flags=pygame.BLEND_PREMULTIPLIED
)
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
screen.fill((20, 70, 80))
screen.blit(
semi_transparent_surface, (100, 100), special_flags=pygame.BLEND_PREMULTIPLIED
)
pg.display.update()
clock.tick(60)
This is the normal behaviour of the standard 'straight' alpha (the pygame default) and the superior 'premultiplied' alpha blending.
The main advantage of 'straight' alpha is that it is easier to understand and easy to dynamically alter the alpha value - otherwise it sucks.
Here is what the premultiplied alpha version looks like if we dial the semi-transparent surface up to 50 alpha:
from pygame-ce.
I also wrote a tutorial on premultiplied alpha blending here:
https://pyga.me/docs/tutorials/en/premultiplied-alpha.html
from pygame-ce.
Thank you so much: you've been incredibly helpful. I originally asked this on Stack Overflow, and the only comment I got was "this seems to be a bug with pygame-ce -- you should report it there."
If you have a stack overflow account, feel free to copy your answer here and I'll accept it: https://stackoverflow.com/questions/78322770/why-are-my-blurred-drop-shadows-lightening-instead-of-darkening-a-semi-transpare
If you don't, I'd love to copy it there with your permission, giving full credit to you.
from pygame-ce.
from pygame-ce.
@iamjackg could you copy over the answer to stack overflow? I'd like to fully resolve this issue so it can be closed in a good state.
from pygame-ce.
Done! Sorry about the delay, got sidetracked by other things.
from pygame-ce.
Related Issues (20)
- Adding the __getattr__ method to DirtySprite may lead to infinite recursion HOT 1
- What about geometry multi-collision and collision? HOT 1
- pygame.mixer.music endevent triggered by set_volume HOT 2
- pygame.mouse.set_pos() is not working under wayland HOT 3
- `pygame.Surface.fblits` slower in 2.5.0.dev2 vs 2.4.1 HOT 13
- Remove xwayland warning
- **TRACKER** Tracking changes that must be done when we start migrating to SDL3 in earnest HOT 1
- Port `TTF_SetFontHinting`
- New test fail in `set_system_cursor` when integer value passed is out of range
- problem with higher fps HOT 4
- pygame.mouse.get_just_pressed()? HOT 1
- Unhelpful error message in pygame.transform.gaussian_blur() with zero-dimensioned surfaces HOT 2
- Working with audio generates error (WSL2 - Windows11) HOT 2
- Portmidi broke cython HOT 1
- Add additional instruction for users encountering "No Module error" while Generating the Document
- Grammar: Missing comma in main Readme? HOT 1
- Local test fails: When compiling from Source on Raspberry Pi HOT 1
- Enhance window with OpenGL with viewport and documentation
- Move tutorials out of main repo into their own repository HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pygame-ce.