CMSC23700 Common Code Library
Support code for CS23700 programming projects
cs237-color.hxx
Go to the documentation of this file.
1 
8 /*
9  * COPYRIGHT (c) 2013 John Reppy (http://cs.uchicago.edu/~jhr)
10  * All rights reserved.
11  */
12 
13 #ifndef _CS237_COLOR_HXX_
14 #define _CS237_COLOR_HXX_
15 
16 #ifndef _CS237_HXX_
17 #error "c237-color.hxx should not be included directly"
18 #endif
19 
20 namespace cs237 {
21 
22  struct color3ub {
23  unsigned char r, g, b;
24 
25  color3ub () : r(0.0f), g(0.0f), b(0.0f) { }
26  color3ub (color3ub const &c) : r(c.r), g(c.g), b(c.b) { }
27  explicit color3ub (color3f const &c);
28  explicit color3ub (unsigned char grey) : r(grey), g(grey), b(grey) { }
29  color3ub (unsigned char rr, unsigned char gg, unsigned char bb) : r(rr), g(gg), b(bb) { }
30 
31  unsigned char & operator[] (unsigned int const &i);
32  unsigned char const & operator[] (unsigned int const &i) const;
33 
34  color3ub & operator= (color3f const &c);
35  };
36 
37  struct color4ub {
38  unsigned char r, g, b, a;
39 
40  color4ub () : r(0), g(0), b(0), a(255) { }
41  color4ub (color4ub const &c) : r(c.r), g(c.g), b(c.b), a(c.a) { }
42  explicit color4ub (color3ub const &c) : r(c.r), g(c.g), b(c.b), a(255) { }
43  explicit color4ub (color3ub const &c, unsigned char alpha) : r(c.r), g(c.g), b(c.b), a(alpha) { }
44  explicit color4ub (color4f const &c);
45  explicit color4ub (unsigned char grey) : r(grey), g(grey), b(grey), a(255) { }
46  explicit color4ub (unsigned char rr, unsigned char gg, unsigned char bb) : r(rr), g(gg), b(bb), a(255) { }
47  color4ub (unsigned char rr, unsigned char gg, unsigned char bb, unsigned char aa)
48  : r(rr), g(gg), b(bb), a(aa)
49  { }
50 
51  unsigned char & operator[] (unsigned int const &i);
52  unsigned char const & operator[] (unsigned int const &i) const;
53 
54  color4ub & operator= (color4f const &c);
55  };
56 
57  struct color3f {
58  float r, g, b;
59 
60  color3f () : r(0.0f), g(0.0f), b(0.0f) { }
61  color3f (color3f const &c) : r(c.r), g(c.g), b(c.b) { }
62  explicit color3f (color3ub const &c);
63  explicit color3f (float grey) : r(grey), g(grey), b(grey) { }
64  color3f (float rr, float gg, float bb) : r(rr), g(gg), b(bb) { }
65 
66  float & operator[] (unsigned int const &i);
67  float const & operator[] (unsigned int const &i) const;
68 
69  color3f & operator= (color3ub const &c);
70 
71  color3f & operator+= (float const &s);
72  color3f & operator+= (color3f const &c);
73 
74  color3f & operator-= (float const &s);
75  color3f & operator-= (color3f const &c);
76 
77  color3f & operator*= (float const &s);
78  color3f & operator*= (color3f const &c);
79 
80  color3f & clamp ();
81  };
82 
83  struct color4f {
84  float r, g, b, a;
85 
86  color4f () : r(0.0f), g(0.0f), b(0.0f), a(1.0f) { }
87  explicit color4f (color3f const &c) : r(c.r), g(c.g), b(c.b), a(1.0f) { }
88  explicit color4f (color3f const &c, float alpha) : r(c.r), g(c.g), b(c.b), a(alpha) { }
89  color4f (color4f const &c) : r(c.r), g(c.g), b(c.b), a(c.a) { }
90  explicit color4f (color4ub const &c);
91  explicit color4f (float grey) : r(grey), g(grey), b(grey), a(1.0f) { }
92  explicit color4f (float rr, float gg, float bb) : r(rr), g(gg), b(bb), a(1.0f) { }
93  color4f (float rr, float gg, float bb, float aa) : r(rr), g(gg), b(bb), a(aa) { }
94 
95  float & operator[] (unsigned int const &i);
96  float const & operator[] (unsigned int const &i) const;
97 
98  color4f & operator= (color4ub const &c);
99 
100  color4f & operator+= (float const &s);
101  color4f & operator+= (color4f const &c);
102 
103  color4f & operator-= (float const &s);
104  color4f & operator-= (color4f const &c);
105 
106  color4f & operator*= (float const &s);
107  color4f & operator*= (color4f const &c);
108 
109  color4f & clamp ();
110  };
111 
115  std::ostream& operator<< (std::ostream& s, color3ub const &c);
116 
120  std::ostream& operator<< (std::ostream& s, color4ub const &c);
121 
125  std::ostream& operator<< (std::ostream& s, color3f const &c);
126 
130  std::ostream& operator<< (std::ostream& s, color4f const &c);
131 
132 } /* namespace cs237 */
133 
134 #endif /* !_CS237_COLOR_HXX_ */
float & operator[](unsigned int const &i)
Definition: cs237-color.inl:108
float a
Definition: cs237-color.hxx:84
color4f(float rr, float gg, float bb, float aa)
Definition: cs237-color.hxx:93
color4f(float rr, float gg, float bb)
Definition: cs237-color.hxx:92
color4f & operator+=(float const &s)
Definition: cs237-color.inl:209
color3ub & operator=(color3f const &c)
Definition: cs237-color.inl:67
unsigned char g
Definition: cs237-color.hxx:23
float r
Definition: cs237-color.hxx:58
color3f & operator+=(float const &s)
Definition: cs237-color.inl:128
color4ub(color3ub const &c)
Definition: cs237-color.hxx:42
color4f & operator-=(float const &s)
Definition: cs237-color.inl:226
color4ub(unsigned char grey)
Definition: cs237-color.hxx:45
color4ub()
Definition: cs237-color.hxx:40
unsigned char & operator[](unsigned int const &i)
Definition: cs237-color.inl:56
color4f(color3f const &c, float alpha)
Definition: cs237-color.hxx:88
float g
Definition: cs237-color.hxx:84
color4f()
Definition: cs237-color.hxx:86
color4ub(color4ub const &c)
Definition: cs237-color.hxx:41
color3f & operator=(color3ub const &c)
convert a color3ub value to a color3f l-value
Definition: cs237-color.inl:120
unsigned char & operator[](unsigned int const &i)
Definition: cs237-color.inl:81
float b
Definition: cs237-color.hxx:84
Definition: cs237-color.hxx:83
color3ub(unsigned char rr, unsigned char gg, unsigned char bb)
Definition: cs237-color.hxx:29
color3f & operator*=(float const &s)
Definition: cs237-color.inl:158
color4f(float grey)
Definition: cs237-color.hxx:91
Definition: cs237-color.hxx:22
Definition: cs237-color.hxx:37
color4ub & operator=(color4f const &c)
Definition: cs237-color.inl:92
unsigned char b
Definition: cs237-color.hxx:38
float r
Definition: cs237-color.hxx:84
color4ub(unsigned char rr, unsigned char gg, unsigned char bb, unsigned char aa)
Definition: cs237-color.hxx:47
float b
Definition: cs237-color.hxx:58
unsigned char g
Definition: cs237-color.hxx:38
unsigned char r
Definition: cs237-color.hxx:38
color3f(float grey)
Definition: cs237-color.hxx:63
color3f(float rr, float gg, float bb)
Definition: cs237-color.hxx:64
color3f & clamp()
Definition: cs237-color.inl:173
color4f & operator=(color4ub const &c)
convert a color4ub value to a color4f l-value
Definition: cs237-color.inl:200
color3f(color3f const &c)
Definition: cs237-color.hxx:61
color3ub()
Definition: cs237-color.hxx:25
float g
Definition: cs237-color.hxx:58
Definition: cs237-aabb.hxx:18
color3f & operator-=(float const &s)
Definition: cs237-color.inl:143
unsigned char r
Definition: cs237-color.hxx:23
color3ub(color3ub const &c)
Definition: cs237-color.hxx:26
color4ub(color3ub const &c, unsigned char alpha)
Definition: cs237-color.hxx:43
float & operator[](unsigned int const &i)
Definition: cs237-color.inl:188
color4f(color3f const &c)
Definition: cs237-color.hxx:87
color4f(color4f const &c)
Definition: cs237-color.hxx:89
Definition: cs237-color.hxx:57
unsigned char a
Definition: cs237-color.hxx:38
color4f & clamp()
Definition: cs237-color.inl:260
color3ub(unsigned char grey)
Definition: cs237-color.hxx:28
color4f & operator*=(float const &s)
Definition: cs237-color.inl:243
color3f()
Definition: cs237-color.hxx:60
unsigned char b
Definition: cs237-color.hxx:23
color4ub(unsigned char rr, unsigned char gg, unsigned char bb)
Definition: cs237-color.hxx:46
std::ostream & operator<<(std::ostream &s, color3ub const &c)
output the color to a stream