Skip to content

Commit cbd7eff

Browse files
committed
LibGfx: Support vertical gradient fill (not just horizontal) :^)
1 parent 098f1cd commit cbd7eff

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

Libraries/LibGfx/Painter.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void Painter::fill_rect(const Rect& a_rect, Color color)
141141
}
142142
}
143143

144-
void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
144+
void Painter::fill_rect_with_gradient(Orientation orientation, const Rect& a_rect, Color gradient_start, Color gradient_end)
145145
{
146146
#ifdef NO_FPU
147147
return fill_rect(a_rect, gradient_start);
@@ -151,12 +151,12 @@ void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start,
151151
if (clipped_rect.is_empty())
152152
return;
153153

154-
int x_offset = clipped_rect.x() - rect.x();
154+
int offset = clipped_rect.primary_offset_for_orientation(orientation) - rect.primary_offset_for_orientation(orientation);
155155

156156
RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
157157
const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
158158

159-
float increment = (1.0 / ((rect.width()) / 255.0));
159+
float increment = (1.0 / ((rect.primary_size_for_orientation(orientation)) / 255.0));
160160

161161
int r2 = gradient_start.red();
162162
int g2 = gradient_start.green();
@@ -165,20 +165,40 @@ void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start,
165165
int g1 = gradient_end.green();
166166
int b1 = gradient_end.blue();
167167

168-
for (int i = clipped_rect.height() - 1; i >= 0; --i) {
169-
float c = x_offset * increment;
170-
for (int j = 0; j < clipped_rect.width(); ++j) {
171-
dst[j] = Color(
168+
if (orientation == Orientation::Horizontal) {
169+
for (int i = clipped_rect.height() - 1; i >= 0; --i) {
170+
float c = offset * increment;
171+
for (int j = 0; j < clipped_rect.width(); ++j) {
172+
dst[j] = Color(
173+
r1 / 255.0 * c + r2 / 255.0 * (255 - c),
174+
g1 / 255.0 * c + g2 / 255.0 * (255 - c),
175+
b1 / 255.0 * c + b2 / 255.0 * (255 - c))
176+
.value();
177+
c += increment;
178+
}
179+
dst += dst_skip;
180+
}
181+
} else {
182+
float c = offset * increment;
183+
for (int i = clipped_rect.height() - 1; i >= 0; --i) {
184+
Color color(
172185
r1 / 255.0 * c + r2 / 255.0 * (255 - c),
173186
g1 / 255.0 * c + g2 / 255.0 * (255 - c),
174-
b1 / 255.0 * c + b2 / 255.0 * (255 - c))
175-
.value();
187+
b1 / 255.0 * c + b2 / 255.0 * (255 - c));
188+
for (int j = 0; j < clipped_rect.width(); ++j) {
189+
dst[j] = color.value();
190+
}
176191
c += increment;
192+
dst += dst_skip;
177193
}
178-
dst += dst_skip;
179194
}
180195
}
181196

197+
void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
198+
{
199+
return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
200+
}
201+
182202
void Painter::draw_ellipse_intersecting(const Rect& rect, Color color, int thickness)
183203
{
184204
constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size

Libraries/LibGfx/Painter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Painter {
4545
~Painter();
4646
void clear_rect(const Rect&, Color);
4747
void fill_rect(const Rect&, Color);
48+
void fill_rect_with_gradient(Orientation, const Rect&, Color gradient_start, Color gradient_end);
4849
void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
4950
void draw_rect(const Rect&, Color, bool rough = false);
5051
void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());

0 commit comments

Comments
 (0)