improve accuracy

This commit is contained in:
Misha 2023-06-27 18:35:58 +03:00
parent 4c2e1ca95c
commit 78b0e8dd62
4 changed files with 33 additions and 35 deletions

View file

@ -33,22 +33,20 @@ void LegoBackgroundColor::SetColorString(const char *colorString)
char *colorStringSplit = strtok(colorStringCopy, Delimiter); char *colorStringSplit = strtok(colorStringCopy, Delimiter);
if (!strcmp(colorStringSplit, set)) if (!strcmp(colorStringSplit, set))
{ {
//TODO: the names of these variables are incorrect as I can't figure out what color format ConvertColor() uses char *hue = strtok(0, Delimiter);
char *blue = strtok(0, Delimiter); if (hue)
if (blue) h = atoi(hue) * 0.01;
b = atoi(blue) * 0.01; char *sat = strtok(0, Delimiter);
char *green = strtok(0, Delimiter); if (sat)
if (green) s = atoi(sat) * 0.01;
g = atoi(green) * 0.01; char *val = strtok(0, Delimiter);
char *red = strtok(0, Delimiter); if (val)
if (red) v = atoi(val) * 0.01;
r = atoi(red) * 0.01;
} }
else if (!strcmp(colorStringSplit, reset)) else if (!strcmp(colorStringSplit, reset))
{ {
// reset it // reset it
ConvertHSVToRGB(this->h, this->s, this->v, &converted_r, &converted_g, &converted_b);
ConvertColor(this->b, this->g, this->r, &converted_r, &converted_g, &converted_b);
videomanager->SetSkyColor(converted_r, converted_g, converted_b); videomanager->SetSkyColor(converted_r, converted_g, converted_b);
} }
free(colorStringCopy); free(colorStringCopy);

View file

@ -1,65 +1,65 @@
#include "legoutil.h" #include "legoutil.h"
// OFFSET: LEGO1 0x1003eae0 // OFFSET: LEGO1 0x1003eae0
void ConvertColor(float h, float s, float v, float *r_out, float *b_out, float *g_out) void ConvertHSVToRGB(float h, float s, float v, float *r_out, float *b_out, float *g_out)
{ {
double calc; // st7 double calc;
double v11; double p;
int hue_index; long hue_index;
double v9; double v9;
double v12; double v12;
double v13; double v13;
if (s >= 0.5) if (s > 0.5f)
calc = (1.0 - v) * s + v; calc = ((1.0f - v) * s + v); //
else else
calc = (v + 1.0) * s; calc = (v + 1.0) * s;
if (calc <= 0.0) if (calc <= 0.0)
{ {
*g_out = 0.0; *g_out = 0.0f;
*b_out = 0.0; *b_out = 0.0f;
*r_out = 0.0; *r_out = 0.0f;
return; return;
} }
v11 = s * 2.0 - calc; p = s * 2.0 - calc;
hue_index = h * 6.0; hue_index = h * 6.0;
v9 = (h * 6.0 - (float)hue_index) * ((calc - v11) / calc) * calc; v9 = (h * 6.0 - (float)hue_index) * ((calc - p) / calc) * calc;
v12 = v11 + v9; v12 = p + v9;
v13 = calc - v9; v13 = calc - v9;
switch (hue_index) switch (hue_index)
{ {
case 0: case 0:
*r_out = calc; *r_out = calc;
*b_out = v12; *b_out = v12;
*g_out = v11; *g_out = p;
break; break;
case 1: case 1:
*r_out = v13; *r_out = v13;
*b_out = calc; *b_out = calc;
*g_out = v11; *g_out = p;
break; break;
case 2: case 2:
*r_out = v11; *r_out = p;
*b_out = calc; *b_out = calc;
*g_out = v12; *g_out = v12;
break; break;
case 3: case 3:
*r_out = v11; *r_out = p;
*b_out = v13; *b_out = v13;
*g_out = calc; *g_out = calc;
break; break;
case 4: case 4:
*r_out = v12; *r_out = v12;
*b_out = v11; *b_out = p;
*g_out = calc; *g_out = calc;
break; break;
case 5: case 5:
*r_out = calc; *r_out = calc;
*b_out = v11; *b_out = p;
*g_out = v13; *g_out = v13;
break; break;
case 6: case 6:
*r_out = calc; *r_out = calc;
*b_out = v11; *b_out = p;
*g_out = v13; *g_out = v13;
break; break;
default: default:

View file

@ -6,5 +6,5 @@ inline T Abs(T p_t)
{ {
return p_t < 0 ? -p_t : p_t; return p_t < 0 ? -p_t : p_t;
} }
void ConvertColor(float r, float g, float b, float* out_r, float* out_g, float* out_b); void ConvertHSVToRGB(float r, float g, float b, float* out_r, float* out_g, float* out_b);
#endif // LEGOUTIL_H #endif // LEGOUTIL_H

View file

@ -14,9 +14,9 @@ class MxBackgroundColor
protected: protected:
MxString m_name; MxString m_name;
MxString m_colorString; MxString m_colorString;
float b; float h;
float g; float s;
float r; float v;
}; };
#endif // MXBACKGROUNDCOLOR_H #endif // MXBACKGROUNDCOLOR_H