Page 1 of 1

How to change the alpha/color of one string?

Posted: Tue Oct 21, 2014 6:55 pm
by jumpsplat120
I'm trying to change the alpha of a string of text to get it to fade in. A while ago I had this working but since I've been revamping my code I can't seem to get the text to fade without having the background image fade as well. is there some vocabulary I might not know about, like stringvar:setColor(255) or something like that?

Re: How to change the alpha/color of one string?

Posted: Tue Oct 21, 2014 8:09 pm
by micha
You have to reset the color back to white after drawing the text:

Code: Select all

love.graphics.setColor(r,g,b) -- your text color
love.graphics.print('Hello World',100,100)
love.graphics.setColor(255,255,255) -- reset to white

Re: How to change the alpha/color of one string?

Posted: Tue Oct 21, 2014 10:43 pm
by Jasoco
Also you can use getColor to store the current color value in case you can't be certain of it before hand. So place it in a local and then reference it afterwards.

Code: Select all

love.graphics.setColor(0,0,255)

...

local oldColor = love.graphics.getColor()
love.graphics.setColor(255,0,0)
love.graphics.print("Some red text", 0, 0)

love.graphics.setColor(oldColor)

Re: How to change the alpha/color of one string?

Posted: Wed Oct 22, 2014 6:44 am
by HugoBDesigner
Both methods are right, but the guy wanted the alpha, which is the 4th argument.

Since it seems like you already set up the fade variable, all you need to do is this (based on what I think your code might look like):

Code: Select all

love.graphics.setBackgroundColor(red, green, blue, 255) --So your background doesn't fade. The "255" is required.
--It works with images or rectangles as well, just use love.graphics.setColor(red, green, blue, 255)

love.graphics.setColor(red, green, blue, alphaVariable) --Set this color for the string color that you want.
--Set the "alphaVariable" for the fading variable you already have.
--Draw the string here

love.graphics.setColor(red, green, blue, 255)
--Set this color for anything else you may have later, and draw them normally

Re: How to change the alpha/color of one string?

Posted: Wed Oct 22, 2014 9:06 am
by micha
Jasoco, you need to store all components of the color. Your code only stores the red-value.

Code: Select all

local oldRed, oldGreen, oldBlue, oldAlpha = love.graphics.getColor()
love.graphics.setColor(255,0,0)
love.graphics.print("Some red text", 0, 0)

love.graphics.setColor(oldRed, oldGreen, oldBlue, oldAlpha)

Re: How to change the alpha/color of one string?

Posted: Wed Oct 22, 2014 1:51 pm
by Ref
Or more simply

Code: Select all

local oldColor = { love.graphics.getColor() }

love.graphics.setColor(255,0,0,fadevalue)
love.graphics.print("Some red text", 0, 0)

love.graphics.setColor(oldColor)

Re: How to change the alpha/color of one string?

Posted: Thu Oct 23, 2014 1:55 am
by Jasoco
micha wrote:Jasoco, you need to store all components of the color. Your code only stores the red-value.

Code: Select all

local oldRed, oldGreen, oldBlue, oldAlpha = love.graphics.getColor()
love.graphics.setColor(255,0,0)
love.graphics.print("Some red text", 0, 0)

love.graphics.setColor(oldRed, oldGreen, oldBlue, oldAlpha)
It was an example. I wasn't showing off color. I was showing off renderTo(). The color is irrelevant. As is the text in my post.