TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_color.hpp
1 
9 #ifndef TurtleBrains_Color_hpp
10 #define TurtleBrains_Color_hpp
11 
12 #include <turtle_brains/core/tb_types.hpp>
13 #include <turtle_brains/core/tb_dynamic_structure.hpp>
14 #include <turtle_brains/math/tb_math.hpp>
15 
16 namespace TurtleBrains::Graphics
17 {
18 
23  class Color
24  {
25  public:
29  constexpr Color(void) noexcept :
30  mAlpha(1.0f),
31  mRed(1.0f),
32  mGreen(1.0f),
33  mBlue(1.0f),
34  mARGB(0xFFFFFFFF)
35  {
36  }
37 
42  constexpr explicit Color(const tbCore::uint32& colorARGB) noexcept :
43  mAlpha(static_cast<float>((colorARGB >> 24) & 0xFF) / 255.0f),
44  mRed(static_cast<float>((colorARGB >> 16) & 0xFF) / 255.0f),
45  mGreen(static_cast<float>((colorARGB >> 8) & 0xFF) / 255.0f),
46  mBlue(static_cast<float>((colorARGB) & 0xFF) / 255.0f),
47  mARGB(colorARGB)
48  {
49  }
50 
62  constexpr explicit Color(const float clampedAlpha, const float clampedRed, const float clampedGreen, const float clampedBlue) noexcept :
63  mAlpha(tbMath::Clamp(clampedAlpha, 0.0f, 1.0f)),
64  mRed(tbMath::Clamp(clampedRed, 0.0f, 1.0f)),
65  mGreen(tbMath::Clamp(clampedGreen, 0.0f, 1.0f)),
66  mBlue(tbMath::Clamp(clampedBlue, 0.0f, 1.0f)),
67  mARGB((((static_cast<tbCore::uint32>(mAlpha * 255.0f) & 0xFF) << 24) | ((static_cast<tbCore::uint32>(mRed * 255.0f) & 0xFF) << 16) |
68  ((static_cast<tbCore::uint32>(mGreen * 255.0f) & 0xFF) << 8) | (static_cast<tbCore::uint32>(mBlue * 255.0f) & 0xFF)))
69  {
70  }
71 
81  bool SetColor(const tbCore::DynamicStructure& colorData, bool triggerErrorOnFailure = true);
82 
87  void SetColor(const tbCore::uint32& colorARGB);
88 
100  void SetColor(const float clampedAlpha, const float clampedRed, const float clampedGreen, const float clampedBlue);
101 
112  void SetColor(const float clampedTween, const Color& startColor, const Color& finalColor);
113 
118 
123 
128 
133 
138 
143 
147  float GetAlpha(void) const;
148 
149  void SetAlpha(const float alpha);
150  void SetAlpha(const int alpha);
151 
155  float GetRed(void) const;
156 
157  void SetRed(const float red);
158  void SetRed(const int red);
159 
163  float GetGreen(void) const;
164 
165  void SetGreen(const float green);
166  void SetGreen(const int green);
167 
171  float GetBlue(void) const;
172 
173  void SetBlue(const float blue);
174  void SetBlue(const int blue);
175 
176  private:
177  //The floats may disappear for size reasons and use GetChannelAsFloat()
178  float mAlpha;
179  float mRed;
180  float mGreen;
181  float mBlue;
182 
183  tbCore::uint32 mARGB;
184  };
185 
186  namespace Implementation { Color RampedColor(const Color& color500, const float rampValue); };
187 
188  namespace ColorPalette
189  {
190  template<const Color& baseColor, int lightness = 500> Color Lightness =
191  Implementation::RampedColor(baseColor, static_cast<float>(lightness) / 1000.0f);
192 
193  template<const Color& baseColor> Color Lighter = Lightness<baseColor, 100>;
194  template<const Color& baseColor> Color Light = Lightness<baseColor, 300>;
195  template<const Color& baseColor> Color Dark = Lightness<baseColor, 700>;
196  template<const Color& baseColor> Color Darker = Lightness<baseColor, 900>;
197 
198  constexpr Color Opaque(0xFFFFFFFF);
199  constexpr Color Transparent(0x00FFFFFF);
200 
201  constexpr Color Black(0xFF000000);
202  constexpr Color White(0xFFFFFFFF);
203  constexpr Color Magenta(0xFFFF00FF);
204 
205 #define tb_make_color(name, rgb) \
206  constexpr Color name(0xFF000000 | rgb); \
207  inline const Color Light##name = Light<name>; \
208  inline const Color Lighter##name = Lighter<name>; \
209  inline const Color Dark##name = Dark<name>; \
210  inline const Color Darker##name = Darker<name>;
211 
212  tb_make_color(Purple, 0xFF6F42C1);
213  tb_make_color(Indigo, 0xFF6610F2);
214  tb_make_color(Blue, 0xFF0D6EFD);
215  tb_make_color(Cyan, 0xFF0DCAF0);
216  tb_make_color(Teal, 0xFF20C997);
217  tb_make_color(Green, 0xFF198754);
218  tb_make_color(Yellow, 0xFFFFC107);
219  tb_make_color(Orange, 0xFFFD7E14);
220  tb_make_color(Pink, 0xFFD63384);
221  tb_make_color(Red, 0xFFDC3545);
222  tb_make_color(Gray, 0xFFADB5BD);
223  };
224 
225 
226  //template<typename Type> DynamicStructure ToDynamicStructure(const Type& object)
227  //{
228  // return tbCore::ToDynamicStructure(object);
229  //}
230 
231  //template<typename Type> Type FromDynamicStructure(const DynamicStructure& data)
232  //{
233  // return tbCore::FromDynamicStructure(data);
234  //}
235 
236  //template<> inline DynamicStructure ToDynamicStructure<Color>(const Color& color)
237  //{
238  // return tbCore::DynamicStructure(Core::ToString(color));
239  //}
240 
241  //template<> inline Color FromDynamicStructure<Color>(const DynamicStructure& data)
242  //{
243  // return Core::FromString<Color>(data.AsString());
244  //}
245 
246 
247 }; /* namespace TurtleBrains::Graphics */
248 
250 
251 namespace TurtleBrains
252 {
253  using Color = Graphics::Color;
254  namespace ColorPalette = Graphics::ColorPalette;
255 
256  template<> inline DynamicStructure ToDynamicStructure<Graphics::Color>(const Graphics::Color& color)
257  {
258  //return tbCore::DynamicStructure(Core::ToString(color));
259  tb_unused(color);
260  return tbCore::DynamicStructure("this is a color");
261  }
262 
263  template<> inline Graphics::Color FromDynamicStructure<Graphics::Color>(const DynamicStructure& data)
264  {
265  tb_unused(data);
266  return Graphics::Color();
267 
268  //return Core::FromString<Graphics::Color>(data.AsString());
269  }
270 };
271 
272 
273 // 2025-11-23: This may eventually move into TyreBytes::ToolBox. Depend on it at your own risk.
274 namespace TyreBytes
275 {
276  namespace ColorPalette
277  {
278  static const TurtleBrains::Graphics::Color Blue(0xFF2E9FFF);
279  static const TurtleBrains::Graphics::Color Pink(0xFFFF2E9F);
280  static const TurtleBrains::Graphics::Color Orange(0xFFFF9F2E);
281 
282  static const TurtleBrains::Graphics::Color Red(0xFFF1284E);
283  static const TurtleBrains::Graphics::Color Green(0xFF2EFF50);
284  };
285 };
286 
287 #endif /* TurtleBrains_Color_hpp */
Definition: tb_dynamic_structure.hpp:95
Definition: tb_color.hpp:24
void SetColor(const tbCore::uint32 &colorARGB)
tbCore::uint8 GetBlueByte(void) const
float GetGreen(void) const
tbCore::uint32 GetColorARGB(void) const
tbCore::uint8 GetGreenByte(void) const
tbCore::uint32 GetColorABGR(void) const
constexpr Color(const tbCore::uint32 &colorARGB) noexcept
Definition: tb_color.hpp:42
constexpr Color(const float clampedAlpha, const float clampedRed, const float clampedGreen, const float clampedBlue) noexcept
Definition: tb_color.hpp:62
void SetColor(const float clampedAlpha, const float clampedRed, const float clampedGreen, const float clampedBlue)
tbCore::uint8 GetRedByte(void) const
tbCore::uint8 GetAlphaByte(void) const
void SetColor(const float clampedTween, const Color &startColor, const Color &finalColor)
float GetAlpha(void) const
bool SetColor(const tbCore::DynamicStructure &colorData, bool triggerErrorOnFailure=true)
constexpr Color(void) noexcept
Definition: tb_color.hpp:29
#define tb_unused(parameter)
Definition: tb_defines.hpp:25
std::uint8_t uint8
Unsigned integer with a size of 8 bits. Supports values from 0 to 255.
Definition: tb_types.hpp:22
std::uint32_t uint32
Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1).
Definition: tb_types.hpp:27
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22