1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2025-02-01 06:57:25 +00:00
UltimateTrainingModpack/source/useful_visual.cpp
Shivam Dutt 216bcbc0c2 Moved Hash40 and Vector3f structs from l2c.cpp to useful.h
Moved LOAD64 macro from l2c_imports.hpp to useful.h
Moved round_to and the various linear interpolation functions from useful.h to useful_visual.h
More formatting fixes
2019-05-28 15:36:33 -05:00

35 lines
1 KiB
C++

#include "useful_visual.h"
#include <math.h>
#include "useful.h"
float round_to(float val, float align) {
return roundf(val / align) * align;
}
float lerp(float min, float max, float t) {
return min + (max - min) * t;
}
float unlerp(float min, float max, float val) {
return (val - min) / (max - min);
}
float lerp_bounded(float min, float max, float t) {
return t <= 0 ? min : t >= 1 ? max : lerp(min, max, t);
}
float unlerp_bounded(float min, float max, float val) {
return val <= min ? 0 : val >= max ? 1 : unlerp(min, max, val);
}
Vector3f color_lerp(Vector3f min_color, Vector3f max_color, float t, float gamma) {
float gamma_inv = 1.0f / gamma;
float align = 1.0f / 255.0f; // color components must be a multiple of 1/255
return {
round_to(powf(lerp_bounded(powf(min_color.x, gamma), powf(max_color.x, gamma), t), gamma_inv), align),
round_to(powf(lerp_bounded(powf(min_color.y, gamma), powf(max_color.y, gamma), t), gamma_inv), align),
round_to(powf(lerp_bounded(powf(min_color.z, gamma), powf(max_color.z, gamma), t), gamma_inv), align)
};
}