1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:07:34 +00:00

LibGfx: Add alternate_color to draw_line

This alternate_color can be used when drawing dashed lines to have two
alternating Colors.
This commit is contained in:
Tobias Christiansen 2021-08-05 16:34:03 +02:00 committed by Ali Mohammad Pur
parent 4a57a4a0b3
commit 61a1122c2d
2 changed files with 14 additions and 2 deletions

View file

@ -1631,7 +1631,7 @@ void Painter::draw_physical_pixel(const IntPoint& physical_position, Color color
fill_physical_rect(rect, color); fill_physical_rect(rect, color);
} }
void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color, int thickness, LineStyle style) void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color, int thickness, LineStyle style, Color alternate_color)
{ {
if (color.alpha() == 0) if (color.alpha() == 0)
return; return;
@ -1645,6 +1645,8 @@ void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color,
auto point2 = to_physical(p2); auto point2 = to_physical(p2);
thickness *= scale(); thickness *= scale();
auto alternate_color_is_transparent = alternate_color == Color::Transparent;
// Special case: vertical line. // Special case: vertical line.
if (point1.x() == point2.x()) { if (point1.x() == point2.x()) {
const int x = point1.x(); const int x = point1.x();
@ -1666,6 +1668,11 @@ void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color,
draw_physical_pixel({ x, y }, color, thickness); draw_physical_pixel({ x, y }, color, thickness);
draw_physical_pixel({ x, min(y + thickness, max_y) }, color, thickness); draw_physical_pixel({ x, min(y + thickness, max_y) }, color, thickness);
draw_physical_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness); draw_physical_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
if (!alternate_color_is_transparent) {
draw_physical_pixel({ x, min(y + thickness * 3, max_y) }, alternate_color, thickness);
draw_physical_pixel({ x, min(y + thickness * 4, max_y) }, alternate_color, thickness);
draw_physical_pixel({ x, min(y + thickness * 5, max_y) }, alternate_color, thickness);
}
} }
} else { } else {
for (int y = min_y; y <= max_y; y += thickness) for (int y = min_y; y <= max_y; y += thickness)
@ -1695,6 +1702,11 @@ void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color,
draw_physical_pixel({ x, y }, color, thickness); draw_physical_pixel({ x, y }, color, thickness);
draw_physical_pixel({ min(x + thickness, max_x), y }, color, thickness); draw_physical_pixel({ min(x + thickness, max_x), y }, color, thickness);
draw_physical_pixel({ min(x + thickness * 2, max_x), y }, color, thickness); draw_physical_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
if (!alternate_color_is_transparent) {
draw_physical_pixel({ min(x + thickness * 3, max_x), y }, alternate_color, thickness);
draw_physical_pixel({ min(x + thickness * 4, max_x), y }, alternate_color, thickness);
draw_physical_pixel({ min(x + thickness * 5, max_x), y }, alternate_color, thickness);
}
} }
} else { } else {
for (int x = min_x; x <= max_x; x += thickness) for (int x = min_x; x <= max_x; x += thickness)

View file

@ -51,7 +51,7 @@ public:
void draw_ellipse_intersecting(const IntRect&, Color, int thickness = 1); void draw_ellipse_intersecting(const IntRect&, Color, int thickness = 1);
void set_pixel(const IntPoint&, Color); void set_pixel(const IntPoint&, Color);
void set_pixel(int x, int y, Color color) { set_pixel({ x, y }, color); } void set_pixel(int x, int y, Color color) { set_pixel({ x, y }, color); }
void draw_line(const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid); void draw_line(const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
void draw_quadratic_bezier_curve(const IntPoint& control_point, const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid); void draw_quadratic_bezier_curve(const IntPoint& control_point, const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void draw_elliptical_arc(const IntPoint& p1, const IntPoint& p2, const IntPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid); void draw_elliptical_arc(const IntPoint& p1, const IntPoint& p2, const IntPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void blit(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f, bool apply_alpha = true); void blit(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f, bool apply_alpha = true);