123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #ifndef CORE_COLOR_HPP
- #define CORE_COLOR_HPP
- namespace Core {
- using ColorChannel = unsigned char;
- template<int N>
- class Color final {
- static_assert(N >= 1 && N <= 4, "a color has 1 to 4 channels");
- ColorChannel data[static_cast<unsigned int>(N)] = {};
- public:
- Color() = default;
- template<typename OT, typename... Args>
- Color(OT a, Args&&... args) {
- init<0>(a, args...);
- }
- private:
- template<int I>
- void init() {
- static_assert(I == N,
- "color channel parameters do not match its size");
- }
- template<int I, typename OT, typename... Args>
- void init(OT a, Args&&... args) {
- data[I] = static_cast<ColorChannel>(a);
- init<I + 1>(args...);
- }
- public:
- float asFloat(int index) const {
- return data[index] * (1.0f / 255.0f);
- }
- ColorChannel& operator[](int index) {
- return data[index];
- }
- const ColorChannel& operator[](int index) const {
- return data[index];
- }
- };
- using Color4 = Color<4>;
- using Color3 = Color<3>;
- using Color2 = Color<2>;
- using Color1 = Color<1>;
- }
- #endif
|