Skip to content

Commit

Permalink
optimize it + use linear algebra in color utils :3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBearodactyl committed Nov 14, 2024
1 parent 9fc485c commit 548395f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 113 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
# set(GEODE_BINDINGS_REPO_PATH "D:\\Projects\\gay-wave-trail\\bindings\\")

project(gay-wave-trail VERSION 1.0.0)

if (CMAKE_GENERATOR MATCHES "Visual Studio 17 2022")
set(CMAKE_C_COMPILER "C:\\Users\\thebe\\scoop\\apps\\llvm\\current\\bin\\clang-cl.exe")
set(CMAKE_CXX_COMPILER "C:\\Users\\thebe\\scoop\\apps\\llvm\\current\\bin\\clang-cl.exe")
endif()

file(
GLOB_RECURSE
SOURCES
Expand All @@ -21,6 +25,14 @@ file(
add_library(${PROJECT_NAME} SHARED ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC include src)

target_compile_options(${PROJECT_NAME} PRIVATE
/O2
/GL
/fp:fast
/Qpar
/GF
)

if(NOT DEFINED ENV{GEODE_SDK})
message(
FATAL_ERROR
Expand Down
3 changes: 3 additions & 0 deletions cpm-package-lock.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CPM Package Lock
# This file should be committed to version control

2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"android": "2.2074",
"mac": "2.2074"
},
"version": "v2.0.3",
"version": "v2.0.4",
"id": "the_bearodactyl.gay-wave-trail",
"name": "Gay Wave Trail",
"developer": "The Bearodactyl",
Expand Down
198 changes: 87 additions & 111 deletions src/utils/color_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,129 +1,105 @@
#include "color_utils.hpp"
#include <cmath>
#include <array>

namespace {
// Pre-computed rotation matrices for HSV color space conversion
constexpr std::array<std::array<float, 3>, 6> HSV_RGB_MATRICES = {{
{{1.0f, 1.0f, 0.0f}}, // 0° to 60°
{{0.0f, 1.0f, 0.0f}}, // 60° to 120°
{{0.0f, 1.0f, 1.0f}}, // 120° to 180°
{{0.0f, 0.0f, 1.0f}}, // 180° to 240°
{{1.0f, 0.0f, 1.0f}}, // 240° to 300°
{{1.0f, 0.0f, 0.0f}} // 300° to 360°
}};
}

void ColorUtils::hsv_to_rgb(float &fR, float &fG, float &fB, float &fH, float &fS, float &fV) {
float c = fV * fS;
float x = static_cast<float>(static_cast<double>(c) * (1 - std::abs(std::fmod(fH / 60.0f, 2) - 1)));
float m = fV - c;

fR = (fH < 60.0f)
? c
: (fH < 120.0f)
? x
: (fH < 180.0f)
? 0
: (fH < 240.0f)
? 0
: (fH < 300.0f)
? x
: c;
fG = (fH < 60.0f)
? x
: (fH < 120.0f)
? c
: (fH < 180.0f)
? c
: (fH < 240.0f)
? x
: (fH < 300.0f)
? 0
: 0;
fB = (fH < 60.0f)
? 0
: (fH < 120.0f)
? 0
: (fH < 180.0f)
? x
: (fH < 240.0f)
? c
: (fH < 300.0f)
? c
: x;

fR += m;
fG += m;
fB += m;
const float c = fV * fS;
const float x = c * (1.0f - std::abs(std::fmod(fH / 60.0f, 2.0f) - 1.0f));
const float m = fV - c;

// Determine which sector of the HSV color wheel we're in
const int sector = static_cast<int>(fH / 60.0f);

// Use pre-computed matrices for the transformation
const auto& matrix = HSV_RGB_MATRICES[sector];

// Matrix multiplication with the color components
fR = matrix[0] * (sector % 2 == 0 ? c : x) + m;
fG = matrix[1] * (sector % 2 == 0 ? x : c) + m;
fB = matrix[2] * (sector % 2 == 0 ? 0.0f : (sector % 3 == 0 ? c : x)) + m;
}

float ColorUtils::owo = 0;

void ColorUtils::hex_to_hsv(uint32_t hex, float &h, float &s, float &v) {
float r = ((hex >> 16) & 0xFF) / 255.0f;
float g = ((hex >> 8) & 0xFF) / 255.0f;
float b = (hex & 0xFF) / 255.0f;

float max = std::max(std::max(r, g), b);
float min = std::min(std::min(r, g), b);
v = max;

float delta = max - min;
if (max != 0.0f) {
s = delta / max;
} else {
s = 0.0f;
h = 0.0f;
return;
}

if (r == max) {
h = (g - b) / delta;
} else if (g == max) {
h = 2.0f + (b - r) / delta;
} else {
h = 4.0f + (r - g) / delta;
}

h *= 60.0f;
if (h < 0.0f) {
h += 360.0f;
}
// Convert hex to normalized RGB using vector operations
const float r = static_cast<float>((hex >> 16) & 0xFF) / 255.0f;
const float g = static_cast<float>((hex >> 8) & 0xFF) / 255.0f;
const float b = static_cast<float>(hex & 0xFF) / 255.0f;

const float max = std::max({r, g, b});
const float min = std::min({r, g, b});
const float delta = max - min;

v = max;
s = (max != 0.0f) ? (delta / max) : 0.0f;

if (delta == 0.0f) {
h = 0.0f;
return;
}

// Use dot product for hue calculation
const float rgb_matrix[3] = {
(g - b) / delta,
2.0f + (b - r) / delta,
4.0f + (r - g) / delta
};

const int max_component = (r == max) ? 0 : (g == max) ? 1 : 2;
h = rgb_matrix[max_component] * 60.0f;

if (h < 0.0f) {
h += 360.0f;
}
}

cocos2d::ccColor3B ColorUtils::hex_to_rgb(const std::string &hex) {
if (! is_valid_hex_code(hex)) {
return cocos2d::ccColor3B{0, 0, 0};
}

std::string clean_hex = hex;
if (clean_hex[0] == '#') {
clean_hex = clean_hex.substr(1);
}

GLubyte r = hex_pair_to_dec(clean_hex.substr(0, 2));
GLubyte g = hex_pair_to_dec(clean_hex.substr(2, 2));
GLubyte b = hex_pair_to_dec(clean_hex.substr(4, 2));

return cocos2d::ccColor3B{r, g, b};
if (!is_valid_hex_code(hex)) {
return {0, 0, 0};
}

const std::string& clean_hex = (hex[0] == '#') ? hex.substr(1) : hex;

// Process all components at once using bit operations
const uint32_t value = std::stoul(clean_hex, nullptr, 16);
return {
static_cast<GLubyte>((value >> 16) & 0xFF),
static_cast<GLubyte>((value >> 8) & 0xFF),
static_cast<GLubyte>(value & 0xFF)
};
}

GLubyte ColorUtils::hex_pair_to_dec(const std::string &hex_pair) {
int val = 0;

for (char c: hex_pair) {
val = val * 16;

if (c >= '0' && c <= '9') {
val += c - '0';
} else {
val += std::tolower(c) - 'a' + 10;
}
}

return static_cast<GLubyte>(val);
// Use bit shifting for faster conversion
return static_cast<GLubyte>(std::stoul(hex_pair, nullptr, 16));
}

bool ColorUtils::is_valid_hex_code(const std::string &hex_code) {
if (hex_code.empty())
return false;

size_t start_pos = (hex_code[0] == '#') ? 1 : 0;

if (hex_code.length() != start_pos + 6)
return false;

return std::all_of(hex_code.begin() + start_pos, hex_code.end(),
[](char c) {
c = std::tolower(c);
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
});
}
if (hex_code.empty()) {
return false;
}

const size_t start_pos = (hex_code[0] == '#') ? 1 : 0;
if (hex_code.length() != start_pos + 6) {
return false;
}

return std::all_of(hex_code.begin() + start_pos, hex_code.end(),
[](char c) {
c = std::tolower(c);
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
});
}

0 comments on commit 548395f

Please sign in to comment.