public api before private members

This commit is contained in:
Andrea Blankenstijn 2021-08-15 16:13:05 +02:00
parent 2c344ecee5
commit 07f7061c20
1 changed files with 42 additions and 43 deletions

View File

@ -14,56 +14,18 @@ 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}
: sdl_type {r, g, b, a},
_operate_on_alpha {op_on_alpha},
_operate_on_colors {op_on_colors}
{}
inline Color(SDL_Color c = {}, bool op_on_alpha = false,
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)
: sdl_type(c), _operate_on_alpha(op_on_alpha), _operate_on_colors(op_on_colors)
{}
Color(const Color&) noexcept = default;
Color(Color&&) noexcept = default;
@ -79,6 +41,16 @@ namespace bwidgets
return {sdl_type, true, _operate_on_alpha};
}
[[nodiscard]] auto& operator()() noexcept
{
return sdl_type;
}
[[nodiscard]] const auto& operator()() const noexcept
{
return sdl_type;
}
template<Numeric N>
[[nodiscard]] auto operator+(N operand) const noexcept
{
@ -127,6 +99,33 @@ namespace bwidgets
&& ((_operate_on_alpha && sdl_type.a != other().a)
|| !_operate_on_alpha);
}
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;
}
};
}