I think I found a bug with alpha in graphics contexts


#1

Using Graphics::setColour makes any calls to DrawableImage::drawAt(…) use the previously set alpha value in the graphics context. Maybe this is by design but it feels weird, seing as we normally use Graphics::beginTransparencyLayer() if we want to use alpha on something.
Small example where “testBitmap” is a fully opaque DrawableImage that produces this result:

https://postimg.org/image/svquwcg5b/

int xpos = 0, ypos = 0;
int w = 100, h = 25;

g.setColour(Colour::fromRGBA(20, 20, 20, 255));
testBitmap->drawAt(g, xpos, ypos, 1.f);
g.setColour(Colour::fromFloatRGBA(255, 255, 255, 0.8f));
g.drawText(“Hello”, xpos,ypos,w,h,Justification::left);

ypos += h;
testBitmap->drawAt(g, xpos, ypos, 1.f);
g.setColour(Colour::fromFloatRGBA(255, 255, 255, 0.6f));
g.drawText(“Hello”, xpos, ypos, w, h, Justification::left);

ypos += h;
testBitmap->drawAt(g, xpos, ypos, 1.f);
g.setColour(Colour::fromFloatRGBA(255, 255, 255, 0.4f));
g.drawText(“Hello”, xpos, ypos, w, h, Justification::left);

ypos += h;
testBitmap->drawAt(g, xpos, ypos, 1.f);
g.setColour(Colour::fromFloatRGBA(255, 255, 255, 0.2f));
g.drawText(“Hello”, xpos, ypos, w, h, Justification::left);

I would expect a colour set to draw something in the context to not be used when drawing images, since well, the colour is not actually used but in this case the alpha channel from the colour is still used.
If I want a active alpha channel for my images i would normally use begin and end transparency layer.


#2

It’s by design - the image is drawn with the current alpha of the graphics context.

/** Draws an image, having applied an affine transform to it.

    This lets you throw the image around in some wacky ways, rotate it, shear,
    scale it, etc.

    Images are composited using the context's current opacity, so if you
    don't want it to be drawn semi-transparently, be sure to call setOpacity (1.0f)
    (or setColour() with an opaque colour) before drawing images.

    If fillAlphaChannelWithCurrentBrush is set to true, then the image's RGB channels
    are ignored and it is filled with the current brush, masked by its alpha channel.

    If you want to render only a subsection of an image, use Image::getClippedImage() to
    create the section that you need.

    @see setImageResamplingQuality, drawImage
*/

#3

That’d do the job, but in a very very inefficient way!


#4

Oh, i completely missed that comment section, it kinda explained it in detail hehe :slight_smile: Sorry about that.

Yeah, now that Im aware of the “bug” I can start using it to my advantage! :slight_smile:

Thanks for your quick reply!