Color.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef DCOLOR_H_INCLUDED
34 #define DCOLOR_H_INCLUDED
35 
36 #include <cstdint>
37 #include <string>
38 #include <vector>
39 #include <unordered_map>
40 
41 
42 
43 namespace Dynacoe {
44 
47 class Color {
48  public:
49 
50 
54  class Component {
55  public:
56  Component(){}
57  Component(const double & f) {data = f*255;}
58  Component(const float & f) {data = f*255;}
59  Component(const uint8_t & f) {data = f;}
60  Component(const int & f) {data = f;}
61  Component(const unsigned int & f) {data = f;}
62 
63  Component & operator+=(const Component & other) {data += other.data; return *this;}
64  Component & operator-=(const Component & other) {data -= other.data; return *this;}
65  Component & operator*=(const Component & other) {*this = (float)*this * (float)other; return *this;}
66  Component & operator/=(const Component & other) {*this = (float)*this / (float)other; return *this;}
67 
68 
69  operator float() const {
70  return data/255.f;
71  }
72 
75  uint8_t Byte() const {
76  return data;
77  }
78 
79  bool operator==(const Component & other) const {return data == other.data;}
80  bool operator!=(const Component & other) const {return data != other.data;}
81 
82  private:
83  uint8_t data;
84  };
85 
86 
87  Color();
88  Color(uint32_t);
89  Color(int red, int green, int blue, int alpha = 255);
90  Color(float redf, float greenf, float bluef, float alphaf = 1.f);
91  Color(const std::vector<Component> & c);
92 
105  Color(const std::string & name );
106  Color(const char * sname);
107  Color(const Color &);
108 
113  uint32_t Int() const;
114 
115 
116  Color & operator= (const Color &);
117 
120  Color & Define(int red, int green, int blue, int alpha);
121 
124  Color & Define(uint32_t c);
125 
126  Color operator+ (const Color &) const;
127  Color operator- (const Color &) const;
128  Color operator* (const Color &) const;
129  Color operator/ (float) const;
130  Color operator* (float) const;
131  Color & operator+=(const Color &);
132  Color & operator-=(const Color &);
133  bool operator==(const Color &) const;
134  bool operator!=(const Color &) const;
135 
136 
149 
150 
154  static void Define(const std::string &, const Color &);
155 
156 
157  bool operator<(const Color & b) const {
158  return Int() < b.Int();
159  }
160 
163  std::string ToString() const;
164 
165 };
166 
167 };
168 
169 #endif
A standard object representing an RGBA color.
Definition: Color.h:47
Component g
The green amount.
Definition: Color.h:142
std::string ToString() const
Returns the string form of the Color.
Component b
The green amount.
Definition: Color.h:145
Definition: AssetID.h:37
Component a
The blue amount.
Definition: Color.h:148
A component of Color. Is from 0.f to 1.f but is stored as a byte. When used, Component s are implicit...
Definition: Color.h:54
Color & Define(int red, int green, int blue, int alpha)
Sets the color.
Component r
The red amount.
Definition: Color.h:139
uint32_t Int() const
Returns an unsigned, 32-bit integer where each byte refers to a color component in RGBA form...
uint8_t Byte() const
Returns the byte-value form of the component.
Definition: Color.h:75
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66