Created
April 10, 2018 15:47
-
-
Save sjhalayka/1cb743137a714b0fdded48c77421b70a to your computer and use it in GitHub Desktop.
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
float min_3d_length = FLT_MAX; | |
float max_3d_length = FLT_MIN; | |
for (set<vertex_3>::const_iterator i = final_vertices.begin(); i != final_vertices.end(); i++) | |
{ | |
if (i->length() > max_3d_length) | |
max_3d_length = i->length(); | |
if (i->length() < min_3d_length) | |
min_3d_length = i->length(); | |
} | |
float max_rainbow = 360.0f; | |
float min_rainbow = 360.0f; | |
for (set<vertex_3>::const_iterator i = final_vertices.begin(); i != final_vertices.end(); i++) | |
{ | |
RGB rgb = HSBtoRGB(static_cast<unsigned short int>( | |
max_rainbow - (((i->length() - min_3d_length) / (max_3d_length - min_3d_length)) * min_rainbow)), | |
static_cast<unsigned char>(50), | |
static_cast<unsigned char>(100)); | |
out << static_cast<float>(rgb.r) / 255.0f << " " << static_cast<float>(rgb.g) / 255.0f << " " << static_cast<float>(rgb.b) / 255.0f << " "; | |
} | |
// ... where HSBtoRGB is: | |
RGB HSBtoRGB(unsigned short int hue_degree, unsigned char sat_percent, unsigned char bri_percent) | |
{ | |
float R = 0.0f; | |
float G = 0.0f; | |
float B = 0.0f; | |
if (hue_degree > 359) | |
hue_degree = 359; | |
if (sat_percent > 100) | |
sat_percent = 100; | |
if (bri_percent > 100) | |
bri_percent = 100; | |
float hue_pos = 6.0f - ((static_cast<float>(hue_degree) / 359.0f) * 6.0f); | |
if (hue_pos >= 0.0f && hue_pos < 1.0f) | |
{ | |
R = 255.0f; | |
G = 0.0f; | |
B = 255.0f * hue_pos; | |
} | |
else if (hue_pos >= 1.0f && hue_pos < 2.0f) | |
{ | |
hue_pos -= 1.0f; | |
R = 255.0f - (255.0f * hue_pos); | |
G = 0.0f; | |
B = 255.0f; | |
} | |
else if (hue_pos >= 2.0f && hue_pos < 3.0f) | |
{ | |
hue_pos -= 2.0f; | |
R = 0.0f; | |
G = 255.0f * hue_pos; | |
B = 255.0f; | |
} | |
else if (hue_pos >= 3.0f && hue_pos < 4.0f) | |
{ | |
hue_pos -= 3.0f; | |
R = 0.0f; | |
G = 255.0f; | |
B = 255.0f - (255.0f * hue_pos); | |
} | |
else if (hue_pos >= 4.0f && hue_pos < 5.0f) | |
{ | |
hue_pos -= 4.0f; | |
R = 255.0f * hue_pos; | |
G = 255.0f; | |
B = 0.0f; | |
} | |
else | |
{ | |
hue_pos -= 5.0f; | |
R = 255.0f; | |
G = 255.0f - (255.0f * hue_pos); | |
B = 0.0f; | |
} | |
if (100 != sat_percent) | |
{ | |
if (0 == sat_percent) | |
{ | |
R = 255.0f; | |
G = 255.0f; | |
B = 255.0f; | |
} | |
else | |
{ | |
if (255.0f != R) | |
R += ((255.0f - R) / 100.0f) * (100.0f - sat_percent); | |
if (255.0f != G) | |
G += ((255.0f - G) / 100.0f) * (100.0f - sat_percent); | |
if (255.0f != B) | |
B += ((255.0f - B) / 100.0f) * (100.0f - sat_percent); | |
} | |
} | |
if (100 != bri_percent) | |
{ | |
if (0 == bri_percent) | |
{ | |
R = 0.0f; | |
G = 0.0f; | |
B = 0.0f; | |
} | |
else | |
{ | |
if (0.0f != R) | |
R *= static_cast<float>(bri_percent) / 100.0f; | |
if (0.0f != G) | |
G *= static_cast<float>(bri_percent) / 100.0f; | |
if (0.0f != B) | |
B *= static_cast<float>(bri_percent) / 100.0f; | |
} | |
} | |
if (R < 0.0f) | |
R = 0.0f; | |
else if (R > 255.0f) | |
R = 255.0f; | |
if (G < 0.0f) | |
G = 0.0f; | |
else if (G > 255.0f) | |
G = 255.0f; | |
if (B < 0.0f) | |
B = 0.0f; | |
else if (B > 255.0f) | |
B = 255.0f; | |
RGB rgb; | |
rgb.r = static_cast<unsigned char>(R); | |
rgb.g = static_cast<unsigned char>(G); | |
rgb.b = static_cast<unsigned char>(B); | |
return rgb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure where I got the HSBtoRGB code from. :(