1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

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%).
This commit is contained in:
MacDue 2022-09-15 08:31:24 +01:00 committed by Sam Atkins
parent 7bc0c66290
commit 8f225acf58
4 changed files with 20 additions and 12 deletions

View file

@ -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 };
};
}

View file

@ -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; }

View file

@ -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; }

View file

@ -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;
};
}