Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_CORE_UTILS_HPP_
2#define IV_SRC_CORE_UTILS_HPP_
3
4#include "core/defines.hpp"
5
6#include <cmath>
7#include <fmt/format.h>
8#include <iostream>
9#include <optional>
10
11namespace iv
12{
13
14namespace color::utils
15{
16
26template<typename T>
27iv::types::color compactRgb(T r, T g, T b, T a)
28 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
29 (std::numeric_limits<T>::radix == 2)) ||
30 (std::is_unsigned_v<T> && sizeof(T) == 1))
31{
32 if constexpr (std::is_floating_point_v<T>)
33 {
34 if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1 || a < 0 || a > 1)
35 {
36 throw std::invalid_argument("Color components must be in the range [0, 1]");
37 }
38
39 return static_cast<iv::types::color>((static_cast<uint8_t>(a * 255) << 24) |
40 (static_cast<uint8_t>(r * 255) << 16) |
41 (static_cast<uint8_t>(g * 255) << 8) | static_cast<uint8_t>(b * 255));
42 }
43 else
44 {
45 if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255)
46 {
47 throw std::invalid_argument("Color components must be in the range [0, 255]");
48 }
49
50 return static_cast<iv::types::color>((a << 24) | (r << 16) | (g << 8) | b);
51 }
52}
53
62template<typename T>
64 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
65 (std::numeric_limits<T>::radix == 2)) ||
66 (std::is_unsigned_v<T> && sizeof(T) == 1))
67{
68 if constexpr (std::is_floating_point_v<T>)
69 {
70 return compactRgb<double>(r, g, b, 1.f);
71 }
72 else
73 {
74 return compactRgb<uint8_t>(r, g, b, 255);
75 }
76}
77
78std::string colorToHex(iv::types::color value, bool hasAlpha = true);
79
80std::optional<iv::types::color> hexToColor(std::string_view hex);
81
82template<typename T>
83void hexToBgr(iv::types::color hexValue, T *b_out, T *g_out, T *r_out, std::optional<T> alpha_out = std::nullopt)
84 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
85 (std::numeric_limits<T>::radix == 2)) ||
86 (std::is_unsigned_v<T> && sizeof(T) == 1))
87{
88 if (b_out == nullptr || g_out == nullptr || r_out == nullptr)
89 {
90 throw std::invalid_argument("Null pointer provided for color component output.");
91 }
92
93 if constexpr (std::is_floating_point_v<T>)
94 {
95 if (alpha_out.has_value())
96 {
97 *alpha_out = (hexValue >> 24);// Extract alpha component
98 }
99
100 *b_out = ((hexValue >> 16) & 0xFF) / 255.0;// Extract blue component
101 *g_out = ((hexValue >> 8) & 0xFF) / 255.0; // Extract green component
102 *r_out = (hexValue & 0xFF) / 255.0; // Extract red component
103 }
104 else
105 {
106 if (alpha_out.has_value())
107 {
108 *alpha_out = (hexValue >> 24) & 0xFF;// Extract alpha component
109 }
110
111 *b_out = (hexValue >> 16) & 0xFF;// Extract blue component
112 *g_out = (hexValue >> 8) & 0xFF; // Extract green component
113 *r_out = hexValue & 0xFF; // Extract red component
114 }
115}
116
117template<typename T>
118void hexToRgb(iv::types::color hexValue, T *r_out, T *g_out, T *b_out,
119 std::optional<std::reference_wrapper<T>> alpha_out = std::nullopt)
120 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
121 (std::numeric_limits<T>::radix == 2)) ||
122 (std::is_unsigned_v<T> && sizeof(T) == 1))
123{
124 if (r_out == nullptr || g_out == nullptr || b_out == nullptr)
125 {
126 throw std::invalid_argument("Null pointer provided for color component output.");
127 }
128
129 if constexpr (std::is_floating_point_v<T>)
130 {
131 if (alpha_out.has_value())
132 {
133 alpha_out->get() =
134 static_cast<T>((hexValue >> 24) & 0xFF) / static_cast<T>(255.0);// Extract alpha component
135 }
136
137 *r_out = static_cast<T>(((hexValue >> 16) & 0xFF)) / static_cast<T>(255.0);// Extract red component
138 *g_out = static_cast<T>(((hexValue >> 8) & 0xFF)) / static_cast<T>(255.0); // Extract green component
139 *b_out = static_cast<T>((hexValue & 0xFF)) / static_cast<T>(255.0); // Extract blue component
140 }
141 else
142 {
143 if (alpha_out.has_value())
144 {
145 alpha_out->get() = static_cast<unsigned char>((hexValue >> 24) & 0xFF);// Extract alpha component
146 }
147
148 *r_out = (hexValue >> 16) & 0xFF;// Extract red component
149 *g_out = (hexValue >> 8) & 0xFF; // Extract green component
150 *b_out = hexValue & 0xFF; // Extract blue component
151 }
152}
153
154template<typename T>
155void rgbToHex(T r, T g, T b, T alpha, iv::types::color *hexValue_out)
156 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
157 (std::numeric_limits<T>::radix == 2)) ||
158 (std::is_unsigned_v<T> && sizeof(T) == 1))
159{
160 if (hexValue_out == nullptr)
161 {
162 throw std::invalid_argument("Null pointer provided for color component output.");
163 }
164
165 if constexpr (std::is_floating_point_v<T>)
166 {
167 *hexValue_out = static_cast<iv::types::color>((static_cast<uint8_t>(alpha * static_cast<T>(255.0)) << 24) |
168 (static_cast<uint8_t>(r * static_cast<T>(255.0)) << 16) |
169 (static_cast<uint8_t>(g * static_cast<T>(255.0)) << 8) |
170 static_cast<uint8_t>(b * static_cast<T>(255.0)));
171 }
172 else
173 {
174 *hexValue_out = static_cast<iv::types::color>((alpha << 24) | (r << 16) | (g << 8) | b);
175 }
176}
177
178double getAlpha(iv::types::color hexValue);
179
181
182template<typename T>
184 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
185 (std::numeric_limits<T>::radix == 2)) ||
186 (std::is_unsigned_v<T> && sizeof(T) == 1))
187{
188 if constexpr (std::is_floating_point_v<T>)
189 {
190 return (hexValue & 0x00FFFFFF) | (static_cast<uint32_t>(alpha * 255) << 24);
191 }
192 else
193 {
194 return (hexValue & 0x00FFFFFF) | (static_cast<uint32_t>(alpha) << 24);
195 }
196}
197
198template<typename T>
200 requires((std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559 &&
201 (std::numeric_limits<T>::radix == 2)) ||
202 (std::is_unsigned_v<T> && sizeof(T) == 1))
203{
204 iv::types::color nonAlphaColor = removeAlpha(hexValue);
205 return addAlpha(nonAlphaColor, alpha);
206}
207
208//TODO: Revisar el uso de estas funciones
209
217
225uint32_t darkenColorRgb(uint32_t rgb, double factorDarken);
226
227iv::types::color darkenColorArgb(iv::types::color argb, double factorDarken);
228
236iv::types::color illuminateColorRgb(iv::types::color rgb, double factorIlluminate);
237
244bool isDarkColorRgb(uint32_t rgb);
245
252bool isIlluminateColorRgb(uint32_t rgb);
253
262void getRgb2Char(iv::types::color color, unsigned char *r, unsigned char *g, unsigned char *b);
263
264}// namespace color::utils
265
266namespace math::utils
267{
268
269template<typename T1, typename T2, typename T3 = double>
270bool isEqual(T1 d1, T2 d2, T3 tolerance = iv::constants::maths::Precision)
271 requires std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2> && std::is_arithmetic_v<T3>
272{
273 return (std::fabs(static_cast<double>(d1) - static_cast<double>(d2)) < static_cast<double>(tolerance));
274}
275
276template<typename T>
277T roundToDecimals(T value, uint8_t decimals)
278 requires std::is_floating_point_v<T>
279{
280 if (decimals == 0)
281 {
282 return std::round(value);
283 }
284
285 const T factor = std::pow(10.0, decimals);
286 return std::round(value * factor) / factor;
287}
288
289}// namespace math::utils
290
291namespace time::utils
292{
293
294#define TIME_IT(expr) \
295 do \
296 { \
297 auto inicio = std::chrono::high_resolution_clock::now(); \
298 expr; \
299 auto fin = std::chrono::high_resolution_clock::now(); \
300 std::chrono::duration<double, std::milli> duracion = fin - inicio; \
301 std::cout << "[" #expr "] Tiempo de ejecución: " << duracion.count() << " ms" << std::endl; \
302 } while (0)
303
305{
306 if (a > b)
307 {
308 return a - b;
309 }
310 return b - a;
311}
312
313std::string formatCalendarTimeFromMilliseconds(iv::types::milliseconds timeInMilliseconds);
314
315std::string formatTimeUnitsFromMilliseconds(iv::types::milliseconds timeInMilliseconds, bool isShortForm = false);
316
317}// namespace time::utils
318
319namespace utils
320{
321
322uint8_t maxBitsLength(iv::eDataType dataType);
323
324}// namespace utils
325
326}// namespace iv
327
328#endif//IV_SRC_CORE_UTILS_HPP_
void getRgb2Char(iv::types::color color, unsigned char *r, unsigned char *g, unsigned char *b)
Get unsigned char values from a 2 Bytes color.
std::optional< iv::types::color > hexToColor(const std::string_view hex)
Definition utils.cpp:28
iv::types::color compactRgb(T r, T g, T b, T a)
Returns an ARGB value according to the values given as unsigned char.
Definition utils.hpp:27
iv::types::color addAlpha(iv::types::color hexValue, T alpha)
Definition utils.hpp:183
iv::types::color colorBorder(const iv::types::color colorState)
Provides a border color.
Definition utils.cpp:54
iv::types::color illuminateColorRgb(const iv::types::color rgb, const double factorIlluminate)
Get a brighter version of the given RGB.
Definition utils.cpp:186
void hexToBgr(iv::types::color hexValue, T *b_out, T *g_out, T *r_out, std::optional< T > alpha_out=std::nullopt)
Definition utils.hpp:83
std::string colorToHex(iv::types::color value, const bool hasAlpha)
Definition utils.cpp:18
iv::types::color removeAlpha(const iv::types::color hexValue)
Definition utils.cpp:43
void rgbToHex(T r, T g, T b, T alpha, iv::types::color *hexValue_out)
Definition utils.hpp:155
bool isIlluminateColorRgb(const uint32_t rgb)
Check if the provided RGB is illuminated.
Definition utils.cpp:224
void hexToRgb(iv::types::color hexValue, T *r_out, T *g_out, T *b_out, std::optional< std::reference_wrapper< T > > alpha_out=std::nullopt)
Definition utils.hpp:118
uint32_t darkenColorRgb(const uint32_t rgb, const double factorDarken)
Get a darker version of the given RGB.
Definition utils.cpp:122
bool isDarkColorRgb(const uint32_t rgb)
Check if the provided RGB is dark.
Definition utils.cpp:207
iv::types::color changeAlpha(iv::types::color hexValue, T alpha)
Definition utils.hpp:199
iv::types::color darkenColorArgb(const iv::types::color argb, const double factorDarken)
Definition utils.cpp:137
double getAlpha(const iv::types::color hexValue)
Definition utils.cpp:38
constexpr double Precision
Definition defines.hpp:130
bool isEqual(T1 d1, T2 d2, T3 tolerance=iv::constants::maths::Precision)
Definition utils.hpp:270
T roundToDecimals(T value, uint8_t decimals)
Definition utils.hpp:277
std::string formatCalendarTimeFromMilliseconds(const iv::types::milliseconds timeInMilliseconds)
Definition utils.cpp:240
std::string formatTimeUnitsFromMilliseconds(iv::types::milliseconds timeInMilliseconds, bool isShortForm)
Definition utils.cpp:249
iv::types::timestamp safeAbsoluteDiff(const iv::types::timestamp a, const iv::types::timestamp b)
Definition utils.hpp:304
uint64_t timestamp
Definition types.hpp:21
uint64_t milliseconds
Definition types.hpp:22
uint32_t color
Definition types.hpp:12
uint8_t maxBitsLength(const iv::eDataType dataType)
Definition utils.cpp:324
Definition AlarmsManager.cpp:18
eDataType
Definition enums.hpp:207