From 064ca625dffe29c5a8f20efdd82fa52250f233e0 Mon Sep 17 00:00:00 2001 From: MacDue Date: Fri, 7 Apr 2023 02:15:02 +0100 Subject: [PATCH] LibGfx: Add BitmapPaintStyle This is a simple paint style for filling a path with a bitmap. --- Userland/Libraries/LibGfx/PaintStyle.h | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Userland/Libraries/LibGfx/PaintStyle.h b/Userland/Libraries/LibGfx/PaintStyle.h index 1abe6978b9..729e9b4285 100644 --- a/Userland/Libraries/LibGfx/PaintStyle.h +++ b/Userland/Libraries/LibGfx/PaintStyle.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,32 @@ private: Color m_color; }; +class BitmapPaintStyle : public PaintStyle { +public: + static ErrorOr> create(Bitmap const& bitmap, IntPoint offset = {}) + { + return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapPaintStyle(bitmap, offset)); + } + + virtual Color sample_color(IntPoint point) const override + { + point += m_offset; + if (m_bitmap->rect().contains(point)) + return m_bitmap->get_pixel(point); + return Color(); + } + +private: + BitmapPaintStyle(Bitmap const& bitmap, IntPoint offset) + : m_bitmap(bitmap) + , m_offset(offset) + { + } + + NonnullRefPtr m_bitmap; + IntPoint m_offset; +}; + class GradientPaintStyle : public PaintStyle { public: ErrorOr add_color_stop(float position, Color color, Optional transition_hint = {})