Unravel Engine C++ Reference
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#pragma once
2
4#include <string>
5#include <algorithm>
6
7namespace math
8{
9using namespace glm;
10
11struct color
12{
13 static color white()
14 {
15 static color c(1.0f, 1.0f, 1.0f, 1.0f);
16 return c;
17 }
18 static color black()
19 {
20 static color c(0.0f, 0.0f, 0.0f, 1.0f);
21 return c;
22 }
24 {
25 static color c(0.0f, 0.0f, 0.0f, 0.0f);
26 return c;
27 }
28 static color red()
29 {
30 static color c(1.0f, 0.0f, 0.0f, 1.0f);
31 return c;
32 }
33 static color purple()
34 {
35 static color c(1.0f, 0.0f, 1.0f, 1.0f);
36 return c;
37 }
38
44 static color random(const char* seed)
45 {
46 return random(std::string(seed));
47 }
48
54 static color random(const std::string& seed)
55 {
56 // Use std::hash to convert string to numeric seed
57 std::hash<std::string> hasher;
58 std::size_t hash_value = hasher(seed);
59
60 // Simple linear congruential generator using the hash as seed
61 auto lcg = [](std::size_t& state) -> float {
62 state = (state * 1103515245u + 12345u) & 0x7fffffffu;
63 return static_cast<float>(state) / static_cast<float>(0x7fffffffu);
64 };
65
66 std::size_t state = hash_value;
67
68 // Generate hue in full range [0, 1)
69 float hue = lcg(state);
70
71 // Generate saturation with bias toward higher values for more vibrant colors
72 float saturation = 0.4f + 0.6f * lcg(state);
73
74 // Generate value/brightness with bias toward higher values for better visibility
75 float value = 0.6f + 0.4f * lcg(state);
76
77 return hsv(hue, saturation, value, 1.0f);
78 }
79
80 vec4 value{};
81
83 {
84 value.x = value.y = value.z = value.w = 0.0f;
85 }
86 color(int r, int g, int b, int a = 255)
87 {
88 float sc = 1.0f / 255.0f;
89 value.x = (float)r * sc;
90 value.y = (float)g * sc;
91 value.z = (float)b * sc;
92 value.w = (float)a * sc;
93 }
94 color(std::uint32_t rgba)
95 {
96 float sc = 1.0f / 255.0f;
97 value.x = (float)(rgba & 0xFF) * sc;
98 value.y = (float)((rgba >> 8) & 0xFF) * sc;
99 value.z = (float)((rgba >> 16) & 0xFF) * sc;
100 value.w = (float)(rgba >> 24) * sc;
101 }
102 color(float r, float g, float b, float a = 1.0f)
103 {
104 value.x = r;
105 value.y = g;
106 value.z = b;
107 value.w = a;
108 }
109 color(const vec4& col)
110 {
111 value = col;
112 }
113 inline operator std::uint32_t() const
114 {
115 return float4_to_u32(value);
116 }
117 inline operator vec4() const
118 {
119 return value;
120 }
121
122 inline void set_hsv(float h, float s, float v, float a = 1.0f)
123 {
124 hsv_to_rgb(h, s, v, value.x, value.y, value.z);
125 value.w = a;
126 }
127
128 static color hsv(float h, float s, float v, float a = 1.0f)
129 {
130 float r, g, b;
131 hsv_to_rgb(h, s, v, r, g, b);
132 return {r, g, b, a};
133 }
134
135 static vec4 u32_to_float4(std::uint32_t in)
136 {
137 float s = 1.0f / 255.0f;
138 return vec4((in & 0xFF) * s, ((in >> 8) & 0xFF) * s, ((in >> 16) & 0xFF) * s, (in >> 24) * s);
139 }
140
141 static std::uint32_t float4_to_u32(const vec4& in)
142 {
143 std::uint32_t out;
144 out = ((std::uint32_t)(saturate(in.x) * 255.0f));
145 out |= ((std::uint32_t)(saturate(in.y) * 255.0f)) << 8;
146 out |= ((std::uint32_t)(saturate(in.z) * 255.0f)) << 16;
147 out |= ((std::uint32_t)(saturate(in.w) * 255.0f)) << 24;
148 return out;
149 }
150
151 // Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]),
152 // from Foley & van Dam p592
153 // Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
154 static void rgb_to_hsv(float r, float g, float b, float& out_h, float& out_s, float& out_v)
155 {
156 float K = 0.f;
157 if(g < b)
158 {
159 const float tmp = g;
160 g = b;
161 b = tmp;
162 K = -1.f;
163 }
164 if(r < g)
165 {
166 const float tmp = r;
167 r = g;
168 g = tmp;
169 K = -2.f / 6.f - K;
170 }
171
172 const float chroma = r - (g < b ? g : b);
173 out_h = glm::abs(K + (g - b) / (6.f * chroma + 1e-20f));
174 out_s = chroma / (r + 1e-20f);
175 out_v = r;
176 }
177
178 // Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]),
179 // from Foley & van Dam p593
180 // also http://en.wikipedia.org/wiki/HSL_and_HSV
181 static void hsv_to_rgb(float h, float s, float v, float& out_r, float& out_g, float& out_b)
182 {
183 if(s == 0.0f)
184 {
185 // gray
186 out_r = out_g = out_b = v;
187 return;
188 }
189
190 h = glm::mod(h, 1.0f) / (60.0f / 360.0f);
191 auto i = int(h);
192 float f = h - float(i);
193 float p = v * (1.0f - s);
194 float q = v * (1.0f - s * f);
195 float t = v * (1.0f - s * (1.0f - f));
196
197 switch(i)
198 {
199 case 0:
200 out_r = v;
201 out_g = t;
202 out_b = p;
203 break;
204 case 1:
205 out_r = q;
206 out_g = v;
207 out_b = p;
208 break;
209 case 2:
210 out_r = p;
211 out_g = v;
212 out_b = t;
213 break;
214 case 3:
215 out_r = p;
216 out_g = q;
217 out_b = v;
218 break;
219 case 4:
220 out_r = t;
221 out_g = p;
222 out_b = v;
223 break;
224 case 5:
225 default:
226 out_r = v;
227 out_g = p;
228 out_b = q;
229 break;
230 }
231 }
232};
233} // namespace math
entt::handle b
entt::handle a
Definition bbox.cpp:5
static color red()
Definition color.h:28
color(const vec4 &col)
Definition color.h:109
static vec4 u32_to_float4(std::uint32_t in)
Definition color.h:135
void set_hsv(float h, float s, float v, float a=1.0f)
Definition color.h:122
static color random(const std::string &seed)
Generates a consistent random color based on a string seed.
Definition color.h:54
static color white()
Definition color.h:13
color(std::uint32_t rgba)
Definition color.h:94
vec4 value
Definition color.h:80
static color black()
Definition color.h:18
static void rgb_to_hsv(float r, float g, float b, float &out_h, float &out_s, float &out_v)
Definition color.h:154
static std::uint32_t float4_to_u32(const vec4 &in)
Definition color.h:141
static color purple()
Definition color.h:33
static color random(const char *seed)
Generates a consistent random color based on a string seed.
Definition color.h:44
static color transparent()
Definition color.h:23
static void hsv_to_rgb(float h, float s, float v, float &out_r, float &out_g, float &out_b)
Definition color.h:181
static color hsv(float h, float s, float v, float a=1.0f)
Definition color.h:128
color(int r, int g, int b, int a=255)
Definition color.h:86
color(float r, float g, float b, float a=1.0f)
Definition color.h:102