Big templatization. Fix for interpolation.
This commit is contained in:
45
color.h
45
color.h
@@ -2,23 +2,48 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#include "intmath.h"
|
||||
|
||||
constexpr uint32_t kNumColors = UINT16_MAX;
|
||||
constexpr uint32_t kMaxColor = UINT16_MAX;
|
||||
|
||||
// 32-bit for compiler convenience, but values are 16-bit
|
||||
struct Color : public std::array<uint32_t, 3> {
|
||||
constexpr uint32_t Difference(const Color& other) const;
|
||||
template <uint32_t C>
|
||||
struct Color : public std::array<uint32_t, C> {
|
||||
constexpr uint32_t Difference(const Color<C>& other) const;
|
||||
constexpr Color<C> Interpolate(const Color<C>& other, uint32_t mul, uint32_t div) const;
|
||||
};
|
||||
|
||||
constexpr uint32_t Color::Difference(const Color& other) const {
|
||||
return (
|
||||
AbsDiff(this->at(0), other.at(0)) +
|
||||
AbsDiff(this->at(1), other.at(1)) +
|
||||
AbsDiff(this->at(2), other.at(2))
|
||||
);
|
||||
struct RgbColor : public Color<3> {};
|
||||
|
||||
template <uint32_t C>
|
||||
constexpr uint32_t Color<C>::Difference(const Color<C>& other) const {
|
||||
uint32_t diff = 0;
|
||||
for (uint32_t c = 0; c < C; ++c) {
|
||||
diff += AbsDiff(this->at(c), other.at(c));
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Color& color);
|
||||
template <uint32_t C>
|
||||
constexpr Color<C> Color<C>::Interpolate(const Color<C>& other, uint32_t mul, uint32_t div) const {
|
||||
Color<C> ret;
|
||||
for (uint32_t c = 0; c < C; ++c) {
|
||||
ret.at(c) = ::Interpolate(this->at(c), other.at(c), mul, div);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <uint32_t C>
|
||||
std::ostream& operator<<(std::ostream& os, const Color<C>& color) {
|
||||
os << std::hex << std::setfill('0') << "Color(";
|
||||
for (uint32_t c = 0; c < C; ++c) {
|
||||
os << "0x" << std::setw(4) << color.at(0);
|
||||
if (c < C - 1) {
|
||||
os << ", ";
|
||||
}
|
||||
}
|
||||
return os << ")" << std::dec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user