Suggested addition to ColourGradient


#1
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…


#2

Yes, good idea! Will add one!


#3

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


#4

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


#5

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.


#6

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 });
or

auto cg = ColourGradient::vertical (Colours::black, Colours::white, 
                                    getLocalBounds().toFloat().getVerticalRange());

#7

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:


#8

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?


#9

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


#10

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


#11

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


#12

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


#13

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.