Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper_functions: RGB(A) to float #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions BunnymodXT/helper_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 1 addition & 3 deletions BunnymodXT/hud_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 3 additions & 12 deletions BunnymodXT/triangle_drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading