What is the best way to add a solid background to a sprite?

Dear Codea Community,

I have a series of sprites which consist of black plots(graphs; i.e. axes and points are black with a clear background).

Since I want the graphs to show up well, I’d like to add a white background to those black sprites.

In addition, I’d like to still keep the rest of the screen black to render other graphical images on directly.

What would be the best way to do this? I see NewImage = myImage:copy() in the Codea documentation, but I believe that actually erases any data that might have been in the NewImage (e.g. a white background). I can’t use setContext(sprite) background(255,255,255) setContext() since that would seem to clear out the black graph information in the images. I tried using setContext(sprite) fill(255,255,255,0) and then drawing a rectangle the size of the sprite but that didn’t seem to do what I want when I finally rendered the sprite (black graph with white rectangle, alpha 0) on a black Codea screen background (much of the black graph information was lost).

Would appreciate anyone’s input/code suggestion of how to add solid backgrounds to otherwise transparent black sprites and then render them on a Codea black background.

Thanks! :blush:

I do think I figured out one way to do this (courtesy of an old post from 2011) by rendering a Sprite onto an image (e.g. the following code snippet):

-- plot is pre-obtained plot/graph Sprite with black markings
        local w,h = spriteSize(plot)
        backgroundSprite = image(w,h) --set backgroundSprite to size of plot Sprite
        setContext(backgroundSprite) -- sets backgroundSprite to be drawn on
        background(255,255,255,0) --set backgroundSprite to white, alpha 0
        spriteMode(CORNER) -- seems easiest to specify corner to paste sprite on 
        sprite(plot,0,0) -- plot per above code is same size as backgroundSprite
        setContext() -- set context back to render on Codea screen
        spriteMode(CENTER) -- change to centering for drawing sprite on screen 
        -- render now combined sprite (backgroundSprite with plot Sprite written
        -- onto it) onto Codea's screen at the midpoint of display at size 600 x 600

Happy to learn from others if better ways to solve this problem

@SugarRay - yep your right, that’s the way to do it. You just have to be careful that you keep your drawing within the limits of w and h.

Note - treating this then as a sprite enables you to rotate the image you created, which can be handy.

1 Like

Thanks a lot, @Bri_G! :blush: