Tints and Gradients

Posted by

In Corona daily build 612, we’ve added a couple of graphics goodies: tinting and gradients!

Tints

You can now tint images by calling setFillColor() on the image object. You pass in colors just like you would for a solid object and Corona interprets this as a tint. The number of arguments determines how the numbers are interpreted. It’s grayscale if you pass 1 or 2 and color if you pass 3 or 4:

image:setFillColor( gray )
image:setFillColor( gray, alpha )
image:setFillColor( red, green, blue )
image:setFillColor( red, green, blue, alpha )

Here’s a quick example of a Andy Warhol-like treatment of the HelloWorld sample to give you a sense of how it works. It draws the world.jpg 4 times. Once normally and then 3 more times with different tints:

local w,h = display.contentWidth*0.5, display.contentHeight*0.5
local halfW,halfH = 0.5*w, 0.5*h
local background = display.newImageRect( "world.jpg", w,h )
background:translate( halfW, halfH )

local dw = w + halfW
local dh = h + halfH

local background = display.newImageRect( "world.jpg", w,h )
background:setFillColor( 255, 255, 0 )
background:translate( dw, halfH )

local background = display.newImageRect( "world.jpg", w,h )
background:setFillColor( 255, 255, 0 )
background:translate( halfW, dh )

local background = display.newImageRect( "world.jpg", w,h )
background:setFillColor( 255, 0, 255 )
background:translate( dw, dh )

To get rid of an image tint, simply pass in white:

image:setFillColor( 255 )

Update: Starting in daily build 614, you can now tint sprite objects as well. Same syntax:

spriteObject:setFillColor( 255, 0, 0 )    -- red tint

 

Gradients

You can now add tints gradients to text objects and also solid rectangles (that’s pure rects, not rounded rects) using the new gradient API. You create a new gradient object by calling graphics.newGradient(). You can pass (and reuse) the object in calls to text:setTextColor() and rect:setFillColor().

The following code demonstrates a vertical gradient that goes from yellow to white:

local myText =
    display.newText( "Hello, World!", 0, 0, native.systemFont, 40 )
myText.x = display.contentWidth * 0.5
myText.y = display.contentWidth * 0.25
local g = graphics.newGradient( { 180, 180, 0 }, { 255 }, "down" )
myText:setTextColor( g )

 

Ready to get started?

Create amazing games and apps for iOS & Android

25 Comments

CairoSeptember 7th, 2011 at 4:52 pm

Wooooooooooo! Wonderful! I know so many people who have been waiting for this!

J. A. WhyeSeptember 7th, 2011 at 5:00 pm

On my “to-do” list for today was to sit down with Acorn and create sets of objects in different colors. I’ve just scratched that off my list!

It means instead of having dozens of graphic objects for party of my game I can have one set and just tint them. *This* may be big enough for me to give up my “I don’t use daily builds for my released apps” stance. :)

Very nice!

Jay

CrossmanSeptember 7th, 2011 at 5:21 pm

This is awesome.

The more photo editing stuff the better.

Ken IbrahimSeptember 7th, 2011 at 5:23 pm

Awesome! I was hoping this would be possible somehow so that I could create different colored versions of a background image by applying a tint to a grayscale original.

John BallingerSeptember 7th, 2011 at 6:43 pm

OMG, I was just thinking about this today, this is a great feature. Yay…

InasSeptember 7th, 2011 at 6:59 pm

Wow, this is useful feature!

Question, if we want to clear the tint, is there any API for that, or should we just set the fillColor to 0 alpha?

WalterSeptember 7th, 2011 at 7:06 pm

To clear a tint, set the fill color to completely white. Tinting modulates the colors of the bitmap by the color of the tint, so modulating with white (1.0) gives back the original bitmap color.

Sun JiajieSeptember 7th, 2011 at 7:09 pm

wow,great!! that’s the feature i was waiting for!

firemaplegamesSeptember 7th, 2011 at 10:49 pm

Awesome!

SannyChanSeptember 7th, 2011 at 10:53 pm

Yes!!

One step closer to closing the graphics capability gap!

You have one more item on your list we need, a copyPixels function!
object.copyPixelsTo(object) to create dynamic textures, which would speed up soo many draw processes, its not even funny!

Or even a display.flatten() function to flatten all objects into one texture. One can dream!

Matt LSeptember 8th, 2011 at 1:22 am

Does this work with sprites as well?

finefinSeptember 8th, 2011 at 3:31 am

great! Now I can get rid of my alpha-channel workarounds to make different brightness levels of a texture.

way to go!

finefinSeptember 8th, 2011 at 4:07 am

I just updated my project and it works like a charm! thank you!

John TranSeptember 8th, 2011 at 7:47 am

Oh yea!!! I need this…Thank you very much!

Paul Allan HarringtonSeptember 8th, 2011 at 8:24 am

Perfect timing. I was just trying to schedule time to edit graphics for this effect and similar and wishing I didn’t have to spend time editing graphics when I should/could be programming :)

Best wishes,

Paul Allan Harrington
Visuals Work: { visualswork.com }
Consulting Group: { pahgroup.com }

codeRebelBaseSeptember 8th, 2011 at 11:26 am

Does tinting work with all display objects, including sprites?

XenonBLSeptember 8th, 2011 at 3:02 pm

Works great! Thanks for implementing.

WalterSeptember 8th, 2011 at 3:57 pm

@Matt, yes starting in build 614 you can tint sprites via setFillColor(). See update in blog post.

don@loqheart.comSeptember 8th, 2011 at 8:42 pm

@Walter That’s strange. I tested with 612, and setFillColor works on sprites there as well. I tested and updated the Spriteloq API as a result. Is there something different in 614?

J. A. WhyeSeptember 9th, 2011 at 10:49 am

I already commented to say “whoohoo!” but wanted to post a follow-up to say thanks again for adding this — I played around with it yesterday and it works like a charm.

I set one of my current art assets to greyscale and after that added tint after tint to create “new versions” of the same thing. It works great and is going to save me a lot of time.

Jay

Chang Kian BoonSeptember 13th, 2011 at 11:22 am

Has anyone tried to call the setFillColor to tint an image on timer.performWithDelay? It works for normal rect or circle, but not with image. Any solution for this?

ChrisSeptember 19th, 2011 at 9:19 am

Will it ever be possible to tint a display group?

ChrisSeptember 19th, 2011 at 6:40 pm

Also, will the transition library be updated to support tints?

saravananSeptember 23rd, 2011 at 3:35 am

local myText =
display.newText( “Hello, World!”, 0, 0, native.systemFont, 40 )
myText.x = display.contentWidth * 0.5
myText.y = display.contentWidth * 0.25
local g = graphics.newGradient( { 180, 180, 0 }, { 255 }, “down” )
myText:setTextColor( g )

copy code and paste into my edit .And i run its make some runtime error

Jeff JohnsonFebruary 12th, 2012 at 2:28 pm

Does not work unless I translate the image afterwards…

Leave a comment

Your comment