creat color.h i color.cpp i llevat de utils.h i utils.cpp

This commit is contained in:
2025-07-21 14:43:35 +02:00
parent 087643c71b
commit 8b7b667c60
70 changed files with 529 additions and 570 deletions

View File

@@ -2,20 +2,13 @@
#pragma once
#include <SDL3/SDL.h> // Para Uint8, SDL_FRect, SDL_FPoint, SDL_Renderer
#include <algorithm> // Para max, min
#include <array> // Para array
#include <cctype> // Para isxdigit
#include <cstdint> // Para int32_t
#include <cstdlib> // Para size_t, abs
#include <stdexcept> // Para invalid_argument
#include <string> // Para string, basic_string, stoi
#include <vector> // Para vector
#include <cstdint> // Para int32_t
#include <string> // Para string
#include <vector> // Para vector
// --- Constantes ---
constexpr int BLOCK = 8;
constexpr int TOTAL_DEMO_DATA = 2000;
constexpr size_t COLOR_CYCLE_SIZE = 6; // Mitad del ciclo espejado
// --- Estructuras y tipos ---
struct Overrides {
@@ -34,118 +27,6 @@ struct Circle {
: x(x_coord), y(y_coord), r(radius) {}
};
// Estructura para definir un color RGBA
struct Color {
private:
static constexpr Uint8 MAX_COLOR_VALUE = 255;
static constexpr Uint8 MIN_COLOR_VALUE = 0;
static constexpr Uint8 DEFAULT_ALPHA = 255;
static constexpr int DEFAULT_LIGHTEN_AMOUNT = 50;
static constexpr int DEFAULT_DARKEN_AMOUNT = 50;
static constexpr int DEFAULT_APPROACH_STEP = 1;
static constexpr size_t HEX_RGB_LENGTH = 6;
static constexpr size_t HEX_RGBA_LENGTH = 8;
static constexpr int HEX_BASE = 16;
static constexpr size_t HEX_COMPONENT_LENGTH = 2;
public:
Uint8 r, g, b, a;
constexpr Color() : r(MIN_COLOR_VALUE), g(MIN_COLOR_VALUE), b(MIN_COLOR_VALUE), a(DEFAULT_ALPHA) {}
explicit constexpr Color(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = DEFAULT_ALPHA)
: r(red), g(green), b(blue), a(alpha) {}
[[nodiscard]] constexpr auto INVERSE() const -> Color {
return Color(MAX_COLOR_VALUE - r, MAX_COLOR_VALUE - g, MAX_COLOR_VALUE - b, a);
}
[[nodiscard]] constexpr auto LIGHTEN(int amount = DEFAULT_LIGHTEN_AMOUNT) const -> Color {
return Color(
std::min(static_cast<int>(MAX_COLOR_VALUE), r + amount),
std::min(static_cast<int>(MAX_COLOR_VALUE), g + amount),
std::min(static_cast<int>(MAX_COLOR_VALUE), b + amount),
a);
}
[[nodiscard]] constexpr auto DARKEN(int amount = DEFAULT_DARKEN_AMOUNT) const -> Color {
return Color(
std::max(static_cast<int>(MIN_COLOR_VALUE), r - amount),
std::max(static_cast<int>(MIN_COLOR_VALUE), g - amount),
std::max(static_cast<int>(MIN_COLOR_VALUE), b - amount),
a);
}
// Método estático para crear Color desde string hexadecimal
static auto fromHex(const std::string &hex_str) -> Color {
std::string hex = hex_str;
// Quitar '#' si existe
if (!hex.empty() && hex[0] == '#') {
hex = hex.substr(1);
}
// Verificar longitud válida (6 para RGB o 8 para RGBA)
if (hex.length() != HEX_RGB_LENGTH && hex.length() != HEX_RGBA_LENGTH) {
throw std::invalid_argument("String hexadecimal debe tener 6 o 8 caracteres");
}
// Verificar que todos los caracteres sean hexadecimales válidos
for (char c : hex) {
if (std::isxdigit(c) == 0) {
throw std::invalid_argument("String contiene caracteres no hexadecimales");
}
}
// Convertir cada par de caracteres a valores RGB(A)
Uint8 r = static_cast<Uint8>(std::stoi(hex.substr(0, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
Uint8 g = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
Uint8 b = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH * 2, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
Uint8 a = DEFAULT_ALPHA; // Alpha por defecto
// Si tiene 8 caracteres, extraer el alpha
if (hex.length() == HEX_RGBA_LENGTH) {
a = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH * 3, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
}
return Color(r, g, b, a);
}
[[nodiscard]] constexpr auto IS_EQUAL_TO(const Color &other) const -> bool {
return r == other.r && g == other.g && b == other.b && a == other.a;
}
[[nodiscard]] constexpr auto APPROACH_TO(const Color &target, int step = DEFAULT_APPROACH_STEP) const -> Color {
auto approach_component = [step](Uint8 current, Uint8 target_val) -> Uint8 {
if (std::abs(current - target_val) <= step) {
return target_val;
}
return (current < target_val) ? current + step : current - step;
};
Uint8 new_r = approach_component(r, target.r);
Uint8 new_g = approach_component(g, target.g);
Uint8 new_b = approach_component(b, target.b);
Uint8 new_a = approach_component(a, target.a);
return Color(new_r, new_g, new_b, new_a);
}
};
// Estructura para definir un color HSV
struct HSV {
float h, s, v;
};
// Estructura para definir el ciclo de color
enum class ColorCycleStyle {
SUBTLE_PULSE, // Variación leve en brillo (por defecto)
HUE_WAVE, // Variación suave en tono (sin verde)
VIBRANT, // Cambios agresivos en tono y brillo
DARKEN_GLOW, // Oscurece hacia el centro y regresa
LIGHT_FLASH // Ilumina hacia el centro y regresa
};
// Posiciones de las notificaciones
enum class NotifyPosition {
TOP,
@@ -193,28 +74,8 @@ struct Zone {
float third_quarter_y; // Anclaje al 75% del eje Y
};
// --- Alias ---
using ColorCycle = std::array<Color, 2 * COLOR_CYCLE_SIZE>;
// --- Funciones utilitarias ---
// Colores
constexpr Color NO_TEXT_COLOR = Color(0XFF, 0XFF, 0XFF);
constexpr Color SHADOW_TEXT_COLOR = Color(0X43, 0X43, 0X4F);
constexpr Color TITLE_SHADOW_TEXT_COLOR = Color(0x14, 0x87, 0xc4);
constexpr Color FLASH_COLOR = Color(0XFF, 0XFF, 0XFF);
constexpr Color BLUE_SKY_COLOR = Color(0X02, 0X88, 0XD1);
constexpr Color PINK_SKY_COLOR = Color(0XFF, 0X6B, 0X97);
constexpr Color GREEN_SKY_COLOR = Color(0X00, 0X79, 0X6B);
// Colores y gráficos
auto getColorLikeKnightRider(const std::vector<Color> &colors, int counter) -> Color;
constexpr auto rgbToHsv(Color color) -> HSV;
constexpr auto hsvToRgb(HSV hsv) -> Color;
auto generateMirroredCycle(Color base, ColorCycleStyle style = ColorCycleStyle::SUBTLE_PULSE) -> ColorCycle;
// Colisiones y geometría
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
auto checkCollision(const Circle &a, const Circle &b) -> bool;
@@ -232,10 +93,6 @@ auto trim(const std::string &str) -> std::string;
// Dibujo
void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int32_t radius);
// Manipulación de color
auto lightenColor(const Color &color, int amount) -> Color;
auto darkenColor(const Color &color, int amount) -> Color;
// Funciones de suavizado (easing)
auto easeOutQuint(double time) -> double;
auto easeInQuint(double time) -> double;