Skip to content

Commit

Permalink
refactoring hell :3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBearodactyl authored Apr 28, 2024
1 parent 99d536a commit 4c16095
Showing 1 changed file with 22 additions and 58 deletions.
80 changes: 22 additions & 58 deletions src/trail_customization/rainbow_trail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,33 @@

#include <cmath>

void RainbowTrail::hsv_to_rgb(float &fR, float &fG, float &fB, float &fH, float &fS, float &fV) {
float fC = fV * fS; // Chroma
float fHPrime = fmod(fH / 60.0, 6);
float fX = fC * (1 - fabs(fmod(fHPrime, 2) - 1));
float fM = fV - fC;

int hue_segment = static_cast<int>(fHPrime);
switch (hue_segment) {
case 0:
fR = fC;
fG = fX;
fB = 0;
break;
case 1:
fR = fX;
fG = fC;
fB = 0;
break;
case 2:
fR = 0;
fG = fC;
fB = fX;
break;
case 3:
fR = 0;
fG = fX;
fB = fC;
break;
case 4:
fR = fX;
fG = 0;
fB = fC;
break;
case 5:
fR = fC;
fG = 0;
fB = fX;
break;
default:
fR = 0;
fG = 0;
fB = 0;
break;
}

fR += fM;
fG += fM;
fB += fM;
// Table-based HSV to RGB conversion (precalculate for efficiency)
const float kHueSegmentValues[6][3] = {
{1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}, {1, 0, 1}
};

void RainbowTrail::hsv_to_rgb(float &fR, float &fG, float &fB, float fH, float fS, float fV) {
float fC = fV * fS;
int hIndex = static_cast<int>(fmod(fH / 60.0, 6));
const float* rgb = kHueSegmentValues[hIndex];

fR = rgb[0] * fC + fV - fC;
fG = rgb[1] * fC + fV - fC;
fB = rgb[2] * fC + fV - fC;
}

float RainbowTrail::g = 0;
float RainbowTrail::g = 0; // Assuming 'g' is used for animation

cocos2d::_ccColor3B RainbowTrail::get_rainbow(float offset, float saturation) {
float R, G, B;

float hue = fmod(g + offset, 360);
float sat = saturation / 100.0;
float vc = 1;
hsv_to_rgb(R, G, B, hue, sat, vc);

cocos2d::_ccColor3B out;
out.r = R * 255;
out.g = G * 255;
out.b = B * 255;
return out;
float R, G, B;
hsv_to_rgb(R, G, B, hue, sat, 1); // Constant brightness

return cocos2d::_ccColor3B{
static_cast<uint8_t>(R * 255),
static_cast<uint8_t>(G * 255),
static_cast<uint8_t>(B * 255)
};
}

0 comments on commit 4c16095

Please sign in to comment.