我试图将 CIE-LAB 颜色空间转换为 RGB 颜色空间。但是有一个错误。
- 输入 LAB 值 = (46.41,-39.24,33.51)
- 收到的结果 XYZ 值 =(-2,641482,15,57358,-5,368798)
- 收到的结果 RGB 值 = (-791,4557,135,8615,-271,5485)
XYZ 值应为 (9.22,15.58,5.54) RGB 值应为 (50,125,50)
我从http://colorizer.org/检查了这些值
我在哪里做错了?
如果您检查以下代码并回答我。我会很高兴的。谢谢。
我将 RGB 转换为 XYZ 并将 XYZ 转换为 LAB 颜色空间转换。您可以使用以下链接进行检查。
public static Vector4 LabToXYZ(Vector4 color)
{
float[] xyz = new float[3];
float[] col = new float[] { color[0], color[1], color[2], color[3]};
xyz[1] = (col[0] + 16.0f) / 116.0f;
xyz[0] = (col[1] / 500.0f) + xyz[0];
xyz[2] = xyz[0] - (col[2] / 200.0f);
for (int i = 0; i < 3; i++)
{
float pow = xyz[i] * xyz[i] * xyz[i];
if (pow > .008856f)
{
xyz[i] = pow;
}
else
{
xyz[i] = (xyz[i]- 16.0f / 116.0f) / 7.787f;
}
}
xyz[0] = xyz[0] * (95.047f);
xyz[1] = xyz[1] * (100.0f);
xyz[2] = xyz[2] * (108.883f);
return new Vector4(xyz[0], xyz[1], xyz[2], color[3]);
}
public static Vector4 XYZToRGB(Vector4 color)
{
float[] rgb = new float[3];
float[] xyz = new float[3];
float[] col = new float[] { color[0], color[1], color[2] };
for (int i = 0; i < 3; i++)
{
xyz[i] = col[i] / 100.0f;
}
rgb[0] = (xyz[0] * 3.240479f) + (xyz[1] * -1.537150f) + (xyz[2] * -.498535f);
rgb[1] = (xyz[0] * -.969256f) + (xyz[1] * 1.875992f) + (xyz[2] * .041556f);
rgb[2] = (xyz[0] * .055648f) + (xyz[1] * -.204043f) + (xyz[2] * 1.057311f);
for (int i = 0; i < 3; i++)
{
if (rgb[i] > .0031308f)
{
rgb[i] = (1.055f * (float)Math.Pow(rgb[i], (1.0f / 2.4f))) - .055f;
}
else
{
rgb[i] = rgb[i] * 12.92f;
}
}
rgb[0] = rgb[0] * 255.0f;
rgb[1] = rgb[1] * 255.0f;
rgb[2] = rgb[2] * 255.0f;
return new Vector4(rgb[0], rgb[1], rgb[2], color[3]);
}
public static Vector4 LabToRGB(Vector4 color)
{
Vector4 xyz = LabToXYZ(color);
Vector4 rgb = XYZToRGB(xyz);
Debug.Log("R: " + rgb[0]);
Debug.Log("G: " + rgb[1]);
Debug.Log("B: " + rgb[2]);
Debug.Log("A: " + color[3]);
return new Vector4 (rgb[0],rgb[1],rgb[2]);
}