bwidgets/inc/basic_widgets/core/type/color.hpp

134 lines
4 KiB
C++

#ifndef BWIDGETS_COLOR_HPP
#define BWIDGETS_COLOR_HPP
#include <cmath>
#include <cstdint>
#include <functional>
#include <SDL2/SDL_assert.h>
#include <SDL2/SDL_pixels.h>
#include <basic_widgets/core/type/concepts.hpp>
namespace bwidgets
{
class Color final
{
public:
[[nodiscard]] auto& operator()() noexcept
{
return sdl_type;
}
[[nodiscard]] const auto& operator()() const noexcept
{
return sdl_type;
}
private:
bool _operate_on_alpha;
bool _operate_on_colors;
template<Numeric N>
auto _operate(N operand, std::function<N(N, N)> operator_) const noexcept
{
Color c(sdl_type, _operate_on_alpha, _operate_on_colors);
if (_operate_on_alpha) {
auto a = std::round(operator_(c().a, operand));
SDL_assert_release(a == (uint8_t)a); // NOLINT
c().a = (uint8_t)a;
}
if (_operate_on_colors) {
auto r = std::round(operator_(c().r, operand));
auto g = std::round(operator_(c().g, operand));
auto b = std::round(operator_(c().b, operand));
// NOLINTNEXTLINE
SDL_assert_release(r == (uint8_t)r && g == (uint8_t)g
&& b == (uint8_t)b);
c().r = (uint8_t)r;
c().g = (uint8_t)g;
c().b = (uint8_t)b;
}
return c;
}
public:
SDL_Color sdl_type;
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a, bool op_on_alpha = false,
bool op_on_colors = true) noexcept
: _operate_on_alpha {op_on_alpha},
_operate_on_colors {op_on_colors},
sdl_type {r, g, b, a}
{}
inline Color(SDL_Color c = {}, bool op_on_alpha = false,
bool op_on_colors = true) noexcept
: _operate_on_alpha(op_on_alpha), _operate_on_colors(op_on_colors), sdl_type(c)
{}
Color(const Color&) noexcept = default;
Color(Color&&) noexcept = default;
~Color() = default;
[[nodiscard]] auto alpha() const noexcept -> Color
{
return {sdl_type, _operate_on_colors, true};
}
[[nodiscard]] auto colors() const noexcept -> Color
{
return {sdl_type, true, _operate_on_alpha};
}
template<Numeric N>
[[nodiscard]] auto operator+(N operand) const noexcept
{
return _operate(operand, [](N a, N b) { return a + b; });
}
template<Numeric N>
[[nodiscard]] auto operator-(N operand) const noexcept
{
return _operate(operand, [](N a, N b) { return a - b; });
}
template<Numeric N>
[[nodiscard]] auto operator*(N operand) const noexcept
{
return _operate(operand, [](N a, N b) { return a * b; });
}
template<Numeric N>
[[nodiscard]] auto operator/(N operand) const noexcept
{
SDL_assert_release(operand != 0); // NOLINT
return _operate<N>(operand, [](N a, N b) { return a / b; });
}
auto operator=(const Color& c) noexcept -> Color& = default;
auto operator=(Color&&) noexcept -> Color& = default;
auto& operator=(SDL_Color c) noexcept
{
sdl_type = c;
return *this;
}
[[nodiscard]] auto operator==(Color other) const noexcept
{
return (_operate_on_colors && sdl_type.r == other().r
&& sdl_type.g == other().g && sdl_type.b == other().b)
|| (_operate_on_alpha && sdl_type.a == other().a);
}
[[nodiscard]] auto operator!=(Color other) const noexcept
{
return (sdl_type.r != other().r || sdl_type.g != other().g
|| sdl_type.b != other().b)
&& ((_operate_on_alpha && sdl_type.a != other().a)
|| !_operate_on_alpha);
}
};
}
#endif