Color.h 757 B

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