mirror of
https://github.com/hyprwm/hyprlock.git
synced 2024-11-17 07:15:57 +01:00
config: make configStringToInt support rgb(a) with decimal values (#558)
This commit is contained in:
parent
2775ab2868
commit
d159e14e5b
1 changed files with 57 additions and 19 deletions
|
@ -1,4 +1,6 @@
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
#include "MiscFunctions.hpp"
|
#include "MiscFunctions.hpp"
|
||||||
#include "../helpers/Log.hpp"
|
#include "../helpers/Log.hpp"
|
||||||
#include <hyprutils/string/String.hpp>
|
#include <hyprutils/string/String.hpp>
|
||||||
|
@ -22,41 +24,77 @@ std::string absolutePath(const std::string& rawpath, const std::string& currentD
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t configStringToInt(const std::string& VALUE) {
|
int64_t configStringToInt(const std::string& VALUE) {
|
||||||
|
auto parseHex = [](const std::string& value) -> int64_t {
|
||||||
|
try {
|
||||||
|
size_t position;
|
||||||
|
auto result = stoll(value, &position, 16);
|
||||||
|
if (position == value.size())
|
||||||
|
return result;
|
||||||
|
} catch (const std::exception&) {}
|
||||||
|
throw std::invalid_argument("invalid hex " + value);
|
||||||
|
};
|
||||||
if (VALUE.starts_with("0x")) {
|
if (VALUE.starts_with("0x")) {
|
||||||
// Values with 0x are hex
|
// Values with 0x are hex
|
||||||
const auto VALUEWITHOUTHEX = VALUE.substr(2);
|
return parseHex(VALUE);
|
||||||
return stol(VALUEWITHOUTHEX, nullptr, 16);
|
|
||||||
} else if (VALUE.starts_with("rgba(") && VALUE.ends_with(')')) {
|
} else if (VALUE.starts_with("rgba(") && VALUE.ends_with(')')) {
|
||||||
const auto VALUEWITHOUTFUNC = VALUE.substr(5, VALUE.length() - 6);
|
const auto VALUEWITHOUTFUNC = trim(VALUE.substr(5, VALUE.length() - 6));
|
||||||
|
|
||||||
if (trim(VALUEWITHOUTFUNC).length() != 8) {
|
// try doing it the comma way first
|
||||||
Debug::log(WARN, "invalid length {} for rgba", VALUEWITHOUTFUNC.length());
|
if (std::count(VALUEWITHOUTFUNC.begin(), VALUEWITHOUTFUNC.end(), ',') == 3) {
|
||||||
throw std::invalid_argument("rgba() expects length of 8 characters (4 bytes)");
|
// cool
|
||||||
|
std::string rolling = VALUEWITHOUTFUNC;
|
||||||
|
auto r = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
|
auto g = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
|
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
|
uint8_t a = 0;
|
||||||
|
try {
|
||||||
|
a = std::round(std::stof(trim(rolling.substr(0, rolling.find(',')))) * 255.f);
|
||||||
|
} catch (std::exception& e) { throw std::invalid_argument("failed parsing " + VALUEWITHOUTFUNC); }
|
||||||
|
|
||||||
|
return a * (Hyprlang::INT)0x1000000 + r * (Hyprlang::INT)0x10000 + g * (Hyprlang::INT)0x100 + b;
|
||||||
|
} else if (VALUEWITHOUTFUNC.length() == 8) {
|
||||||
|
const auto RGBA = parseHex(VALUEWITHOUTFUNC);
|
||||||
|
// now we need to RGBA -> ARGB. The config holds ARGB only.
|
||||||
|
return (RGBA >> 8) + 0x1000000 * (RGBA & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto RGBA = std::stol(VALUEWITHOUTFUNC, nullptr, 16);
|
throw std::invalid_argument("rgba() expects length of 8 characters (4 bytes) or 4 comma separated values");
|
||||||
|
|
||||||
// now we need to RGBA -> ARGB. The config holds ARGB only.
|
|
||||||
return (RGBA >> 8) + 0x1000000 * (RGBA & 0xFF);
|
|
||||||
} else if (VALUE.starts_with("rgb(") && VALUE.ends_with(')')) {
|
} else if (VALUE.starts_with("rgb(") && VALUE.ends_with(')')) {
|
||||||
const auto VALUEWITHOUTFUNC = VALUE.substr(4, VALUE.length() - 5);
|
const auto VALUEWITHOUTFUNC = trim(VALUE.substr(4, VALUE.length() - 5));
|
||||||
|
|
||||||
if (trim(VALUEWITHOUTFUNC).length() != 6) {
|
// try doing it the comma way first
|
||||||
Debug::log(WARN, "invalid length {} for rgb", VALUEWITHOUTFUNC.length());
|
if (std::count(VALUEWITHOUTFUNC.begin(), VALUEWITHOUTFUNC.end(), ',') == 2) {
|
||||||
throw std::invalid_argument("rgb() expects length of 6 characters (3 bytes)");
|
// cool
|
||||||
|
std::string rolling = VALUEWITHOUTFUNC;
|
||||||
|
auto r = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
|
auto g = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
rolling = rolling.substr(rolling.find(',') + 1);
|
||||||
|
auto b = configStringToInt(trim(rolling.substr(0, rolling.find(','))));
|
||||||
|
|
||||||
|
return (Hyprlang::INT)0xFF000000 + r * (Hyprlang::INT)0x10000 + g * (Hyprlang::INT)0x100 + b;
|
||||||
|
} else if (VALUEWITHOUTFUNC.length() == 6) {
|
||||||
|
return parseHex(VALUEWITHOUTFUNC) + 0xFF000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto RGB = std::stol(VALUEWITHOUTFUNC, nullptr, 16);
|
throw std::invalid_argument("rgb() expects length of 6 characters (3 bytes) or 3 comma separated values");
|
||||||
|
|
||||||
return RGB + 0xFF000000; // 0xFF for opaque
|
|
||||||
} else if (VALUE.starts_with("true") || VALUE.starts_with("on") || VALUE.starts_with("yes")) {
|
} else if (VALUE.starts_with("true") || VALUE.starts_with("on") || VALUE.starts_with("yes")) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (VALUE.starts_with("false") || VALUE.starts_with("off") || VALUE.starts_with("no")) {
|
} else if (VALUE.starts_with("false") || VALUE.starts_with("off") || VALUE.starts_with("no")) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VALUE.empty() || !isNumber(VALUE))
|
if (VALUE.empty() || !isNumber(VALUE, false))
|
||||||
return 0;
|
throw std::invalid_argument("cannot parse \"" + VALUE + "\" as an int.");
|
||||||
|
|
||||||
return std::stoll(VALUE);
|
try {
|
||||||
|
const auto RES = std::stoll(VALUE);
|
||||||
|
return RES;
|
||||||
|
} catch (std::exception& e) { throw std::invalid_argument(std::string{"stoll threw: "} + e.what()); }
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue