我正在尝试 TSL 色彩空间(Tint Saturation Luminance)。阅读此维基页面
我尝试编写两个简单的 rgb2tsl 和 tsl2rgb 例程:
/// input: r,g,b in [0..1]
/// output: t,s,l in [0..1]
void RGB2TSL( float r, float g, float b, float &t, float &s, float &l )
{
float rn, gn;
if ( r+g+b == 0.0f )
{
t = s = l = 0.0f;
return ;
}
rn = r/(r+g+b);
gn = g/(r+g+b);
float r1, g1;
r1 = rn-1.0f/3.0f;
g1 = gn-1.0f/3.0f;
l = 0.299*r+0.587*g+0.114*b;
if ( g1 > 0 )
t = (1.0f/(2.0f*PI))*atan2f(r1, g1) + 1.0f/4.0f;
else if ( g1 < 0 )
t = (1.0f/(2.0f*PI))*atan2f(r1, g1) + 3.0f/4.0f;
else
t = 0;
s = sqrtf(9.0f/5.0f*(r1*r1+g1*g1));
}
/// input: t,s,l in [0..1]
/// output: r,g,b in [0..1]
void TSL2RGB( float t, float s, float l, float &r, float &g, float &b )
{
float r1, g1, k, x;
x = -sinf(2*PI*t);
if ( x != 0 )
x = cosf(2*PI*t)/x;
if ( t > 0.5f )
g1 = -s*sqrtf(5.0f/(9.0*(x*x+1.0f)));
else if ( t < 0.5f )
g1 = s*sqrtf(5.0f/(9.0*(x*x+1.0f)));
else
g1 = 0;
if ( t == 0 )
r1 = (sqrtf(5.0f)/3.0f)*s;
else
r1 = x*g1+1.0f/3.0f;
k = 1.0f/(0.185*r1+0.473*g1+0.114);
r = k*r1;
g = k*g1;
b = k*(1-r1-g1);
}
请不要考虑代码质量,只关注我无法找到的错误:当我将 rgb 三元组转换为 tsl 三元组并且不做任何更改时,我将其转换回 r、g、b ,我没有得到原始图像,但是以任何可能的方式确实不同且错误!