-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPixel.h
More file actions
83 lines (66 loc) · 2.07 KB
/
Pixel.h
File metadata and controls
83 lines (66 loc) · 2.07 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef PIXEL_H
#define PIXEL_H
namespace P3D
{
template <const unsigned int RBits = 8, const unsigned int GBits = 8, const unsigned int BBits = 8, class TStorageType = unsigned int> class Pixel
{
public:
constexpr Pixel(const Pixel& r) : p(r.p) {}
constexpr Pixel(const TStorageType v) : p(v) {}
constexpr operator TStorageType() const {return p;}
constexpr Pixel(const unsigned int r, const unsigned int g, const unsigned int b)
{
Set(r, g, b);
}
constexpr unsigned int R() const
{
return (p & rmask()) >> rshift();
}
constexpr unsigned int G() const
{
return (p & gmask()) >> gshift();
}
constexpr unsigned int B() const
{
return (p & bmask()) >> bshift();
}
constexpr void Set(unsigned int r, unsigned int g, unsigned int b)
{
p = (
((r << rshift()) & rmask()) |
((g << gshift()) & gmask()) |
((b << bshift()) & bmask())
);
}
private:
constexpr unsigned int rmask() const
{
return ((1 << RBits)-1) << rshift();
}
constexpr unsigned int gmask() const
{
return ((1 << GBits)-1) << gshift();
}
constexpr unsigned int bmask() const
{
return ((1 << BBits)-1) << bshift();
}
constexpr unsigned int rshift() const
{
return 0;
}
constexpr unsigned int gshift() const
{
return RBits;
}
constexpr unsigned int bshift() const
{
return (GBits + RBits);
}
TStorageType p;
};
typedef Pixel<8,8,8,unsigned int> RGB888; //24bit color
typedef Pixel<5,6,5,unsigned short> RGB565; //16bit color
typedef Pixel<5,5,5,unsigned short> RGB555; //15bit color
}
#endif // PIXEL_H