diff options
author | MacDue <macdue@dueutil.tech> | 2022-09-15 08:31:24 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-09-16 10:50:48 +0100 |
commit | 8f225acf586be64d53a4ca88a498b467833524ce (patch) | |
tree | 469bd1329ffe27173d55e58bb7315decbbfa6dcd /Userland | |
parent | 7bc0c662908aa21654e7ac093dc1a146f0142724 (diff) | |
download | serenity-8f225acf586be64d53a4ca88a498b467833524ce.zip |
LibGfx: Allow applying all color filters with an amount
This amount can be handled in the filter's implementation or if
not it will default to mixing between the new and previous pixel.
This behaviour is used for implementing CSS filters that allow stuff
like grayscale(70%).
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/ColorFilter.h | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/InvertFilter.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/SepiaFilter.h | 13 |
4 files changed, 20 insertions, 12 deletions
diff --git a/Userland/Libraries/LibGfx/Filters/ColorFilter.h b/Userland/Libraries/LibGfx/Filters/ColorFilter.h index 248eb8113c..9a5623fb45 100644 --- a/Userland/Libraries/LibGfx/Filters/ColorFilter.h +++ b/Userland/Libraries/LibGfx/Filters/ColorFilter.h @@ -12,8 +12,18 @@ namespace Gfx { class ColorFilter : public Filter { public: + ColorFilter(float amount = 1.0f) + : m_amount(amount) + { + } + virtual ~ColorFilter() = default; + virtual bool amount_handled_in_filter() const + { + return false; + } + virtual void apply(Bitmap& target_bitmap, IntRect const& target_rect, Bitmap const& source_bitmap, IntRect const& source_rect) override { VERIFY(source_rect.size() == target_rect.size()); @@ -30,15 +40,14 @@ public: auto source_pixel = source_bitmap.get_pixel(source_x, source_y); auto target_color = convert_color(source_pixel); - target_bitmap.set_pixel(target_x, target_y, target_color); + target_bitmap.set_pixel(target_x, target_y, m_amount < 1.0f && !amount_handled_in_filter() ? source_pixel.mixed_with(target_color, m_amount) : target_color); } } } protected: - ColorFilter() = default; - virtual Color convert_color(Color) = 0; + float m_amount { 1.0f }; }; } diff --git a/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h b/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h index 59c32f04f3..64050b2578 100644 --- a/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h +++ b/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h @@ -13,7 +13,7 @@ namespace Gfx { class GrayscaleFilter : public ColorFilter { public: - GrayscaleFilter() = default; + using ColorFilter::ColorFilter; virtual ~GrayscaleFilter() = default; virtual StringView class_name() const override { return "GrayscaleFilter"sv; } diff --git a/Userland/Libraries/LibGfx/Filters/InvertFilter.h b/Userland/Libraries/LibGfx/Filters/InvertFilter.h index d0b2251bb7..991070fac7 100644 --- a/Userland/Libraries/LibGfx/Filters/InvertFilter.h +++ b/Userland/Libraries/LibGfx/Filters/InvertFilter.h @@ -13,7 +13,7 @@ namespace Gfx { class InvertFilter : public ColorFilter { public: - InvertFilter() = default; + using ColorFilter::ColorFilter; virtual ~InvertFilter() = default; virtual StringView class_name() const override { return "InvertFilter"sv; } diff --git a/Userland/Libraries/LibGfx/Filters/SepiaFilter.h b/Userland/Libraries/LibGfx/Filters/SepiaFilter.h index 9b94a9e8e0..86ca152cd0 100644 --- a/Userland/Libraries/LibGfx/Filters/SepiaFilter.h +++ b/Userland/Libraries/LibGfx/Filters/SepiaFilter.h @@ -15,19 +15,18 @@ namespace Gfx { class SepiaFilter : public ColorFilter { public: - SepiaFilter(float amount = 1.0f) - : m_amount(amount) - { - } + using ColorFilter::ColorFilter; virtual ~SepiaFilter() = default; virtual StringView class_name() const override { return "SepiaFilter"sv; } + virtual bool amount_handled_in_filter() const override + { + return true; + } + protected: Color convert_color(Color original) override { return original.sepia(m_amount); }; - -private: - float m_amount; }; } |