Color: afegit static Color fromHex(const std::string& hexStr)

This commit is contained in:
2025-06-26 11:42:37 +02:00
parent 1ed09226e0
commit 78efcbccb7
6 changed files with 50 additions and 44 deletions

View File

@@ -8,6 +8,7 @@
#include <string> // Para string
#include <vector> // Para vector
#include <array> // Para array
#include <stdexcept>
// --- Constantes ---
constexpr int BLOCK = 8;
@@ -19,10 +20,9 @@ struct Overrides
{
std::string param_file; // Fichero de parametros a utilizar
bool clear_hi_score_table; // Reinicia la tabla de records
bool set_v_sync; // Establece el vsync
Overrides()
: param_file(""), clear_hi_score_table(false), set_v_sync(false) {}
: param_file(""), clear_hi_score_table(false) {}
};
extern Overrides overrides;
@@ -57,6 +57,36 @@ struct Color
std::max(0, g - amount),
std::max(0, b - amount));
}
// Método estático para crear Color desde string hexadecimal
static Color fromHex(const std::string& hexStr)
{
std::string hex = hexStr;
// Quitar '#' si existe
if (!hex.empty() && hex[0] == '#') {
hex = hex.substr(1);
}
// Verificar longitud válida (6 caracteres para RGB)
if (hex.length() != 6) {
throw std::invalid_argument("String hexadecimal debe tener 6 caracteres");
}
// Verificar que todos los caracteres sean hexadecimales válidos
for (char c : hex) {
if (!std::isxdigit(c)) {
throw std::invalid_argument("String contiene caracteres no hexadecimales");
}
}
// Convertir cada par de caracteres a valores RGB
Uint8 r = static_cast<Uint8>(std::stoi(hex.substr(0, 2), nullptr, 16));
Uint8 g = static_cast<Uint8>(std::stoi(hex.substr(2, 2), nullptr, 16));
Uint8 b = static_cast<Uint8>(std::stoi(hex.substr(4, 2), nullptr, 16));
return Color(r, g, b);
}
};
// Estructura para definir un color HSV