From 231bd05f5ef5b3440e9c80c64774ef3b3c558006 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 26 Jun 2025 11:52:17 +0200 Subject: [PATCH] Color: afegit suport per al canal alpha --- source/utils.h | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/source/utils.h b/source/utils.h index ee0c06e..506082a 100644 --- a/source/utils.h +++ b/source/utils.h @@ -35,27 +35,29 @@ struct Circle : x(xCoord), y(yCoord), r(radius) {} }; -// Estructura para definir un color RGB +// Estructura para definir un color RGBA struct Color { - Uint8 r, g, b; - constexpr Color() : r(0), g(0), b(0) {} - explicit constexpr Color(Uint8 red, Uint8 green, Uint8 blue) : r(red), g(green), b(blue) {} + Uint8 r, g, b, a; + constexpr Color() : r(0), g(0), b(0), a(255) {} + explicit constexpr Color(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = 255) : r(red), g(green), b(blue), a(alpha) {} - constexpr Color getInverse() const { return Color(255 - r, 255 - g, 255 - b); } + constexpr Color getInverse() const { return Color(255 - r, 255 - g, 255 - b, a); } constexpr Color lighten(int amount = 50) const { return Color( std::min(255, r + amount), std::min(255, g + amount), - std::min(255, b + amount)); + std::min(255, b + amount), + a); } constexpr Color darken(int amount = 50) const { return Color( std::max(0, r - amount), std::max(0, g - amount), - std::max(0, b - amount)); + std::max(0, b - amount), + a); } // Método estático para crear Color desde string hexadecimal @@ -68,9 +70,9 @@ struct Color 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 longitud válida (6 para RGB o 8 para RGBA) + if (hex.length() != 6 && hex.length() != 8) { + throw std::invalid_argument("String hexadecimal debe tener 6 o 8 caracteres"); } // Verificar que todos los caracteres sean hexadecimales válidos @@ -80,12 +82,18 @@ struct Color } } - // Convertir cada par de caracteres a valores RGB + // Convertir cada par de caracteres a valores RGB(A) Uint8 r = static_cast(std::stoi(hex.substr(0, 2), nullptr, 16)); Uint8 g = static_cast(std::stoi(hex.substr(2, 2), nullptr, 16)); Uint8 b = static_cast(std::stoi(hex.substr(4, 2), nullptr, 16)); + Uint8 a = 255; // Alpha por defecto - return Color(r, g, b); + // Si tiene 8 caracteres, extraer el alpha + if (hex.length() == 8) { + a = static_cast(std::stoi(hex.substr(6, 2), nullptr, 16)); + } + + return Color(r, g, b, a); } }; @@ -182,7 +190,6 @@ constexpr Color GREEN_SKY_COLOR = Color(0X00, 0X79, 0X6B); constexpr Color SERV_MENU_TITLE_COLOR = Color(0x99, 0xFF, 0x62); constexpr Color SERV_MENU_TEXT_COLOR = Color(255, 255, 255); constexpr Color SERV_MENU_SELECTED_COLOR = Color(255, 170, 18).lighten(); -//constexpr Color SERV_MENU_SELECTED_COLOR = SERV_MENU_TITLE_COLOR.darken(); constexpr Color SERV_MENU_BG_COLOR_SHADOW = Color(0x1E, 0x1E, 0x1E); constexpr Color SERV_MENU_BG_COLOR_ALPHA = Color(0x00, 0x00, 0x00); constexpr Color DEBUG_COLOR = Color(0xFF, 0xFF, 0x00);