Size: 1418 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// cs/renderer/pixel.hh
#ifndef CS_RENDERER_PIXEL_HH
#define CS_RENDERER_PIXEL_HH

#include <stdint.h>

#include <iostream>
#include <tuple>

#include "cs/log.hh"

namespace cs::renderer {

struct Pixel {
  uint8_t r, g, b, a;
  Pixel() : Pixel(0, 0, 0, 0) {}
  Pixel(uint8_t red, uint8_t green, uint8_t blue,
        uint8_t alpha)
      : r(red), g(green), b(blue), a(alpha) {}

  bool operator==(const Pixel& other) const {
    return r == other.r && g == other.g && b == other.b &&
           a == other.a;
  }

  std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>
  as_tuple() {
    return std::make_tuple(r, g, b, a);
  }

  friend std::ostream& operator<<(std::ostream& os,
                                  const Pixel& px) {
    return os << "Pixel(" << static_cast<unsigned int>(px.r)
              << ", " << static_cast<unsigned int>(px.g)
              << ", " << static_cast<unsigned int>(px.b)
              << ", " << static_cast<unsigned int>(px.a)
              << ")";
  }

  int operator[](size_t i) {
    if (i == 0) {
      return r;
    } else if (i == 1) {
      return g;
    } else if (i == 2) {
      return b;
    } else if (i == 3) {
      return a;
    }

#define INVARIANT(msg)       \
  LOG(ERROR) << msg << ENDL; \
  exit(1);

    INVARIANT("Unknown index" + std::to_string(i) +
              " for Pixel operator[]");
  }
};

}  // namespace cs::renderer

#endif  // CS_RENDERER_PIXEL_HH
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.