How to change the alpha/color of one string?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
jumpsplat120
Prole
Posts: 5
Joined: Fri Oct 03, 2014 9:07 pm

How to change the alpha/color of one string?

Post 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?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

Post 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
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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)
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post 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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

Post 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)
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post 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)
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests