Color.cppm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module;
  2. export module Core.Color;
  3. export import Core.Types;
  4. export namespace Core {
  5. using ColorChannel = u8;
  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(
  15. static_cast<ColorChannel>(a),
  16. static_cast<ColorChannel>(args)...) {
  17. static_assert(
  18. sizeof...(args) + 1 == N,
  19. "color channel parameters do not match its size");
  20. }
  21. float asFloat(size_t index) const {
  22. return data[index] * (1.0f / 255.0f);
  23. }
  24. ColorChannel& operator[](size_t index) {
  25. return data[index];
  26. }
  27. const ColorChannel& operator[](size_t index) const {
  28. return data[index];
  29. }
  30. };
  31. using Color4 = Color<4>;
  32. using Color3 = Color<3>;
  33. using Color2 = Color<2>;
  34. using Color1 = Color<1>;
  35. }