Suggested addition to ColourGradient

template <typename RectangleType>
static ColourGradient createVerticalGradient(const Colour & topColour, const Colour & bottomColour, const Rectangle<RectangleType> & area)
    return ColourGradient(topColour, 0.0f, float(area.getY()), 
        bottomColour, 0.0f, float(area.getBottom()), false);

Given how much UI work seems to involve a subtle vertical shading, how about this as an addition to ColourGradient maybe…


Yes, good idea! Will add one!

Cool man - will save a lot of messy calls to the existing constructor.

Noticed and appreciated the addition, but still I think @jimc’s idea of having a method that takes top and bottom colour and an area, would save even more typing in many cases.

Would you consider adding also

template <typename CoordType>
ColourGradient::vertical (Colour top, Colour bottom, Rectangle <CoordType> area);

and the corresponring horizontal()?

Thank you

Yeah, when I looked at all the places I’d used it, I didn’t see anywhere that would have benefitted from that method, but yup, good FR, I’ll probably throw that in there too.

Wouldn’t something like
static ColourGradient vertical (Colour colour1, Colour colour2, Range<float> verticalRange);
cover both cases?

Then you could use it like:
auto cg = ColourGradient::vertical (Colours::black, Colours::white, { 0.0f, 100.0f });

auto cg = ColourGradient::vertical (Colours::black, Colours::white, 

good catch dave96!

I’m a little ashamed for not having seen this myself, because it was me requesting the Rectangle::getVerticalRange() methods in the first place :blush:

1 Like

I liked mine better for killing the typing, but the range is a less confusing interface probably. Probably should be a template so that cast to float isn’t needed there I guess?

Yeah, it could be a template. I guess I was just sticking with the type that the ColourGradient stores its coords in internally.

Yeah, but as a user you often have int Rectangles for components…

I went for the colour, float approach because with gradients you might want them to go in the opposite direction, e.g. vertical (bottomColour, 100, topColour, 0), which would be confusing if you had to convert it to a Range

Yeah but if you did that wouldn’t you just reverse the colours?

Yes, but personally I don’t always want to think of the gradient as top-to-bottom, it’s just between two colours, each with a coordinate, and the order that you give them doesn’t really seem like it should matter.