Symphony Of Empires
color.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (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, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // color.hpp
20 //
21 // Abstract:
22 // Primitive color type used through the engine
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <cstddef>
29 
30 namespace Eng3D {
32  struct Color {
33  float r = 0.f, g = 0.f, b = 0.f;
34  float a = 1.f;
35 
36  constexpr Color()
37  : r{ 0.f },
38  g{ 0.f },
39  b{ 0.f },
40  a{ 0.f }
41  {
42 
43  }
44 
45  constexpr Color(float red, float green, float blue, float alpha = 1.f)
46  : r{ red },
47  g{ green },
48  b{ blue },
49  a{ alpha }
50  {
51 
52  }
53 
54  ~Color() = default;
55 
62  constexpr static Color rgba8(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) {
63  return Color(red / 255.f, green / 255.f, blue / 255.f, alpha / 255.f);
64  }
65 
71  constexpr static Color rgb8(uint8_t red, uint8_t green, uint8_t blue) {
72  return rgba8(red, green, blue, 255);
73  }
74 
78  constexpr static Color argb32(uint32_t argb) {
79  const auto a = ((argb >> 24) & 0xFF) / 255.f;
80  const auto r = ((argb >> 16) & 0xFF) / 255.f;
81  const auto g = ((argb >> 8) & 0xFF) / 255.f;
82  const auto b = (argb & 0xFF) / 255.f;
83  return Color(r, g, b, a);
84  }
85 
89  constexpr static Color rgb32(uint32_t argb) {
90  return Color::argb32(argb | (0xff << 24));
91  }
92 
93  constexpr static Color abgr32(uint32_t abgr) {
94  const auto a = ((abgr >> 24) & 0xFF) / 255.f;
95  const auto b = ((abgr >> 16) & 0xFF) / 255.f;
96  const auto g = ((abgr >> 8) & 0xFF) / 255.f;
97  const auto r = (abgr & 0xFF) / 255.f;
98  return Color(r, g, b, a);
99  }
100 
101  constexpr static Color bgr32(uint32_t abgr) {
102  return Color::abgr32(abgr | (0xff << 24));
103  }
104 
107  constexpr uint32_t get_value() const {
108  const auto alpha = static_cast<uint8_t>(a * 255);
109  const auto red = static_cast<uint8_t>(r * 255);
110  const auto green = static_cast<uint8_t>(g * 255);
111  const auto blue = static_cast<uint8_t>(b * 255);
112  uint32_t color = (alpha << 24) | (blue << 16) | (green << 8) | (red);
113  return color;
114  }
115 
121  constexpr static Color lerp(Color color1, Color color2, float lamda) {
122  const auto r = color1.r * (1 - lamda) + color2.r * lamda;
123  const auto g = color1.g * (1 - lamda) + color2.g * lamda;
124  const auto b = color1.b * (1 - lamda) + color2.b * lamda;
125  return Color(r, g, b);
126  }
127 
128  constexpr static Color get_random(size_t mod) {
129  const uint32_t colors[] = {
130  0xFF000000, 0xFF00FF00, 0xFF0000FF, 0xFFFF0000, 0xFF01FFFE, 0xFFFFA6FE,
131  0xFFFFDB66, 0xFF006401, 0xFF010067, 0xFF95003A, 0xFF007DB5, 0xFFFF00F6,
132  0xFFFFEEE8, 0xFF774D00, 0xFF90FB92, 0xFF0076FF, 0xFFD5FF00, 0xFFFF937E,
133  0xFF6A826C, 0xFFFF029D, 0xFFFE8900, 0xFF7A4782, 0xFF7E2DD2, 0xFF85A900,
134  0xFFFF0056, 0xFFA42400, 0xFF00AE7E, 0xFF683D3B, 0xFFBDC6FF, 0xFF263400,
135  0xFFBDD393, 0xFF00B917, 0xFF9E008E, 0xFF001544, 0xFFC28C9F, 0xFFFF74A3,
136  0xFF01D0FF, 0xFF004754, 0xFFE56FFE, 0xFF788231, 0xFF0E4CA1, 0xFF91D0CB,
137  0xFFBE9970, 0xFF968AE8, 0xFFBB8800, 0xFF43002C, 0xFFDEFF74, 0xFF00FFC6,
138  0xFFFFE502, 0xFF620E00, 0xFF008F9C, 0xFF98FF52, 0xFF7544B1, 0xFFB500FF,
139  0xFF00FF78, 0xFFFF6E41, 0xFF005F39, 0xFF6B6882, 0xFF5FAD4E, 0xFFA75740,
140  0xFFA5FFD2, 0xFFFFB167, 0xFF009BFF, 0xFFE85EBE
141  };
142  return Eng3D::Color::rgb32(colors[mod % 64]);
143  }
144  };
145 }
Primitive color type used through the engine.
Definition: color.hpp:32
constexpr static Color rgb8(uint8_t red, uint8_t green, uint8_t blue)
Create a color from RGB components.
Definition: color.hpp:71
constexpr uint32_t get_value() const
Get the raw value of the color.
Definition: color.hpp:107
constexpr Color(float red, float green, float blue, float alpha=1.f)
Definition: color.hpp:45
constexpr Color()
Definition: color.hpp:36
constexpr static Color get_random(size_t mod)
Definition: color.hpp:128
constexpr static Color bgr32(uint32_t abgr)
Definition: color.hpp:101
float g
Definition: color.hpp:33
constexpr static Color abgr32(uint32_t abgr)
Definition: color.hpp:93
float b
Definition: color.hpp:33
constexpr static Color rgba8(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Create a color from RGBA components.
Definition: color.hpp:62
float r
Definition: color.hpp:33
constexpr static Color argb32(uint32_t argb)
Create a color from RGBA32 components.
Definition: color.hpp:78
~Color()=default
constexpr static Color rgb32(uint32_t argb)
Create a color from RGB32 components.
Definition: color.hpp:89
float a
Definition: color.hpp:34
constexpr static Color lerp(Color color1, Color color2, float lamda)
Combine two colors with LERP.
Definition: color.hpp:121