mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-28 05:52:53 +00:00
LibWeb+LibGfx: Migrate (most of) the CSS gradient painting to LibGfx
This moves the CSS gradient painting to the painter creating: - Painter::fill_rect_with_linear_gradient() - Painter::fill_rect_with_conic_gradient() - Painter::fill_rect_with_radial_gradient() This has a few benefits: - The gradients can now easily respect the painter scale - The Painter::fill_pixels() escape hatch can be removed - We can remove the old fixed color stop gradient code - The old functions are now just a shim - Anywhere can now easily use this gradient painting code! This only leaves the color stop resolution in LibWeb (which is fine). Just means in LibGfx you have to actually specify color stop positions. (Also while here add a small optimization to avoid generating excessively long gradient lines)
This commit is contained in:
parent
668204041b
commit
c8c065b6b0
Notes:
sideshowbarker
2024-07-17 03:30:41 +09:00
Author: https://github.com/MacDue
Commit: c8c065b6b0
Pull-request: https://github.com/SerenityOS/serenity/pull/16938
7 changed files with 254 additions and 241 deletions
|
@ -235,47 +235,7 @@ void Painter::fill_rect_with_gradient(Orientation orientation, IntRect const& a_
|
|||
fill_rect(a_rect, gradient_start);
|
||||
return;
|
||||
}
|
||||
|
||||
auto rect = to_physical(a_rect);
|
||||
auto clipped_rect = IntRect::intersection(rect, clip_rect() * scale());
|
||||
if (clipped_rect.is_empty())
|
||||
return;
|
||||
|
||||
int offset = clipped_rect.primary_offset_for_orientation(orientation) - rect.primary_offset_for_orientation(orientation);
|
||||
|
||||
ARGB32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
|
||||
size_t const dst_skip = m_target->pitch() / sizeof(ARGB32);
|
||||
|
||||
float increment = (1.0 / ((rect.primary_size_for_orientation(orientation))));
|
||||
float alpha_increment = increment * ((float)gradient_end.alpha() - (float)gradient_start.alpha());
|
||||
|
||||
if (orientation == Orientation::Horizontal) {
|
||||
for (int i = clipped_rect.height() - 1; i >= 0; --i) {
|
||||
float c = offset * increment;
|
||||
float c_alpha = gradient_start.alpha() + offset * alpha_increment;
|
||||
for (int j = 0; j < clipped_rect.width(); ++j) {
|
||||
auto color = gamma_accurate_blend(gradient_start, gradient_end, c);
|
||||
color.set_alpha(c_alpha);
|
||||
dst[j] = Color::from_argb(dst[j]).blend(color).value();
|
||||
c_alpha += alpha_increment;
|
||||
c += increment;
|
||||
}
|
||||
dst += dst_skip;
|
||||
}
|
||||
} else {
|
||||
float c = offset * increment;
|
||||
float c_alpha = gradient_start.alpha() + offset * alpha_increment;
|
||||
for (int i = clipped_rect.height() - 1; i >= 0; --i) {
|
||||
auto color = gamma_accurate_blend(gradient_start, gradient_end, c);
|
||||
color.set_alpha(c_alpha);
|
||||
for (int j = 0; j < clipped_rect.width(); ++j) {
|
||||
dst[j] = Color::from_argb(dst[j]).blend(color).value();
|
||||
}
|
||||
c_alpha += alpha_increment;
|
||||
c += increment;
|
||||
dst += dst_skip;
|
||||
}
|
||||
}
|
||||
return fill_rect_with_linear_gradient(a_rect, Array { ColorStop { gradient_start, 0 }, ColorStop { gradient_end, 1 } }, orientation == Orientation::Horizontal ? 90.0f : 0.0f);
|
||||
}
|
||||
|
||||
void Painter::fill_rect_with_gradient(IntRect const& a_rect, Color gradient_start, Color gradient_end)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue