Backported some utility functionality from the KeyLight project

This commit is contained in:
Sylar 2026-01-04 17:29:32 +01:00
parent 53e578a0a6
commit 5878d69b5e
7 changed files with 142 additions and 32 deletions

View file

@ -1 +1 @@
1937
1938

View file

@ -225,4 +225,47 @@ namespace ostd
}
}
} //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;
}
}
}

View file

@ -51,6 +51,9 @@ namespace ostd
inline void invalidate(void) 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:
uint8_t r;
uint8_t g;

View file

@ -7,6 +7,8 @@
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0501
#elif defined(__APPLE__)
#define MAC_OS
#else
#define LINUX_OS
#endif

View file

@ -1,4 +1,5 @@
#include "Utils.hpp"
#include "Logger.hpp"
namespace ostd
{
@ -62,16 +63,16 @@ namespace ostd
return list;
}
ostd::String Utils::getHomeDirPath(void)
std::filesystem::path Utils::getHomeDirPath(void)
{
ostd::String home_path = "";
#ifdef WINDOWS_OS
home_path = ostd::String(getenv("HOMEDRIVE")) + "\\" + ostd::String(getenv("HOMEPATH"));
#elif defined(LINUX_OS)
home_path = ostd::String(getenv("HOME"));
#else
home_path = "NULL";
#endif
String home_path = "";
#ifdef WINDOWS_OS
home_path = String(getenv("HOMEDRIVE")) + "\\" + String(getenv("HOMEPATH"));
#elif defined(LINUX_OS) || defined(MAC_OS)
home_path = String(getenv("HOME"));
#else
home_path = "NULL";
#endif
return home_path;
}
@ -80,4 +81,50 @@ namespace ostd
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;
}
}

View file

@ -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;
}
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;
}
}

View file

@ -36,6 +36,7 @@ namespace ostd
//Implemented in <Time.hpp>
static void sleep(uint32_t __time, eTimeUnits __unit = eTimeUnits::Milliseconds);
static uint64_t getRunningTime_ms(void);
static String secondsToFormattedString(int32_t totalSeconds);
//Implemented in <md5.cpp>
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> listDirectoriesInDirectoryRecursive(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 bool ensureDirectory(const String& path);
static bool deleteDirectory(const String& path);
//Implemented in <ShuntingYard.cpp>
static int32_t solveIntegerExpression(const String& expr);