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

@@ -1,36 +1,21 @@
#define _USE_MATH_DEFINES
#include "utils.h"
#include <SDL3/SDL.h> // Para SDL_RenderPoint, SDL_FRect, SDL_CloseIO, SDL_I...
#include <algorithm> // Para min, clamp, find_if_not, find, transform
#include <SDL3/SDL.h> // Para SDL_RenderPoint, SDL_FRect, SDL_CloseIO, SDL_IOFromFile, SDL_LogCategory, SDL_LogError, SDL_LogInfo, SDL_ReadIO, SDL_FPoint, SDL_Renderer
#include <algorithm> // Para clamp, find_if_not, find, transform
#include <cctype> // Para tolower, isspace
#include <cmath> // Para pow, sinf, fmaxf, fminf, M_PI, fmodf, roundf, sin
#include <compare> // Para operator<
#include <cmath> // Para pow, sin, M_PI, cos
#include <compare> // Para operator<, __synth3way_t
#include <cstdlib> // Para size_t
#include <filesystem> // Para path
#include <stdexcept> // Para runtime_error
#include <string> // Para basic_string, allocator, string, char_traits
#include <string> // Para basic_string, allocator, string, operator==, operator+, char_traits
#include "lang.h" // Para getText
#include "lang.h" // Para getText
// Variables
Overrides overrides = Overrides();
// Obtiene un color del vector de colores imitando al Coche Fantástico
auto getColorLikeKnightRider(const std::vector<Color> &colors, int counter) -> Color {
int cycle_length = colors.size() * 2 - 2;
size_t n = counter % cycle_length;
size_t index;
if (n < colors.size()) {
index = n; // Avanza: 0,1,2,3
} else {
index = 2 * (colors.size() - 1) - n; // Retrocede: 2,1
}
return colors[index];
}
// Calcula el cuadrado de la distancia entre dos puntos
auto distanceSquared(int x1, int y1, int x2, int y2) -> double {
const int DELTA_X = x2 - x1;
@@ -153,24 +138,6 @@ void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int3
}
}
// Aclara el color
auto lightenColor(const Color &color, int amount) -> Color {
Color new_color;
new_color.r = std::min(255, color.r + amount);
new_color.g = std::min(255, color.g + amount);
new_color.b = std::min(255, color.b + amount);
return new_color;
}
// Oscurece el color
auto darkenColor(const Color &color, int amount) -> Color {
Color new_color;
new_color.r = std::min(255, color.r - +amount);
new_color.g = std::min(255, color.g - +amount);
new_color.b = std::min(255, color.b - +amount);
return new_color;
}
// Quita los espacioes en un string
auto trim(const std::string &str) -> std::string {
auto start = std::find_if_not(str.begin(), str.end(), ::isspace);
@@ -369,128 +336,4 @@ auto getFileName(const std::string &path) -> std::string {
auto getPath(const std::string &full_path) -> std::string {
std::filesystem::path path(full_path);
return path.parent_path().string();
}
constexpr auto rgbToHsv(Color color) -> HSV {
float r = color.r / 255.0F;
float g = color.g / 255.0F;
float b = color.b / 255.0F;
float max = fmaxf(fmaxf(r, g), b);
float min = fminf(fminf(r, g), b);
float delta = max - min;
float h = 0.0F;
if (delta > 0.00001F) {
if (max == r) {
h = fmodf((g - b) / delta, 6.0F);
} else if (max == g) {
h = ((b - r) / delta) + 2.0F;
} else {
h = ((r - g) / delta) + 4.0F;
}
h *= 60.0F;
if (h < 0.0F) {
h += 360.0F;
}
}
float s = (max <= 0.0F) ? 0.0F : delta / max;
float v = max;
return {h, s, v};
}
constexpr auto hsvToRgb(HSV hsv) -> Color {
float c = hsv.v * hsv.s;
float x = c * (1 - std::abs(std::fmod(hsv.h / 60.0F, 2) - 1));
float m = hsv.v - c;
float r = 0;
float g = 0;
float b = 0;
if (hsv.h < 60) {
r = c;
g = x;
b = 0;
} else if (hsv.h < 120) {
r = x;
g = c;
b = 0;
} else if (hsv.h < 180) {
r = 0;
g = c;
b = x;
} else if (hsv.h < 240) {
r = 0;
g = x;
b = c;
} else if (hsv.h < 300) {
r = x;
g = 0;
b = c;
} else {
r = c;
g = 0;
b = x;
}
return Color(
static_cast<uint8_t>(roundf((r + m) * 255)),
static_cast<uint8_t>(roundf((g + m) * 255)),
static_cast<uint8_t>(roundf((b + m) * 255)));
}
auto generateMirroredCycle(Color base, ColorCycleStyle style) -> ColorCycle {
ColorCycle result{};
HSV base_hsv = rgbToHsv(base);
for (size_t i = 0; i < COLOR_CYCLE_SIZE; ++i) {
float t = static_cast<float>(i) / (COLOR_CYCLE_SIZE - 1); // 0 → 1
float hue_shift = 0.0F;
float sat_shift = 0.0F;
float val_shift = 0.0F;
switch (style) {
case ColorCycleStyle::SUBTLE_PULSE:
// Solo brillo suave
val_shift = 0.07F * sinf(t * M_PI);
break;
case ColorCycleStyle::HUE_WAVE:
// Oscilación leve de tono
hue_shift = 15.0F * (t - 0.5F) * 2.0F;
val_shift = 0.05F * sinf(t * M_PI);
break;
case ColorCycleStyle::VIBRANT:
// Cambios fuertes en tono y brillo
hue_shift = 35.0F * sinf(t * M_PI);
val_shift = 0.2F * sinf(t * M_PI);
sat_shift = -0.2F * sinf(t * M_PI);
break;
case ColorCycleStyle::DARKEN_GLOW:
// Se oscurece al centro
val_shift = -0.15F * sinf(t * M_PI);
break;
case ColorCycleStyle::LIGHT_FLASH:
// Se ilumina al centro
val_shift = 0.25F * sinf(t * M_PI);
break;
}
HSV adjusted = {
fmodf(base_hsv.h + hue_shift + 360.0F, 360.0F),
fminf(1.0F, fmaxf(0.0F, base_hsv.s + sat_shift)),
fminf(1.0F, fmaxf(0.0F, base_hsv.v + val_shift))};
Color c = hsvToRgb(adjusted);
result[i] = c;
result[2 * COLOR_CYCLE_SIZE - 1 - i] = c; // espejo
}
return result;
}
}