I'm getting comfused with ColourGradient


I’m trying to make sense of the ColourGradient class, but I’m going round in circles.

If I want to use a ColourGradient as a brush for a Graphics::fillPath() or Graphics::fillRect() operation, what do I need to do?

Let’s say I have:

	ColourGradient cg;
	cg.addColour(0, Colours::white);
	cg.addColour(0.5f, Colours::red);
	cg.addColour(1.0f, Colours::black);

       	GradientBrush gb(cg); 


	g.fillRect(0, 0, width, height);

assuming g is my current Graphics context, and width and height are the component size.

This doesn’t work. I guess I need to scale the ColourGradient, but I’ve tried assigning an affine transform to cg.transform, and I’ve tried using gb.applyTransform() but neither seem to be doing anything helpful.

Typically all that I see is the last colour in my gradient list - which would make sense I guess[1] if the gradient was occupying just one pixel.

What am I totally failing to grasp here?

[1] though I’d have thought that the first rasterline would be greyish if so.


Hi there valley

If you use the ColourGradient constructor with no arguments, you’ll also need to set all its member variables - i.e. the x, y coords of its endpoints. Best to use the other constructor to create some endpoints and end colours, then add your extra points in between them.

Apart from that it looks fine.


Ahh, I was completely missing the point of what the xys were doing.

It’s all so much easier now! :slight_smile:

Cheers, Jules.