Feature requests: Optimized gradient functions


I was just doing some profiling, and it turns out gradient fills are pretty slow. But the gradient has lots of funky features I don’t really need. It would be nice if juce had some optimized gradient fill functions for the most general cases. This is one I use and it seems over 10x faster.

inline void fillVerticalGradient (Graphics& g, const ColourGradient& grad, Rectangle<int> r)
    const int h = r.getHeight();
    const int w = r.getWidth();
    const int x = r.getX();
    const int y = r.getY();

    for (int i = 0; i < h; i++)
        g.setColour (grad.getColourAtPosition (i / double (h)));
        g.fillRect (x, y + i, w, 1);


I remember I had an increase of performance with rectangles and drawVertical/HorizontalLine too. But the fastest way for me was to use OpenGL shaders.


This won’t take in to account hi-dpi displays though (whose scale factor is greater than 1.0). For that you’ll probably need to get the Graphics context’s internal scale factor and make your increment 1.0 / scaleFactor?
That probably means using a Rectangle too and I’m not sure if that would adversely affect performance.

Of course, a 1px increment may be fine (or perhaps even a 2px increment in some cases).

I’d check this on a variety of hardware though, it’s likely to be slower on some systems (most notably if using OpenGL).


Well this doesn’t have to be faster on any platform, some platforms e.g. CoreGraphics, also apply a dither.


I didn’t think about hi-dpi, I’ll need to test that and I don’t have one.