Color.h 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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(
  15. N == size,
  16. "color size and amount of channel arguments do not match");
  17. for(int i = 0; i < N; i++) {
  18. data[i] = init[i];
  19. }
  20. }
  21. float asFloat(int index) const {
  22. return data[index] * (1.0f / 255.0f);
  23. }
  24. ColorChannel& operator[](int index) {
  25. return data[index];
  26. }
  27. const ColorChannel& operator[](int index) const {
  28. return data[index];
  29. }
  30. };
  31. typedef Color<4> Color4;
  32. typedef Color<3> Color3;
  33. typedef Color<2> Color2;
  34. typedef Color<1> Color1;
  35. #endif