Backported some utility functionality from the KeyLight project
This commit is contained in:
parent
53e578a0a6
commit
5878d69b5e
7 changed files with 142 additions and 32 deletions
|
|
@ -1 +1 @@
|
||||||
1937
|
1938
|
||||||
|
|
|
||||||
|
|
@ -13,28 +13,28 @@ namespace ostd
|
||||||
setTypeName("ox::Color");
|
setTypeName("ox::Color");
|
||||||
BaseObject::setValid(true);
|
BaseObject::setValid(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(uint8_t rgb_single_value, uint8_t alpha)
|
Color::Color(uint8_t rgb_single_value, uint8_t alpha)
|
||||||
{
|
{
|
||||||
set(rgb_single_value, alpha);
|
set(rgb_single_value, alpha);
|
||||||
setTypeName("ox::Color");
|
setTypeName("ox::Color");
|
||||||
BaseObject::setValid(true);
|
BaseObject::setValid(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha)
|
Color::Color(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha)
|
||||||
{
|
{
|
||||||
set(_r, _g, _b, alpha);
|
set(_r, _g, _b, alpha);
|
||||||
setTypeName("ox::Color");
|
setTypeName("ox::Color");
|
||||||
BaseObject::setValid(true);
|
BaseObject::setValid(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(const String& color_string)
|
Color::Color(const String& color_string)
|
||||||
{
|
{
|
||||||
set(color_string);
|
set(color_string);
|
||||||
setTypeName("ox::Color");
|
setTypeName("ox::Color");
|
||||||
BaseObject::setValid(true);
|
BaseObject::setValid(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(const FloatCol& normalized_color)
|
Color::Color(const FloatCol& normalized_color)
|
||||||
{
|
{
|
||||||
set(normalized_color);
|
set(normalized_color);
|
||||||
|
|
@ -49,17 +49,17 @@ namespace ostd
|
||||||
b = copy.b;
|
b = copy.b;
|
||||||
a = copy.a;
|
a = copy.a;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Color::operator==(const Color& col2)
|
bool Color::operator==(const Color& col2)
|
||||||
{
|
{
|
||||||
return (r == col2.r && g == col2.g && b == col2.b && a == col2.a);
|
return (r == col2.r && g == col2.g && b == col2.b && a == col2.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Color::operator!=(const Color& col2)
|
bool Color::operator!=(const Color& col2)
|
||||||
{
|
{
|
||||||
return !(*this == col2);
|
return !(*this == col2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::operator=(const Color& copy)
|
Color& Color::operator=(const Color& copy)
|
||||||
{
|
{
|
||||||
BaseObject::operator=(copy);
|
BaseObject::operator=(copy);
|
||||||
|
|
@ -69,7 +69,7 @@ namespace ostd
|
||||||
a = copy.a;
|
a = copy.a;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::set(void)
|
Color& Color::set(void)
|
||||||
{
|
{
|
||||||
r = 0;
|
r = 0;
|
||||||
|
|
@ -78,7 +78,7 @@ namespace ostd
|
||||||
a = 255;
|
a = 255;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::set(uint8_t rgb_single_value, uint8_t alpha)
|
Color& Color::set(uint8_t rgb_single_value, uint8_t alpha)
|
||||||
{
|
{
|
||||||
r = rgb_single_value;
|
r = rgb_single_value;
|
||||||
|
|
@ -87,7 +87,7 @@ namespace ostd
|
||||||
a = alpha;
|
a = alpha;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::set(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha)
|
Color& Color::set(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha)
|
||||||
{
|
{
|
||||||
r = _r;
|
r = _r;
|
||||||
|
|
@ -96,7 +96,7 @@ namespace ostd
|
||||||
a = alpha;
|
a = alpha;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::set(const String& color_string)
|
Color& Color::set(const String& color_string)
|
||||||
{
|
{
|
||||||
String se(color_string);
|
String se(color_string);
|
||||||
|
|
@ -144,7 +144,7 @@ namespace ostd
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Color::set(const FloatCol& normalized_color)
|
Color& Color::set(const FloatCol& normalized_color)
|
||||||
{
|
{
|
||||||
r = static_cast<uint8_t>(std::round(normalized_color.r * 255));
|
r = static_cast<uint8_t>(std::round(normalized_color.r * 255));
|
||||||
|
|
@ -153,7 +153,7 @@ namespace ostd
|
||||||
a = static_cast<uint8_t>(std::round(normalized_color.a * 255));
|
a = static_cast<uint8_t>(std::round(normalized_color.a * 255));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
String Color::hexString(bool include_alpha, String prefix) const
|
String Color::hexString(bool include_alpha, String prefix) const
|
||||||
{
|
{
|
||||||
String hex = "";
|
String hex = "";
|
||||||
|
|
@ -165,7 +165,7 @@ namespace ostd
|
||||||
hex = prefix + String(hex).toUpper();
|
hex = prefix + String(hex).toUpper();
|
||||||
return hex;
|
return hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
String Color::rgbString(bool include_parenthesis, bool include_alpha) const
|
String Color::rgbString(bool include_parenthesis, bool include_alpha) const
|
||||||
{
|
{
|
||||||
String rgb = "";
|
String rgb = "";
|
||||||
|
|
@ -180,7 +180,7 @@ namespace ostd
|
||||||
rgb.add(")");
|
rgb.add(")");
|
||||||
return rgb;
|
return rgb;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Color::asInteger(eColorFormat format) const
|
uint32_t Color::asInteger(eColorFormat format) const
|
||||||
{
|
{
|
||||||
union uC32 {
|
union uC32 {
|
||||||
|
|
@ -203,17 +203,17 @@ namespace ostd
|
||||||
}
|
}
|
||||||
return c32_u.value;
|
return c32_u.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color::FloatCol Color::getNormalizedColor(void) const
|
Color::FloatCol Color::getNormalizedColor(void) const
|
||||||
{
|
{
|
||||||
return { r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f };
|
return { r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f };
|
||||||
}
|
}
|
||||||
|
|
||||||
String Color::toString(void) const
|
String Color::toString(void) const
|
||||||
{
|
{
|
||||||
return hexString(true, "#") + " -> rgba" + rgbString(true, true);
|
return hexString(true, "#") + " -> rgba" + rgbString(true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Color::print(bool newLine, OutputHandlerBase* __destination) const
|
void Color::print(bool newLine, OutputHandlerBase* __destination) const
|
||||||
{
|
{
|
||||||
if (__destination == nullptr)
|
if (__destination == nullptr)
|
||||||
|
|
@ -224,5 +224,48 @@ namespace ostd
|
||||||
if (newLine) __destination->nl();
|
if (newLine) __destination->nl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namesoace ox
|
void Color::RGBtoHSV(float r, float g, float b, float& h, float& s, float& v)
|
||||||
|
{
|
||||||
|
float max = std::max({r, g, b});
|
||||||
|
float min = std::min({r, g, b});
|
||||||
|
float d = max - min;
|
||||||
|
|
||||||
|
v = max;
|
||||||
|
|
||||||
|
if (max == 0)
|
||||||
|
{
|
||||||
|
s = 0;
|
||||||
|
h = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
s = d / max;
|
||||||
|
|
||||||
|
if (max == r) h = std::fmod((g - b) / d + 6.0f, 6.0f);
|
||||||
|
else if (max == g) h = (b - r) / d + 2.0f;
|
||||||
|
else h = (r - g) / d + 4.0f;
|
||||||
|
|
||||||
|
h /= 6.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Color::HSVtoRGB(float h, float s, float v, float& r, float& g, float& b)
|
||||||
|
{
|
||||||
|
int i = static_cast<int>(h * 6);
|
||||||
|
float f = h * 6 - i;
|
||||||
|
float p = v * (1 - s);
|
||||||
|
float q = v * (1 - f * s);
|
||||||
|
float t = v * (1 - (1 - f) * s);
|
||||||
|
|
||||||
|
switch (i % 6)
|
||||||
|
{
|
||||||
|
case 0: r = v, g = t, b = p; break;
|
||||||
|
case 1: r = q, g = v, b = p; break;
|
||||||
|
case 2: r = p, g = v, b = t; break;
|
||||||
|
case 3: r = p, g = q, b = v; break;
|
||||||
|
case 4: r = t, g = p, b = v; break;
|
||||||
|
case 5: r = v, g = p, b = q; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,13 +51,16 @@ namespace ostd
|
||||||
inline void invalidate(void) override { }
|
inline void invalidate(void) override { }
|
||||||
inline void setValid(bool valid) override { }
|
inline void setValid(bool valid) override { }
|
||||||
|
|
||||||
|
static void RGBtoHSV(float r, float g, float b, float& h, float& s, float& v);
|
||||||
|
static void HSVtoRGB(float h, float s, float v, float& r, float& g, float& b);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
uint8_t r;
|
uint8_t r;
|
||||||
uint8_t g;
|
uint8_t g;
|
||||||
uint8_t b;
|
uint8_t b;
|
||||||
uint8_t a;
|
uint8_t a;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@
|
||||||
#undef _WIN32_WINNT
|
#undef _WIN32_WINNT
|
||||||
#endif
|
#endif
|
||||||
#define _WIN32_WINNT 0x0501
|
#define _WIN32_WINNT 0x0501
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#define MAC_OS
|
||||||
#else
|
#else
|
||||||
#define LINUX_OS
|
#define LINUX_OS
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
#include "Logger.hpp"
|
||||||
|
|
||||||
namespace ostd
|
namespace ostd
|
||||||
{
|
{
|
||||||
|
|
@ -62,16 +63,16 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String Utils::getHomeDirPath(void)
|
std::filesystem::path Utils::getHomeDirPath(void)
|
||||||
{
|
{
|
||||||
ostd::String home_path = "";
|
String home_path = "";
|
||||||
#ifdef WINDOWS_OS
|
#ifdef WINDOWS_OS
|
||||||
home_path = ostd::String(getenv("HOMEDRIVE")) + "\\" + ostd::String(getenv("HOMEPATH"));
|
home_path = String(getenv("HOMEDRIVE")) + "\\" + String(getenv("HOMEPATH"));
|
||||||
#elif defined(LINUX_OS)
|
#elif defined(LINUX_OS) || defined(MAC_OS)
|
||||||
home_path = ostd::String(getenv("HOME"));
|
home_path = String(getenv("HOME"));
|
||||||
#else
|
#else
|
||||||
home_path = "NULL";
|
home_path = "NULL";
|
||||||
#endif
|
#endif
|
||||||
return home_path;
|
return home_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,4 +81,50 @@ namespace ostd
|
||||||
return std::filesystem::current_path();
|
return std::filesystem::current_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Utils::ensureDirectory(const String& path)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs::path dir(path);
|
||||||
|
|
||||||
|
if (!fs::exists(dir))
|
||||||
|
{
|
||||||
|
if (!fs::create_directories(dir))
|
||||||
|
{
|
||||||
|
OX_ERROR("Failed to create directory: %s", path.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!fs::is_directory(dir))
|
||||||
|
{
|
||||||
|
OX_ERROR("Path exists but is not a directory: %s", path.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const fs::filesystem_error& e)
|
||||||
|
{
|
||||||
|
OX_ERROR("Filesystem error for path '%s': %s", path.c_str(), e.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Utils::deleteDirectory(const String& path)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (fs::exists(path) && fs::is_directory(path))
|
||||||
|
{
|
||||||
|
fs::remove_all(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (const fs::filesystem_error& e)
|
||||||
|
{
|
||||||
|
OX_ERROR("Failed to delete tmp folder '%s': %s", path.c_str(), e.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,4 +74,16 @@ namespace ostd
|
||||||
{
|
{
|
||||||
return std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count() - Utils::s_startTime_ms;
|
return std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count() - Utils::s_startTime_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String Utils::secondsToFormattedString(int32_t totalSeconds)
|
||||||
|
{
|
||||||
|
int32_t hours = totalSeconds / 3600;
|
||||||
|
int32_t minutes = (totalSeconds % 3600) / 60;
|
||||||
|
int32_t seconds = totalSeconds % 60;
|
||||||
|
String fmtstr = "";
|
||||||
|
fmtstr.add(String("").add(hours).addLeftPadding(2, '0')).add(":");
|
||||||
|
fmtstr.add(String("").add(minutes).addLeftPadding(2, '0')).add(":");
|
||||||
|
fmtstr.add(String("").add(seconds).addLeftPadding(2, '0'));
|
||||||
|
return fmtstr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ namespace ostd
|
||||||
//Implemented in <Time.hpp>
|
//Implemented in <Time.hpp>
|
||||||
static void sleep(uint32_t __time, eTimeUnits __unit = eTimeUnits::Milliseconds);
|
static void sleep(uint32_t __time, eTimeUnits __unit = eTimeUnits::Milliseconds);
|
||||||
static uint64_t getRunningTime_ms(void);
|
static uint64_t getRunningTime_ms(void);
|
||||||
|
static String secondsToFormattedString(int32_t totalSeconds);
|
||||||
|
|
||||||
//Implemented in <md5.cpp>
|
//Implemented in <md5.cpp>
|
||||||
static String md5(const String& str);
|
static String md5(const String& str);
|
||||||
|
|
@ -47,8 +48,10 @@ namespace ostd
|
||||||
static std::vector<std::filesystem::path> listFilesInDirectoryRecursive(const String& directoryPath);
|
static std::vector<std::filesystem::path> listFilesInDirectoryRecursive(const String& directoryPath);
|
||||||
static std::vector<std::filesystem::path> listDirectoriesInDirectoryRecursive(const String& directoryPath);
|
static std::vector<std::filesystem::path> listDirectoriesInDirectoryRecursive(const String& directoryPath);
|
||||||
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
|
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
|
||||||
static ostd::String getHomeDirPath(void);
|
static std::filesystem::path getHomeDirPath(void);
|
||||||
static std::filesystem::path getWorkingDirPath(void);
|
static std::filesystem::path getWorkingDirPath(void);
|
||||||
|
static bool ensureDirectory(const String& path);
|
||||||
|
static bool deleteDirectory(const String& path);
|
||||||
|
|
||||||
//Implemented in <ShuntingYard.cpp>
|
//Implemented in <ShuntingYard.cpp>
|
||||||
static int32_t solveIntegerExpression(const String& expr);
|
static int32_t solveIntegerExpression(const String& expr);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue