License Public Domain
Lines 116
Keywords
class (8) color (8) OpenGL (15)
Included in this Library
Permissions
Viewable by Everyone
Editable by All Siafoo Users

color.h Atom Feed 0

In Brief Color class that stores colors conveniently for OpenGL applications.
# 's
  1/**************************************************************************
2 * Copyright (C) 2006 Michael Sutherland
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 ****************************************************************************/
20
21#ifndef COLOR_H
22#define COLOR_H
23
24#include <iostream>
25
26namespace hud {
27
28 enum Colors {
29 R = 0,
30 G,
31 B,
32 A
33 };
34
35 class Color
36 {
37 private: /// variables
38 double m_color[4];
39
40 public: /// constructors/destructors
41 Color() {
42 m_color[R] = 0;
43 m_color[G] = 0;
44 m_color[B] = 0;
45 m_color[A] = 1;
46 }
47
48 Color(const double color[4])
49 {
50 for (unsigned i = 0; i < 4; ++i)
51 {
52 m_color[i] = color[i];
53 }
54 }
55 Color(const Color& color)
56 {
57 m_color[R] = color[R];
58 m_color[G] = color[G];
59 m_color[B] = color[B];
60 m_color[A] = color[A];
61 }
62 Color(double red, double green, double blue, double alpha = 1)
63 {
64 m_color[R] = red;
65 m_color[G] = green;
66 m_color[B] = blue;
67 m_color[A] = alpha;
68 }
69 ~Color() {}
70
71 public: /// accessors
72 double red() const { return m_color[R]; }
73 double green() const { return m_color[G]; }
74 double blue() const { return m_color[B]; }
75 double alpha() const { return m_color[A]; }
76
77 public: /// setters
78 void setRed(double red) { m_color[R] = red; }
79 void setGreen(double green) { m_color[G] = green; }
80 void setBlue(double blue) { m_color[B] = blue; }
81 void setAlpha(double alpha) { m_color[A] = alpha; }
82
83 public: /// member functions
84 const double* rgba() const { return m_color; }
85 const double* v() const { return m_color; }
86 bool verifyRange() const;
87
88 public: /// overloaded operators
89 inline Color& operator=(const Color& color)
90 {
91 m_color[R] = color.m_color[R];
92 m_color[G] = color.m_color[G];
93 m_color[B] = color.m_color[B];
94 m_color[A] = color.m_color[A];
95 return *this;
96 }
97
98 inline double& operator[](const unsigned index) { return m_color[index]; }
99 inline double operator[](const unsigned index) const { return m_color[index]; }
100 inline double& operator()(const unsigned index) { return m_color[index]; }
101 inline double operator()(const unsigned index) const { return m_color[index]; }
102
103 }; // class Color
104
105
106 /////////////////////
107 // Color Functions //
108 /////////////////////
109
110 inline std::ostream& operator<< (std::ostream& os, const Color& color)
111 {
112 return os << "(r=" << color.red() << ", g=" << color.green() << ", b=" << color.blue()
113 << ", a=" << color.alpha() << ")";
114 }
115
116 inline bool operator==(const Color& lhs, const Color& rhs)
117 {
118 return (lhs.red() == rhs.red() && lhs.green() == rhs.green()
119 && lhs.blue() == rhs.blue() && lhs.alpha() == rhs.alpha());
120 }
121
122 inline bool operator!=(const Color& lhs, const Color& rhs)
123 {
124 return !(lhs == rhs);
125 }
126
127} // namespace hud
128
129#endif // COLOR_H

Color class that stores colors conveniently for OpenGL applications.