12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- #ifndef COLOR_H
- #define COLOR_H
- typedef unsigned char ColorChannel;
- template<int N>
- class Color {
- static_assert(N <= 4, "a color can have at most 4 channels");
- ColorChannel data[N];
- public:
- Color() = default;
- template<typename... Args>
- Color(ColorChannel a, Args&&... args) {
- const int size = sizeof...(args) + 1;
- int init[size] = {a, args...};
- static_assert(N == size, "color size and amount of channel arguments do not match");
- for(int i = 0; i < N; i++) {
- data[i] = init[i];
- }
- }
- 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];
- }
- };
- typedef Color<4> Color4;
- typedef Color<3> Color3;
- typedef Color<2> Color2;
- typedef Color<1> Color1;
- #endif
|