diff --git a/Userland/Libraries/LibGfx/Filters/HueRotateFilter.h b/Userland/Libraries/LibGfx/Filters/HueRotateFilter.h index a4350ded35..7b61a37453 100644 --- a/Userland/Libraries/LibGfx/Filters/HueRotateFilter.h +++ b/Userland/Libraries/LibGfx/Filters/HueRotateFilter.h @@ -6,17 +6,14 @@ #pragma once -#include -#include -#include +#include namespace Gfx { -class HueRotateFilter : public ColorFilter { +class HueRotateFilter : public MatrixFilter { public: HueRotateFilter(float angle_degrees) - : ColorFilter(angle_degrees) - , m_operation(calculate_operation_matrix(angle_degrees)) + : MatrixFilter(calculate_hue_rotate_matrix(angle_degrees)) { } @@ -25,37 +22,12 @@ public: return true; } - float angle_degrees() const - { - return m_amount; - } - virtual StringView class_name() const override { return "HueRotateFilter"sv; } -protected: - Color convert_color(Color original) override - { - FloatVector3 rgb = { - original.red() / 256.0f, - original.green() / 256.0f, - original.blue() / 256.0f, - }; - rgb = m_operation * rgb; - auto safe_float_to_u8 = [](float value) -> u8 { - return static_cast(AK::clamp(value, 0.0f, 1.0f) * 256); - }; - return Color { - safe_float_to_u8(rgb[0]), - safe_float_to_u8(rgb[1]), - safe_float_to_u8(rgb[2]), - original.alpha() - }; - } - private: - static FloatMatrix3x3 calculate_operation_matrix(float angle) + static FloatMatrix3x3 calculate_hue_rotate_matrix(float angle_degrees) { - float angle_rads = angle * (AK::Pi / 180); + float angle_rads = angle_degrees * (AK::Pi / 180); float cos_angle = 0; float sin_angle = 0; AK::sincos(angle_rads, sin_angle, cos_angle); @@ -77,8 +49,6 @@ private: }; // clang-format on } - - FloatMatrix3x3 m_operation; }; } diff --git a/Userland/Libraries/LibGfx/Filters/MatrixFilter.h b/Userland/Libraries/LibGfx/Filters/MatrixFilter.h new file mode 100644 index 0000000000..0f88e4801d --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/MatrixFilter.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022, MacDue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Gfx { + +class MatrixFilter : public ColorFilter { +public: + MatrixFilter(FloatMatrix3x3 operation, float amount = 1.0f) + : ColorFilter(amount) + , m_operation(operation) + { + } + +protected: + Color convert_color(Color original) override + { + auto constexpr u8_max = AK::NumericLimits::max(); + auto safe_float_to_u8 = [](float value) -> u8 { + return AK::clamp(static_cast(value * u8_max), 0, u8_max); + }; + FloatVector3 rgb = { + original.red() / float(u8_max), + original.green() / float(u8_max), + original.blue() / float(u8_max), + }; + rgb = m_operation * rgb; + return Color { + safe_float_to_u8(rgb[0]), + safe_float_to_u8(rgb[1]), + safe_float_to_u8(rgb[2]), + original.alpha() + }; + } + +private: + FloatMatrix3x3 const m_operation; +}; + +}