remove \r
[extl.git] / extl / graphic / basic_color.h
blob086c26ded033f62c6d36b341d7e13e5a4386954e
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: basic_color.h
4 * Created: 08.12.31
5 * Updated: 08.12.31
7 * Brief: The basic_color class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_GRAPHIC_BASIC_COLOR_H
14 #define EXTL_GRAPHIC_BASIC_COLOR_H
16 /*!\file basic_color.h
17 * \brief basic_color class
20 /* ///////////////////////////////////////////////////////////////////////
21 * Includes
23 #include "prefix.h"
25 /* ///////////////////////////////////////////////////////////////////////
26 * Macros
28 // make rgba
29 #define EXTL_RGBA(r, g, b, a) ((r << 24) | (g << 16) | (b << 8) | (a))
30 #define EXTL_RGB(r, g, b) (EXTL_RGBA(r, g, b, 0))
32 // get rgba components
33 #define EXTL_RGBA_GETR(rgba) ((rgba >> 24) & 0x000000FF)
34 #define EXTL_RGBA_GETG(rgba) ((rgba >> 16) & 0x000000FF)
35 #define EXTL_RGBA_GETB(rgba) ((rgba >> 8) & 0x000000FF)
36 #define EXTL_RGBA_GETA(rgba) ((rgba) & 0x000000FF)
38 // set rgba components
39 #define EXTL_RGBA_SETR(rgba, r) do{ rgba &= 0x00FFFFFF; rgba |= ((r << 24) & 0xFF000000); }while (0)
40 #define EXTL_RGBA_SETG(rgba, g) do{ rgba &= 0xFF00FFFF; rgba |= ((g << 16) & 0x00FF0000); }while (0)
41 #define EXTL_RGBA_SETB(rgba, b) do{ rgba &= 0xFFFF00FF; rgba |= ((b << 8) & 0x0000FF00); }while (0)
42 #define EXTL_RGBA_SETA(rgba, a) do{ rgba &= 0xFFFFFF00; rgba |= ((a) & 0x000000FF); }while (0)
44 // rgba constants
45 #define EXTL_RGBA_BLACK EXTL_RGB(0, 0, 0)
46 #define EXTL_RGBA_WHITE EXTL_RGB(255, 255, 255)
47 #define EXTL_RGBA_RED EXTL_RGB(255, 0, 0)
48 #define EXTL_RGBA_GREEN EXTL_RGB(0, 255, 0)
49 #define EXTL_RGBA_BLUE EXTL_RGB(0, 0, 255)
50 #define EXTL_RGBA_YELLOW EXTL_RGB(255, 255, 0)
51 /* ///////////////////////////////////////////////////////////////////////
52 * ::extl::graphic namespace
54 EXTL_GRAPHIC_BEGIN_WHOLE_NAMESPACE
56 /*!brief basic_color
58 * \ingroup extl_group_graphic
60 class basic_color
62 /// \name Types
63 /// @{
64 public:
65 typedef basic_color class_type;
66 typedef e_uint32_t rgba_type;
67 typedef e_int_t int_type;
68 typedef int_type value_type;
69 typedef value_type& reference;
70 typedef value_type const& const_reference;
71 typedef e_bool_t bool_type;
72 /// @}
75 /// \name Members
76 /// @{
77 private:
78 /// 32bit: r g b a
79 rgba_type m_rgba;
80 /// @}
82 /// \name Constructors
83 /// @{
84 public:
85 explicit_k basic_color(rgba_type const& c = rgba_type())
86 : m_rgba(c)
89 basic_color ( const_reference r
90 , const_reference g
91 , const_reference b
92 , const_reference a = value_type()
94 : m_rgba(EXTL_RGBA(r, g, b, a))
97 basic_color(class_type const& rhs)
98 : m_rgba(rhs.rgba())
101 /// @}
103 /// \name Attributes
104 /// @{
105 public:
106 value_type const red() const { return EXTL_RGBA_GETR(rgba()); }
107 void red(const_reference val) { EXTL_RGBA_SETR(m_rgba, val); }
109 value_type const green() const { return EXTL_RGBA_GETG(rgba()); }
110 void green(const_reference val) { EXTL_RGBA_SETG(m_rgba, val); }
112 value_type const blue() const { return EXTL_RGBA_GETB(rgba()); }
113 void blue(const_reference val) { EXTL_RGBA_SETB(m_rgba, val); }
115 value_type const alpha() const { return EXTL_RGBA_GETA(rgba()); }
116 void alpha(const_reference val) { EXTL_RGBA_SETA(m_rgba, val); }
118 value_type const gray() const { return ((red() + green() + blue()) / 3); }
119 void gray(const_reference val) { red(val); green(val); blue(val); }
121 bool_type const is_black() const { return (m_rgba == EXTL_RGBA_BLACK); }
122 void is_black(bool_type b) { b? rgba(EXTL_RGBA_BLACK) : rgba(EXTL_RGBA_WHITE); }
124 rgba_type const rgba() const { return m_rgba; }
125 void rgba(rgba_type const& val) { m_rgba = val; }
126 /// @}
128 /// \name Mutators
129 /// @{
130 public:
131 void swap(class_type& rhs) { std_swap(m_rgba, rhs.m_rgba); }
132 void clear() { rgba(rgba_type()); }
133 /// @}
135 /// \name Operators
136 /// @{
137 public:
138 bool_type operator ==(class_type const& rhs) const { return (rgba() == rhs.rgba()); }
139 bool_type operator ==(rgba_type const& rhs) const { return (rgba() == rhs); }
141 bool_type operator !=(class_type const& rhs) const { return !((*this) == rhs); }
142 bool_type operator !=(rgba_type const& rhs) const { return (rgba() != rhs); }
143 /// @}
147 /* ///////////////////////////////////////////////////////////////////////
148 * ::extl::graphic namespace
150 EXTL_GRAPHIC_END_WHOLE_NAMESPACE
152 /* //////////////////////////////////////////////////////////////////// */
153 #endif /* EXTL_GRAPHIC_BASIC_COLOR_H */
154 /* //////////////////////////////////////////////////////////////////// */