Provide GenerateLinearGradient with several colors.

This commit is contained in:
John Preston 2021-09-06 13:17:13 +03:00
parent af1429cb87
commit db1b4b65c7
4 changed files with 40 additions and 4 deletions

View file

@ -821,7 +821,17 @@ QImage BlurLargeImage(QImage image, int radius) {
return QImage();
} else if (colors.size() > 2) {
return GenerateComplexGradient(size, colors, rotation, progress);
} else {
return GenerateLinearGradient(size, colors, rotation);
}
}
QImage GenerateLinearGradient(
QSize size,
const std::vector<QColor> &colors,
int rotation) {
Expects(!colors.empty());
auto result = QImage(size, QImage::Format_RGB32);
if (colors.size() == 1) {
result.fill(colors.front());
@ -846,10 +856,21 @@ QImage BlurLargeImage(QImage image, int radius) {
Unexpected("Rotation value in GenerateDitheredGradient.");
}();
auto gradient = QLinearGradient(start, finalStop);
gradient.setStops(QGradientStops{
{ 0.0, colors[0] },
{ 1.0, colors[1] }
});
if (colors.size() == 2) {
gradient.setStops(QGradientStops{
{ 0.0, colors[0] },
{ 1.0, colors[1] }
});
} else {
auto stops = QGradientStops();
const auto step = 1. / (colors.size() - 1);
auto point = 0.;
for (const auto color : colors) {
stops.append({ point, color });
point += step;
}
}
p.fillRect(QRect(QPoint(), size), QBrush(std::move(gradient)));
p.end();

View file

@ -35,6 +35,11 @@ namespace Images {
int rotation = 0,
float progress = 1.f);
[[nodiscard]] QImage GenerateLinearGradient(
QSize size,
const std::vector<QColor> &colors,
int rotation = 0);
[[nodiscard]] const std::array<QImage, 4> &CornersMask(
ImageRoundRadius radius);
[[nodiscard]] std::array<QImage, 4> PrepareCorners(

View file

@ -110,6 +110,15 @@ palette::SetResult palette::setColor(QLatin1String name, uchar r, uchar g, uchar
return duplicate ? SetResult::Duplicate : SetResult::Ok;
}
palette::SetResult palette::setColor(QLatin1String name, const QColor &color) {
auto r = 0;
auto g = 0;
auto b = 0;
auto a = 0;
color.getRgb(&r, &g, &b, &a);
return setColor(name, uchar(r), uchar(g), uchar(b), uchar(a));
}
palette::SetResult palette::setColor(QLatin1String name, QLatin1String from) {
const auto nameIndex = internal::GetPaletteIndex(name);
if (nameIndex < 0) {

View file

@ -30,6 +30,7 @@ public:
Duplicate,
};
SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);
SetResult setColor(QLatin1String name, const QColor &color);
SetResult setColor(QLatin1String name, QLatin1String from);
void reset(const colorizer &with);
void reset();