diff options
author | MacDue <macdue@dueutil.tech> | 2022-09-15 08:31:27 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-09-16 10:50:48 +0100 |
commit | 978a70ddcc7b7c6ee7125717200d1ba21a3c8785 (patch) | |
tree | 36e937efb8f5843dccaebe6a3c8bb6f0f394f904 | |
parent | 8f225acf586be64d53a4ca88a498b467833524ce (diff) | |
download | serenity-978a70ddcc7b7c6ee7125717200d1ba21a3c8785.zip |
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
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/BrightnessFilter.h | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/ContrastFilter.h | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/OpacityFilter.h | 33 |
3 files changed, 115 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/Filters/BrightnessFilter.h b/Userland/Libraries/LibGfx/Filters/BrightnessFilter.h new file mode 100644 index 0000000000..85303ba9ae --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/BrightnessFilter.h @@ -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() + }; + }; +}; + +} diff --git a/Userland/Libraries/LibGfx/Filters/ContrastFilter.h b/Userland/Libraries/LibGfx/Filters/ContrastFilter.h new file mode 100644 index 0000000000..5bcb6764df --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/ContrastFilter.h @@ -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() + }; + }; +}; + +} diff --git a/Userland/Libraries/LibGfx/Filters/OpacityFilter.h b/Userland/Libraries/LibGfx/Filters/OpacityFilter.h new file mode 100644 index 0000000000..5e16907355 --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/OpacityFilter.h @@ -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); + }; +}; + +} |