Color.h 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef COLOR_H
  2. #define COLOR_H
  3. typedef unsigned char ColorChannel;
  4. template<int N>
  5. class Color {
  6. static_assert(N <= 4, "a color can have at most 4 channels");
  7. ColorChannel data[N];
  8. public:
  9. Color() = default;
  10. template<typename... Args>
  11. Color(ColorChannel a, Args&&... args) {
  12. const int size = sizeof...(args) + 1;
  13. int init[size] = {a, args...};
  14. static_assert(N == size, "color size and amount of channel arguments do not match");
  15. for(int i = 0; i < N; i++) {
  16. data[i] = init[i];
  17. }
  18. }
  19. float asFloat(int index) const {
  20. return data[index] * (1.0f / 255.0f);
  21. }
  22. ColorChannel& operator[](int index) {
  23. return data[index];
  24. }
  25. const ColorChannel& operator[](int index) const {
  26. return data[index];
  27. }
  28. };
  29. typedef Color<4> Color4;
  30. typedef Color<3> Color3;
  31. typedef Color<2> Color2;
  32. typedef Color<1> Color1;
  33. #endif