Created
July 2, 2012 03:11
-
-
Save davidbitton/3030772 to your computer and use it in GitHub Desktop.
Why is it always 1?!
This file contains hidden or 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
static GLKVector4 HslToRgb(float h, float s, float l, float a); | |
static GLKVector4 HslToRgb(float h, float s, float l, float a) { | |
float r = 0.0f, g = 0.0f, b = 0.0f; | |
float temp1, temp2; | |
if(l == 0.0f) | |
{ | |
r = g = b = 0.0f; | |
} | |
else | |
{ | |
if(s == 0.0f) | |
{ | |
r = g = b = l; | |
} | |
else | |
{ | |
temp2 = ((l <= 0.5f) ? l * (1.0f + s) : l + s - (l * s)); | |
temp1 = 2.0f * l - temp2; | |
float t3[] = {h + 1.0f / 3.0f, h, h - 1.0f / 3.0f}; | |
float clr[] = {0.0f, 0.0f, 0.0f}; | |
for(int i = 0;i < 3;i++) | |
{ | |
if(t3[i] < 0.0f) | |
t3[i] += 1.0f; | |
if(t3[i] > 1.0f) | |
t3[i] -= 1.0f; | |
if(6.0f * t3[i] < 1.0f) | |
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0f; | |
else if(2.0f * t3[i] < 1.0f) | |
clr[i] = temp2; | |
else if(3.0f * t3[i] < 2.0f) | |
clr[i] = (temp1 + (temp2 - temp1) * ((2.0f / 3.0f) - t3[i]) * 6.0f); | |
else | |
clr[i] = temp1; | |
} | |
r = clr[0]; | |
g = clr[1]; | |
b = clr[2]; | |
} | |
} | |
return GLKVector4Make(r, g, b, a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i adapted some code for translating HSL/HSB to RGB, however this line clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0f is always 1 when t3[i] is 0.119444