1#ifndef IV_SRC_CORE_UTILS_HPP_
2#define IV_SRC_CORE_UTILS_HPP_
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))
32 if constexpr (std::is_floating_point_v<T>)
34 if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1 || a < 0 || a > 1)
36 throw std::invalid_argument(
"Color components must be in the range [0, 1]");
40 (
static_cast<uint8_t
>(r * 255) << 16) |
41 (
static_cast<uint8_t
>(g * 255) << 8) |
static_cast<uint8_t
>(b * 255));
45 if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255)
47 throw std::invalid_argument(
"Color components must be in the range [0, 255]");
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))
68 if constexpr (std::is_floating_point_v<T>)
80std::optional<iv::types::color>
hexToColor(std::string_view hex);
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))
88 if (b_out ==
nullptr || g_out ==
nullptr || r_out ==
nullptr)
90 throw std::invalid_argument(
"Null pointer provided for color component output.");
93 if constexpr (std::is_floating_point_v<T>)
95 if (alpha_out.has_value())
97 *alpha_out = (hexValue >> 24);
100 *b_out = ((hexValue >> 16) & 0xFF) / 255.0;
101 *g_out = ((hexValue >> 8) & 0xFF) / 255.0;
102 *r_out = (hexValue & 0xFF) / 255.0;
106 if (alpha_out.has_value())
108 *alpha_out = (hexValue >> 24) & 0xFF;
111 *b_out = (hexValue >> 16) & 0xFF;
112 *g_out = (hexValue >> 8) & 0xFF;
113 *r_out = hexValue & 0xFF;
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))
124 if (r_out ==
nullptr || g_out ==
nullptr || b_out ==
nullptr)
126 throw std::invalid_argument(
"Null pointer provided for color component output.");
129 if constexpr (std::is_floating_point_v<T>)
131 if (alpha_out.has_value())
134 static_cast<T
>((hexValue >> 24) & 0xFF) /
static_cast<T
>(255.0);
137 *r_out =
static_cast<T
>(((hexValue >> 16) & 0xFF)) /
static_cast<T
>(255.0);
138 *g_out =
static_cast<T
>(((hexValue >> 8) & 0xFF)) /
static_cast<T
>(255.0);
139 *b_out =
static_cast<T
>((hexValue & 0xFF)) /
static_cast<T
>(255.0);
143 if (alpha_out.has_value())
145 alpha_out->get() =
static_cast<unsigned char>((hexValue >> 24) & 0xFF);
148 *r_out = (hexValue >> 16) & 0xFF;
149 *g_out = (hexValue >> 8) & 0xFF;
150 *b_out = hexValue & 0xFF;
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))
160 if (hexValue_out ==
nullptr)
162 throw std::invalid_argument(
"Null pointer provided for color component output.");
165 if constexpr (std::is_floating_point_v<T>)
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)));
174 *hexValue_out =
static_cast<iv::types::color>((alpha << 24) | (r << 16) | (g << 8) | b);
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))
188 if constexpr (std::is_floating_point_v<T>)
190 return (hexValue & 0x00FFFFFF) | (
static_cast<uint32_t
>(alpha * 255) << 24);
194 return (hexValue & 0x00FFFFFF) | (
static_cast<uint32_t
>(alpha) << 24);
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))
205 return addAlpha(nonAlphaColor, alpha);
269template<
typename T1,
typename T2,
typename T3 =
double>
271 requires std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2> && std::is_arithmetic_v<T3>
273 return (std::fabs(
static_cast<double>(d1) -
static_cast<double>(d2)) <
static_cast<double>(tolerance));
278 requires std::is_floating_point_v<T>
282 return std::round(value);
285 const T factor = std::pow(10.0, decimals);
286 return std::round(value * factor) / factor;
294#define TIME_IT(expr) \
297 auto inicio = std::chrono::high_resolution_clock::now(); \
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; \
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