-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize it + use linear algebra in color utils :3
- Loading branch information
1 parent
9fc485c
commit 548395f
Showing
4 changed files
with
104 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |