Views
fadeColor
fadeColor is a helper function to make smooth color transitions suitable to fading effects.
function fadeColor(
time, prologue, attack, sustain, decay, epilogue,
fade_in_r, fade_in_g, fade_in_b,
fade_out_r, fade_out_g, fade_out_b
)
-- [0, prologue)
if time < prologue then
return
fade_in_r,
fade_in_g,
fade_in_b,
255
end
-- (prologue, prologue + attack]
time = time - prologue
if time < attack then
return
fade_in_r,
fade_in_g,
fade_in_b,
( math.cos( time / attack * math.pi ) + 1 ) / 2 * 255
end
-- (prologue + attack, prologue + attack + sustain]
time = time - attack
if time < sustain then
return
fade_in_r,
fade_in_g,
fade_in_b,
0
end
-- (prologue + attack + sustain, prologue + attack + sustain + decay]
time = time - sustain
if time < decay then
return
fade_out_r,
fade_out_g,
fade_out_b,
255 - ( ( math.cos( time / decay * math.pi ) + 1 ) / 2 * 255 )
end
-- (prologue + attack + sustain + decay, prologue + attack + sustain + decay + epilogue]
time = time - decay
if time < epilogue then
return
fade_out_r,
fade_out_g,
fade_out_b,
255
end
-- End of fading, return all nils.
end
time, prologue, attack, sustain, decay, epilogue,
fade_in_r, fade_in_g, fade_in_b,
fade_out_r, fade_out_g, fade_out_b
)
-- [0, prologue)
if time < prologue then
return
fade_in_r,
fade_in_g,
fade_in_b,
255
end
-- (prologue, prologue + attack]
time = time - prologue
if time < attack then
return
fade_in_r,
fade_in_g,
fade_in_b,
( math.cos( time / attack * math.pi ) + 1 ) / 2 * 255
end
-- (prologue + attack, prologue + attack + sustain]
time = time - attack
if time < sustain then
return
fade_in_r,
fade_in_g,
fade_in_b,
0
end
-- (prologue + attack + sustain, prologue + attack + sustain + decay]
time = time - sustain
if time < decay then
return
fade_out_r,
fade_out_g,
fade_out_b,
255 - ( ( math.cos( time / decay * math.pi ) + 1 ) / 2 * 255 )
end
-- (prologue + attack + sustain + decay, prologue + attack + sustain + decay + epilogue]
time = time - decay
if time < epilogue then
return
fade_out_r,
fade_out_g,
fade_out_b,
255
end
-- End of fading, return all nils.
end
Arguments:
- time is the 0-based time to evaluate the color.
- prologue is how much time the resulting color will be equal to the fully opaque fade_in color.
- attack is how much time it takes to go from fully opaque fade_in to fully transparent fade_in.
- sustain is how much time the color resulting color will be the fully transparent fade_in.
- decay is how much time it takes to go from fully transparent fade_out color to fully opaque fade_out color.
- epilogue is how much time the resulting color will be equal to the fully opaque fade_out color.
- fade_in_[rgb] are the components of the fade_in color.
- fade_out_[rgb] are the components of the fade_out color.
The function returns nil when the transitions are over. The transitions from fully opaque to fully transparent are evaluated with the cosine function to make it smooth. Usage:
-- This is part of your update loop, draw whatever you need here.
-- Evaluate a color that makes the screen fade from white and then to black.
local r, g, b, a = fadeColor(
dt,
2, -- Two seconds opaque white.
2, -- Two seconds to make it transparent.
3, -- Three seconds transparent.
2, -- Two seconds to go to opaque black.
0, -- Zero seconds opaque black.
255, 255, 255, -- White.
0, 0, 0 -- Black.
)
-- If the fading is done, better go do something else...
if not r then
-- Change state to something else...
return
end
-- Fill the entire screen with the resulting color.
love.graphics.setColor( r, g, b, a )
love.graphics.rectangle( 'fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight() )
-- Evaluate a color that makes the screen fade from white and then to black.
local r, g, b, a = fadeColor(
dt,
2, -- Two seconds opaque white.
2, -- Two seconds to make it transparent.
3, -- Three seconds transparent.
2, -- Two seconds to go to opaque black.
0, -- Zero seconds opaque black.
255, 255, 255, -- White.
0, 0, 0 -- Black.
)
-- If the fading is done, better go do something else...
if not r then
-- Change state to something else...
return
end
-- Fill the entire screen with the resulting color.
love.graphics.setColor( r, g, b, a )
love.graphics.rectangle( 'fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight() )