mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
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:
parent
8f225acf58
commit
978a70ddcc
Notes:
sideshowbarker
2024-07-17 08:37:36 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/978a70ddcc Pull-request: https://github.com/SerenityOS/serenity/pull/15123 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/gmta Reviewed-by: https://github.com/linusg ✅
3 changed files with 115 additions and 0 deletions
41
Userland/Libraries/LibGfx/Filters/BrightnessFilter.h
Normal file
41
Userland/Libraries/LibGfx/Filters/BrightnessFilter.h
Normal 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()
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
}
|
41
Userland/Libraries/LibGfx/Filters/ContrastFilter.h
Normal file
41
Userland/Libraries/LibGfx/Filters/ContrastFilter.h
Normal 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()
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
}
|
33
Userland/Libraries/LibGfx/Filters/OpacityFilter.h
Normal file
33
Userland/Libraries/LibGfx/Filters/OpacityFilter.h
Normal 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);
|
||||
};
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue