Color.h 721 B

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