mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
color.h
Go to the documentation of this file.
1 #ifndef MF_COLOR_H_
2 #define MF_COLOR_H_
3 
4 #include <cstdint>
5 #include "elem.h"
6 
7 namespace mf {
8 
10 struct alignas(4) mono_color {
11  mono_color() = default;
12  mono_color(std::uint8_t nint) : intensity(nint) { }
13 
14  mono_color(const mono_color&) = default;
15  mono_color& operator=(const mono_color&) = default;
16 
17  std::uint8_t intensity;
18 
19  const static mono_color black;
20  const static mono_color white;
21 };
22 
23 bool operator==(const mono_color& a, const mono_color& b);
24 bool operator!=(const mono_color& a, const mono_color& b);
25 
27 struct rgb_color {
28  rgb_color() = default;
29  rgb_color(std::uint8_t nr, std::uint8_t ng, std::uint8_t nb) :
30  r(nr), g(ng), b(nb) { }
31 
32  rgb_color(const rgb_color&) = default;
33  rgb_color& operator=(const rgb_color&) = default;
34 
35 
36  std::uint8_t r; // red
37  std::uint8_t g; // green
38  std::uint8_t b; // blue
39 
40  const static rgb_color black;
41  const static rgb_color white;
42 };
43 
44 bool operator==(const rgb_color& a, const rgb_color& b);
45 bool operator!=(const rgb_color& a, const rgb_color& b);
46 
47 
49 struct alignas(4) rgba_color {
50  rgba_color() = default;
51  rgba_color(std::uint8_t nr, std::uint8_t ng, std::uint8_t nb, std::uint8_t na = 255) :
52  r(nr), g(ng), b(nb), a(na) { }
53 
54  rgba_color(const rgba_color&) = default;
55  rgba_color& operator=(const rgba_color&) = default;
56 
57  std::uint8_t r; // red
58  std::uint8_t g; // green
59  std::uint8_t b; // blue
60  std::uint8_t a; // alpha
61 
62  static rgba_color null() noexcept { return rgba_color(0, 0, 0, 0); }
63  bool is_null() const noexcept { return (a == 0); }
64 
65  const static rgba_color black;
66  const static rgba_color white;
67 };
68 
69 bool operator==(const rgba_color& a, const rgba_color& b);
70 bool operator!=(const rgba_color& a, const rgba_color& b);
71 
72 
74 struct alignas(4) ycbcr_color {
75  ycbcr_color() = default;
76  ycbcr_color(std::uint8_t ny, std::uint8_t ncr, std::uint8_t ncb) :
77  y(ny), cr(ncr), cb(ncb) { }
78 
79  ycbcr_color(const ycbcr_color&) = default;
80  ycbcr_color& operator=(const ycbcr_color&) = default;
81 
82  std::uint8_t y; // luma, Y'
83  std::uint8_t cr; // chroma-red, U
84  std::uint8_t cb; // chroma-blue, V
85 };
86 
87 bool operator==(const ycbcr_color& a, const ycbcr_color& b);
88 bool operator!=(const ycbcr_color& a, const ycbcr_color& b);
89 
90 
91 
93 template<typename Output, typename Input>
94 Output color_convert(const Input&);
95 
96 template<> rgb_color color_convert(const ycbcr_color&);
97 
98 template<> inline rgba_color color_convert(const rgb_color& in) {
99  return rgba_color(in.r, in.g, in.b, 255);
100 }
101 
102 template<> inline rgb_color color_convert(const mono_color& in) {
103  return rgb_color(in.intensity, in.intensity, in.intensity);
104 }
105 
106 template<> inline mono_color color_convert(const ycbcr_color& in) {
107  return mono_color(in.y);
108 }
109 
110 template<> inline rgba_color color_convert(const ycbcr_color& in) {
111  return color_convert<rgba_color>(color_convert<rgb_color>(in));
112 }
113 
114 template<> inline rgb_color color_convert(const rgba_color& in) {
115  return rgb_color(in.r, in.g, in.b);
116 }
117 
118 template<>
120  elem_traits_base<mono_color, std::uint8_t, 1, false> { };
121 
122 template<>
124  elem_traits_base<rgb_color, std::uint8_t, 3, false> { };
125 
126 template<>
128  elem_traits_base<rgba_color, std::uint8_t, 4, true> { };
129 
130 template<>
132  elem_traits_base<ycbcr_color, std::uint8_t, 3, false> { };
133 
134 
135 
136 }
137 
138 #endif
std::uint8_t g
Definition: color.h:37
ycbcr_color(std::uint8_t ny, std::uint8_t ncr, std::uint8_t ncb)
Definition: color.h:76
static const rgb_color white
Definition: color.h:41
rgb_color(std::uint8_t nr, std::uint8_t ng, std::uint8_t nb)
Definition: color.h:29
std::uint8_t a
Definition: color.h:60
ycbcr_color & operator=(const ycbcr_color &)=default
YCbCr color, 8 bit.
Definition: color.h:74
std::uint8_t r
Definition: color.h:36
ycbcr_color()=default
static rgba_color null() noexcept
Definition: color.h:62
bool operator!=(const mono_color &a, const mono_color &b)
Definition: color.cc:37
mono_color(std::uint8_t nint)
Definition: color.h:12
std::uint8_t b
Definition: color.h:38
static const mono_color black
Definition: color.h:19
static const rgba_color white
Definition: color.h:66
rgb_color & operator=(const rgb_color &)=default
mono_color()=default
Monochrome color, 8 bit.
Definition: color.h:10
Elem traits base class with the required members.
Definition: elem.h:14
static const rgb_color black
Definition: color.h:40
std::uint8_t cr
Definition: color.h:83
rgb_color()=default
rgba_color & operator=(const rgba_color &)=default
RGBA color, 8 bit.
Definition: color.h:49
rgba_color()=default
std::uint8_t r
Definition: color.h:57
std::uint8_t b
Definition: color.h:59
std::uint8_t intensity
Definition: color.h:17
rgb_color color_convert(const ycbcr_color &in)
Definition: color.cc:19
mono_color & operator=(const mono_color &)=default
static const rgba_color black
Definition: color.h:65
Default elem traits, using Elem as standard layout scalar type.
Definition: elem.h:30
static const mono_color white
Definition: color.h:20
std::uint8_t y
Definition: color.h:82
RGB color, 8 bit.
Definition: color.h:27
std::uint8_t g
Definition: color.h:58
bool operator==(const mono_color &a, const mono_color &b)
Definition: color.cc:33
rgba_color(std::uint8_t nr, std::uint8_t ng, std::uint8_t nb, std::uint8_t na=255)
Definition: color.h:51
std::uint8_t cb
Definition: color.h:84
bool is_null() const noexcept
Definition: color.h:63