feat(client): add support for 8-bit hex color codes in CSS
This allows users to use #RRGGBBAA format in their style.css. The client now detects 8-bit hex codes, transforms them into GTK-compatible rgba() syntax, and loads the modified data into the CSS provider. - Added utility to detect 8-bit hex patterns. - Added transformation logic to convert hex alpha to decimal. - Intercepted CSS loading in Client::setupCss to handle the conversion.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
bool has_8bit_hex(std::string file_path) {
|
||||
std::ifstream f(file_path, std::ios::in | std::ios::binary);
|
||||
const auto size = fs::file_size(file_path);
|
||||
std::string result(size, '\0');
|
||||
f.read(result.data(), size);
|
||||
std::regex pattern(
|
||||
R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))");
|
||||
return std::regex_search(result, pattern);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
namespace fs = std::filesystem;
|
||||
std::string transform_8bit_to_hex(std::string file_path) {
|
||||
std::ifstream f(file_path, std::ios::in | std::ios::binary);
|
||||
const auto size = fs::file_size(file_path);
|
||||
std::string result(size, '\0');
|
||||
f.read(result.data(), size);
|
||||
|
||||
std::regex pattern(
|
||||
R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))");
|
||||
std::string final_output;
|
||||
|
||||
auto it = std::sregex_iterator(result.begin(), result.end(), pattern);
|
||||
auto eof = std::sregex_iterator();
|
||||
std::smatch match = *it;
|
||||
|
||||
while (it != eof) {
|
||||
|
||||
final_output += match.prefix().str();
|
||||
|
||||
int r = stoi(match[1].str(), nullptr, 16);
|
||||
int g = stoi(match[2].str(), nullptr, 16);
|
||||
int b = stoi(match[3].str(), nullptr, 16);
|
||||
double a = (stoi(match[4].str(), nullptr, 16) / 255.0);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed
|
||||
<< std::setprecision(2) << a << ")";
|
||||
final_output += ss.str();
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
final_output += match.suffix().str();
|
||||
|
||||
return final_output;
|
||||
}
|
||||
Reference in New Issue
Block a user