From 6f03f5a46b438d8993691e18378c378778da266e Mon Sep 17 00:00:00 2001 From: SmileyAG <58108407+SmileyAG@users.noreply.github.com> Date: Fri, 5 Jul 2024 04:18:28 +0400 Subject: [PATCH] helper_functions: RGB(A) to float --- BunnymodXT/helper_functions.hpp | 26 ++++++++++++++++++++++++++ BunnymodXT/hud_custom.cpp | 4 +--- BunnymodXT/triangle_drawing.cpp | 15 +++------------ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/BunnymodXT/helper_functions.hpp b/BunnymodXT/helper_functions.hpp index 44961a289..af60334f9 100644 --- a/BunnymodXT/helper_functions.hpp +++ b/BunnymodXT/helper_functions.hpp @@ -13,4 +13,30 @@ namespace helper_functions void com_fixslashes(std::string &str); std::string swap_lib(const char* current_lib_path, std::string new_lib_path, const char *start); void crash_if_failed(std::string str); + + inline void rgb_to_float(float &r, float &g, float &b) + { + r = std::clamp(r / 255.0f, 0.0f, 1.0f); + g = std::clamp(g / 255.0f, 0.0f, 1.0f); + b = std::clamp(b / 255.0f, 0.0f, 1.0f); + } + + inline void rgb_to_float(float *dest, const unsigned r, const unsigned g, const unsigned b) + { + dest[0] = std::clamp(r / 255.0f, 0.0f, 1.0f); + dest[1] = std::clamp(g / 255.0f, 0.0f, 1.0f); + dest[2] = std::clamp(b / 255.0f, 0.0f, 1.0f); + } + + inline void rgba_to_float(float &r, float &g, float &b, float &a) + { + rgb_to_float(r, g, b); + a = std::clamp(a / 255.0f, 0.0f, 1.0f); + } + + inline void rgba_to_float(float *dest, const unsigned r, const unsigned g, const unsigned b, const unsigned a) + { + rgb_to_float(dest, r, g, b); + dest[3] = std::clamp(a / 255.0f, 0.0f, 1.0f); + } } \ No newline at end of file diff --git a/BunnymodXT/hud_custom.cpp b/BunnymodXT/hud_custom.cpp index 732f2eb3d..fb349750c 100644 --- a/BunnymodXT/hud_custom.cpp +++ b/BunnymodXT/hud_custom.cpp @@ -451,9 +451,7 @@ namespace CustomHud std::istringstream ss(CVars::con_color.GetString()); ss >> r >> g >> b; - consoleColor[0] = r / 255.0f; - consoleColor[1] = g / 255.0f; - consoleColor[2] = b / 255.0f; + helper_functions::rgb_to_float(consoleColor, r, g, b); } // Default: yellowish. diff --git a/BunnymodXT/triangle_drawing.cpp b/BunnymodXT/triangle_drawing.cpp index 0a4c8de15..6ed2eb005 100644 --- a/BunnymodXT/triangle_drawing.cpp +++ b/BunnymodXT/triangle_drawing.cpp @@ -195,10 +195,7 @@ namespace TriangleDrawing float r, g, b, a; ServerDLL::GetTriggerColor(classname, r, g, b); ServerDLL::GetTriggerAlpha(classname, !active, true, a); - r /= 255.0f; - g /= 255.0f; - b /= 255.0f; - a /= 255.0f; + helper_functions::rgba_to_float(r, g, b, a); if (active) a = GetPulsatingAlpha(a, svTime + offset); @@ -229,10 +226,7 @@ namespace TriangleDrawing ss >> r >> g >> b >> a; static float triggerColor[4]; - triggerColor[0] = r / 255.0f; - triggerColor[1] = g / 255.0f; - triggerColor[2] = b / 255.0f; - triggerColor[3] = a / 255.0f; + helper_functions::rgba_to_float(triggerColor, r, g, b, a); pTriAPI->Color4f(triggerColor[0], triggerColor[1], triggerColor[2], triggerColor[3]); } else { @@ -436,10 +430,7 @@ namespace TriangleDrawing ss >> r >> g >> b >> a; static float triggerColor[4]; - triggerColor[0] = r / 255.0f; - triggerColor[1] = g / 255.0f; - triggerColor[2] = b / 255.0f; - triggerColor[3] = a / 255.0f; + helper_functions::rgba_to_float(triggerColor, r, g, b, a); pTriAPI->Color4f(triggerColor[0], triggerColor[1], triggerColor[2], triggerColor[3]); } else {