fading white rect appears dark when changing alpha

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
isyung
Prole
Posts: 1
Joined: Sat Apr 19, 2025 7:39 am

fading white rect appears dark when changing alpha

Post by isyung »

hey love2d folks,

i’m running into a weird issue with fade‑out animations and blend modes. here’s my scenario:
  • i have a simple grid background (checker of 0.75/0.8 grey)
  • on hover of a button i draw a pure white rectangle above it
  • when the hover ends, i want that white rect to fade out by reducing its alpha from 1 → 0
    my draw code looks like this:

Code: Select all

-- inside button:draw()
if self.buyEffect and self.buyStacks > 0 and self.buyFade > 0 then
    -- try to keep it pure white as it fades
    love.graphics.setBlendMode("replace")
    love.graphics.setColor(1, 1, 1, self.buyFade)
    love.graphics.rectangle(
        "fill",
        self.x,
        self.y - (gridSize * self.buyStacks),
        gridSize * 10,
        gridSize * self.buyStacks
    )
    love.graphics.setBlendMode("alpha")
    love.graphics.setColor(1,1,1,1)
end
what i see:
  • at full alpha it’s bright white as expected
  • as soon as self.buyFade dips below 1, the rectangle looks dark grey (almost like it’s blending two greys instead of white → transparent)
  • even if i hardcode the alpha to .5 or something it still appears grey
  • if i set it to a color it also seems to get dark as it fades.
what i expected:
  • the rectangle should stay white while fading out, eventually disappearing to reveal the grid underneath
i’ve tried both "replace" and "alpha" blend modes, but can’t get that crisp white fade. is there another blend mode or draw trick i’m missing? how can i fade only the alpha channel of a pure white rectangle without it going dark? is this just how love works?

any pointers would be hugely appreciated—thanks!
Imnotaplayer
Prole
Posts: 3
Joined: Thu Dec 15, 2022 6:31 pm

Re: fading white rect appears dark when changing alpha

Post by Imnotaplayer »

If you're drawing the rectangle to a canvas, you may want to use the "premultiplied" BlendAlphaMode when drawing that canvas.

Image

Nothing looks inherently wrong with the code provided, so this is a guess at your problem.
RNavega
Party member
Posts: 465
Joined: Sun Aug 16, 2020 1:28 pm

Re: fading white rect appears dark when changing alpha

Post by RNavega »

The formulas used by the blend modes are in here btw: https://love2d.org/wiki/BlendMode_Formulas
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 8 guests