Color.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef CORE_COLOR_HPP
  2. #define CORE_COLOR_HPP
  3. #include "core/utils/Types.hpp"
  4. namespace Core {
  5. using ColorChannel = unsigned char;
  6. template<size_t N>
  7. class Color final {
  8. static_assert(N >= 1 && N <= 4, "a color has 1 to 4 channels");
  9. ColorChannel data[N] = {};
  10. public:
  11. Color() = default;
  12. template<typename OT, typename... Args>
  13. Color(OT a, Args&&... args)
  14. : data(static_cast<ColorChannel>(a),
  15. static_cast<ColorChannel>(args)...) {
  16. static_assert(sizeof...(args) + 1 == N,
  17. "color channel parameters do not match its size");
  18. }
  19. float asFloat(size_t index) const {
  20. return data[index] * (1.0f / 255.0f);
  21. }
  22. ColorChannel& operator[](size_t index) {
  23. return data[index];
  24. }
  25. const ColorChannel& operator[](size_t index) const {
  26. return data[index];
  27. }
  28. };
  29. using Color4 = Color<4>;
  30. using Color3 = Color<3>;
  31. using Color2 = Color<2>;
  32. using Color1 = Color<1>;
  33. }
  34. #endif