VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grcolour.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file grcolour.h
19 ///
20 
21 #ifndef GR_COLOUR__H
22 #define GR_COLOUR__H
23 
24 #include "gdef.h"
25 #include <utility> // std::swap
26 #include <iostream>
27 
28 namespace Gr
29 {
30  class Colour ;
31 }
32 
33 /// \class Gr::Colour
34 /// A simple rgb colour structure.
35 ///
37 {
38 public:
39  typedef unsigned char value_t ;
40  typedef g_uint32_t cc_t ;
41 
42  Colour() ;
43  ///< Default constructor for black.
44 
45  Colour( value_t r , value_t g , value_t b ) ;
46  ///< Constructor taking red, green and blue values.
47 
48  value_t r() const ;
49  ///< Returns the red value.
50 
51  value_t g() const ;
52  ///< Returns the green value.
53 
54  value_t b() const ;
55  ///< Returns the blue value.
56 
57  cc_t cc() const ;
58  ///< Returns a combined-component value that incorporates
59  ///< the r(), g() and b() values. This is what Xlib uses
60  ///< for a pixel value.
61 
62  static Colour from( cc_t cc ) ;
63  ///< Creates a colour from a combined-component value.
64 
65 public:
66  value_t m_r ;
67  value_t m_g ;
68  value_t m_b ;
69 } ;
70 
71 inline
73  m_r(0U) ,
74  m_g(0U) ,
75  m_b(0U)
76 {
77 }
78 
79 inline
80 Gr::Colour::Colour( value_t r_ , value_t g_ , value_t b_ ) :
81  m_r(r_) ,
82  m_g(g_) ,
83  m_b(b_)
84 {
85 }
86 
87 inline
88 Gr::Colour::value_t Gr::Colour::r() const
89 {
90  return m_r ;
91 }
92 
93 inline
94 Gr::Colour::value_t Gr::Colour::g() const
95 {
96  return m_g ;
97 }
98 
99 inline
100 Gr::Colour::value_t Gr::Colour::b() const
101 {
102  return m_b ;
103 }
104 
105 inline
106 Gr::Colour::cc_t Gr::Colour::cc() const
107 {
108  const cc_t r = m_r ;
109  const cc_t g = m_g ;
110  const cc_t b = m_b ;
111  return (r<<16) | (g<<8) | (b<<0) ;
112 }
113 
114 inline
116 {
117  return Colour( (cc>>16) & 255U , (cc>>8) & 255U , (cc>>0) & 255U ) ;
118 }
119 
120 namespace Gr
121 {
122  inline bool operator==( const Colour & p , const Colour & q )
123  {
124  return p.m_r == q.m_r && p.m_g == q.m_g && p.m_b == q.m_b ;
125  }
126  inline bool operator!=( const Colour & p , const Colour & q )
127  {
128  return !(p==q) ;
129  }
130  inline void swap( Colour & p , Colour & q )
131  {
132  std::swap( p.m_r , q.m_r ) ;
133  std::swap( p.m_g , q.m_g ) ;
134  std::swap( p.m_b , q.m_b ) ;
135  }
136  inline std::ostream & operator<<( std::ostream & stream , const Colour & colour )
137  {
138  stream
139  << "{Colour=["
140  << static_cast<unsigned int>(colour.r()) << ","
141  << static_cast<unsigned int>(colour.g()) << ","
142  << static_cast<unsigned int>(colour.b()) << "]}" ;
143  return stream ;
144  }
145 }
146 
147 #endif
value_t r() const
Returns the red value.
Definition: grcolour.h:88
A simple rgb colour structure.
Definition: grcolour.h:36
static Colour from(cc_t cc)
Creates a colour from a combined-component value.
Definition: grcolour.h:115
value_t g() const
Returns the green value.
Definition: grcolour.h:94
value_t b() const
Returns the blue value.
Definition: grcolour.h:100
Colour()
Default constructor for black.
Definition: grcolour.h:72
cc_t cc() const
Returns a combined-component value that incorporates the r(), g() and b() values. ...
Definition: grcolour.h:106