How to draw a vertical gradient?


#1

article removed


#2

Sorry Frank, but that code snippet looks like something you’d find in a programming exam! “Identify at least 10 bugs in the following code…” :stuck_out_tongue:

There’s no way it’d actually work properly - if you set your gradient position to the clip region, you’ll get artifacts everywhere when sub-regions of your component get painted! Not to mention the utterly bizarre idea of doing the work in the destructor!

Maybe a better version would be:

static void drawVerticalGradient (Graphics& g, const Rectangle<int>& area, const Colour& baseColour = Colours::grey) { g.setGradientFill (ColourGradient (baseColour.brighter (0.5f), 0, (float) area.getY(), baseColour.darker (0.5f), 0, (float) area.getBottom(), false)); g.fill (area); }


#3

Sorry, if you think this is a one-fits-all code.
I used this code to draw test components which have no child components.
Obviously the DTOR processing will not work, if you do additional things, because it only gets called when execution leaves the scope.
But I like the DTOR processing and used this also in my AutoLayout classes.
Sure, in all other cases you will need a separate method to call paint() explicitly.

I just wanted to be helpful and present some ideas and code in this forum,
no production code.


#4

[quote]I just wanted to be helpful and present some ideas and code in this forum,
no production code.[/quote]

And it’s much appreciated!