LibGfx: Add BrightnessFilter, ContrastFilter, and OpacityFilter

These filters are based off the ones defined in:
https://drafts.fxtf.org/filter-effects-1/#supported-filter-functions
This commit is contained in:
MacDue 2022-09-15 08:31:27 +01:00 committed by Sam Atkins
parent 8f225acf58
commit 978a70ddcc
Notes: sideshowbarker 2024-07-17 08:37:36 +09:00
3 changed files with 115 additions and 0 deletions

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
#include <LibGfx/Filters/ColorFilter.h>
namespace Gfx {
class BrightnessFilter : public ColorFilter {
public:
using ColorFilter::ColorFilter;
virtual ~BrightnessFilter() = default;
virtual StringView class_name() const override { return "BrightnessFilter"sv; }
virtual bool amount_handled_in_filter() const override
{
return true;
}
protected:
Color convert_color(Color original) override
{
auto convert_channel = [&](u8 channel) {
return static_cast<u8>(clamp(round_to<int>(channel * m_amount), 0, 255));
};
return Gfx::Color {
convert_channel(original.red()),
convert_channel(original.green()),
convert_channel(original.blue()),
original.alpha()
};
};
};
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
#include <LibGfx/Filters/ColorFilter.h>
namespace Gfx {
class ContrastFilter : public ColorFilter {
public:
using ColorFilter::ColorFilter;
virtual ~ContrastFilter() = default;
virtual StringView class_name() const override { return "ContrastFilter"sv; }
virtual bool amount_handled_in_filter() const override
{
return true;
}
protected:
Color convert_color(Color original) override
{
auto convert_channel = [&](u8 channel) {
return static_cast<u8>(clamp(round_to<int>(channel * m_amount + (-128 * m_amount) + 128), 0, 255));
};
return Gfx::Color {
convert_channel(original.red()),
convert_channel(original.green()),
convert_channel(original.blue()),
original.alpha()
};
};
};
}

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
#include <LibGfx/Filters/ColorFilter.h>
namespace Gfx {
class OpacityFilter : public ColorFilter {
public:
using ColorFilter::ColorFilter;
virtual ~OpacityFilter() = default;
virtual StringView class_name() const override { return "OpacityFilter"sv; }
virtual bool amount_handled_in_filter() const override
{
return true;
}
protected:
Color convert_color(Color original) override
{
return original.with_alpha(m_amount * 255);
};
};
}