Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Color.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_COLOR_H
22 #define GF_COLOR_H
23 
24 #include "Portability.h"
25 #include "Vector.h"
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
32  /**
33  * @ingroup graphics
34  * @brief Predefined colors
35  *
36  * This class does not contain anything but predefined colors. All the
37  * primary, secondary and [tertiary colors](https://en.wikipedia.org/wiki/Tertiary_color)
38  * are defined by their usual names.
39  *
40  * If you want to define a color, you should use gf::Color4f.
41  *
42  * @sa gf::Color4f
43  */
44  struct GF_API Color {
45  /**
46  * @brief Deleted constructor
47  */
48  Color() = delete;
49 
50  /**
51  * @brief Black predefined color
52  */
53  static constexpr Color4f Black{0.0f, 0.0f, 0.0f, 1.0f};
54 
55  /**
56  * @brief White predefined color
57  */
58  static constexpr Color4f White{1.0f, 1.0f, 1.0f, 1.0f};
59 
60  /**
61  * @brief Red predefined color
62  */
63  static constexpr Color4f Red{1.0f, 0.0f, 0.0f, 1.0f};
64 
65  /**
66  * @brief Green predefined color
67  */
68  static constexpr Color4f Green{0.0f, 1.0f, 0.0f, 1.0f};
69 
70  /**
71  * @brief Blue predefined color
72  */
73  static constexpr Color4f Blue{0.0f, 0.0f, 1.0f, 1.0f};
74 
75  /**
76  * @brief Cyan predefined color
77  */
78  static constexpr Color4f Cyan{0.0f, 1.0f, 1.0f, 1.0f};
79 
80  /**
81  * @brief Magenta predefined color
82  */
83  static constexpr Color4f Magenta{1.0f, 0.0f, 1.0f, 1.0f};
84 
85  /**
86  * @brief Yellow predefined color
87  */
88  static constexpr Color4f Yellow{1.0f, 1.0f, 0.0f, 1.0f};
89 
90  /**
91  * @brief Transparent (black) predefined color
92  */
93  static constexpr Color4f Transparent{0.0f, 0.0f, 0.0f, 0.0f};
94 
95  /**
96  * @brief Opaque predefined color
97  *
98  * @param value The opacity value
99  */
100  static constexpr Color4f Opaque(float value = 0.5f) {
101  return { 1.0f, 1.0f, 1.0f, value };
102  }
103 
104  /**
105  * @brief Gray predefined color
106  *
107  * @param value The gray value
108  */
109  static constexpr Color4f Gray(float value = 0.5f) {
110  return { value, value, value, 1.0f };
111  }
112 
113  /**
114  * @brief Orange predefined color
115  */
116  static constexpr Color4f Orange{1.0f, 0.5f, 0.0f, 1.0f};
117 
118  /**
119  * @brief Rose predefined color
120  */
121  static constexpr Color4f Rose{1.0f, 0.0f, 0.5f, 1.0f};
122 
123  /**
124  * @brief Chartreuse predefined color
125  */
126  static constexpr Color4f Chartreuse{0.5f, 1.0f, 0.0f, 1.0f};
127 
128  /**
129  * @brief Spring (green) predefined color
130  */
131  static constexpr Color4f Spring{0.0f, 1.0f, 0.5f, 1.0f};
132 
133  /**
134  * @brief Violet predefined color
135  */
136  static constexpr Color4f Violet{0.5f, 0.0f, 1.0f, 1.0f};
137 
138  /**
139  * @brief Azure predefined color
140  */
141  static constexpr Color4f Azure{0.0f, 0.5f, 1.0f, 1.0f};
142 
143 
144  /**
145  * @brief Compute a lighter color
146  *
147  * This function takes a color and gives a lighter color based on a
148  * percentage. If this percentage is 0, the same color is returned. If this
149  * percentage is 1, the white color is returned.
150  *
151  * @param color The color
152  * @param percent The percentage, must be in @f$ [0, 1] @f$
153  */
154  static Color4f lighter(Color4f color, float percent = 0.5f);
155 
156  /**
157  * @brief Compute a darker color
158  *
159  * This function takes a color and gives a darker color based on a
160  * percentage. If this percentage is 0, the same color is returned. If this
161  * percentage is 1, the black color is returned.
162  *
163  * @param color The color
164  * @param percent The percentage, must be in @f$ [0, 1] @f$
165  */
166  static Color4f darker(Color4f color, float percent = 0.5f);
167 
168  /**
169  * @brief Get a color from 4 8-bit channels
170  *
171  * @param r The red channel
172  * @param g The green channel
173  * @param b The blue channel
174  * @param a The alpha channel
175  * @returns The corresponding color
176  */
177  static Color4f fromRgba32(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
178 
179  /**
180  * @brief Get a color from 32-bit value
181  *
182  * @param color The 32-bit color
183  * @returns The corresponding color
184  */
185  static Color4f fromRgba32(uint32_t color);
186 
187  /**
188  * @brief Get a color from a 32-bit color
189  *
190  * @param color A 32-bit color
191  * @return The corresponding color
192  */
193  static Color4f fromRgba32(Color4u color);
194 
195  /**
196  * @brief Convert a color to a 32-bit color
197  *
198  * @param color A color
199  * @return The corresponding 32-bit color
200  */
201  static Color4u toRgba32(Color4f color);
202 
203  };
204 
205 #ifndef DOXYGEN_SHOULD_SKIP_THIS
206 }
207 #endif
208 }
209 
210 #endif // GF_COLOR_H
static Color4f fromRgba32(Color4u color)
Get a color from a 32-bit color.
static constexpr Color4f Azure
Azure predefined color.
Definition: Color.h:141
Color()=delete
Deleted constructor.
static constexpr Color4f Transparent
Transparent (black) predefined color.
Definition: Color.h:93
static Color4f fromRgba32(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Get a color from 4 8-bit channels.
static constexpr Color4f Magenta
Magenta predefined color.
Definition: Color.h:83
static Color4f lighter(Color4f color, float percent=0.5f)
Compute a lighter color.
static constexpr Color4f Chartreuse
Chartreuse predefined color.
Definition: Color.h:126
static constexpr Color4f White
White predefined color.
Definition: Color.h:58
static constexpr Color4f Violet
Violet predefined color.
Definition: Color.h:136
static constexpr Color4f Cyan
Cyan predefined color.
Definition: Color.h:78
static Color4f fromRgba32(uint32_t color)
Get a color from 32-bit value.
static constexpr Color4f Red
Red predefined color.
Definition: Color.h:63
static constexpr Color4f Yellow
Yellow predefined color.
Definition: Color.h:88
static Color4u toRgba32(Color4f color)
Convert a color to a 32-bit color.
static constexpr Color4f Gray(float value=0.5f)
Gray predefined color.
Definition: Color.h:109
constexpr Vector(T x, T y, T z, T w)
Constructor that takes 4 components.
Definition: Vector.h:837
Definition: Action.h:34
Predefined colors.
Definition: Color.h:44
static constexpr Color4f Rose
Rose predefined color.
Definition: Color.h:121
static constexpr Color4f Green
Green predefined color.
Definition: Color.h:68
static constexpr Color4f Black
Black predefined color.
Definition: Color.h:53
static constexpr Color4f Opaque(float value=0.5f)
Opaque predefined color.
Definition: Color.h:100
static constexpr Color4f Spring
Spring (green) predefined color.
Definition: Color.h:131
static constexpr Color4f Blue
Blue predefined color.
Definition: Color.h:73
static Color4f darker(Color4f color, float percent=0.5f)
Compute a darker color.
#define GF_API
Definition: Portability.h:35
static constexpr Color4f Orange
Orange predefined color.
Definition: Color.h:116