C# RGB/XYZ和XYZ-LAB颜色空间转换算法

C# RGB/XYZ和XYZ-LAB颜色空间转换算法,c#,rgb,cielab,xyz,C#,Rgb,Cielab,Xyz,我尝试将RGB颜色空间转换为CIE-LAB颜色空间。但这是一个错误 输入RGB值=(50125,50) 收到的结果实验室值=(41,-38,34) 实验室值应为(46.41,-39.24,33.51) 我从中检查了这个值 我在哪里犯了错误 如果您检查以下代码并回答我。我会很高兴的。谢谢 public static Vector4 RGBToLab(Vector4 color) { float[] xyz = new float[3];

我尝试将RGB颜色空间转换为CIE-LAB颜色空间。但这是一个错误

  • 输入RGB值=(50125,50)
  • 收到的结果实验室值=(41,-38,34)
实验室值应为(46.41,-39.24,33.51)

我从中检查了这个值 我在哪里犯了错误

如果您检查以下代码并回答我。我会很高兴的。谢谢

 public static Vector4 RGBToLab(Vector4 color)
        {
            float[] xyz = new float[3];
            float[] lab = new float[3];
            float[] rgb = new float[] { color[0], color[1], color[2], color[3] };

            rgb[0] = color[0] / 255.0f;
            rgb[1] = color[1] / 255.0f;
            rgb[2] = color[2] / 255.0f;

            if (rgb[0] > .04045f)
            {
                rgb[0] = (float)Math.Pow((rgb[0] + .0055) / 1.055, 2.4);
            }
            else
            {
                rgb[0] = rgb[0] / 12.92f;
            }

            if (rgb[1] > .04045f)
            {
                rgb[1] = (float)Math.Pow((rgb[1] + .0055) / 1.055, 2.4);
            }
            else
            {
                rgb[1] = rgb[1] / 12.92f;
            }

            if (rgb[2] > .04045f)
            {
                rgb[2] = (float)Math.Pow((rgb[2] + .0055) / 1.055, 2.4);
            }
            else
            {
                rgb[2] = rgb[2] / 12.92f;
            }
            rgb[0] = rgb[0] * 100.0f;
            rgb[1] = rgb[1] * 100.0f;
            rgb[2] = rgb[2] * 100.0f;


            xyz[0] = ((rgb[0] * .412453f) + (rgb[1] * .357580f) + (rgb[2] * .180423f));  
            xyz[1] = ((rgb[0] * .212671f) + (rgb[1] * .715160f) + (rgb[2] * .072169f));
            xyz[2] = ((rgb[0] * .019334f) + (rgb[1] * .119193f) + (rgb[2] * .950227f));


            xyz[0] = xyz[0] / 95.047f;
            xyz[1] = xyz[1] / 100.0f;
            xyz[2] = xyz[2] / 108.883f;

            if (xyz[0] > .008856f)
            {
                xyz[0] = (float)Math.Pow(xyz[0], (1.0 / 3.0));
            }
            else
            {
                xyz[0] = (xyz[0] * 7.787f) + (16.0f / 116.0f);
            }

            if (xyz[1] > .008856f)
            {
                xyz[1] = (float)Math.Pow(xyz[1], 1.0 / 3.0);
            }
            else
            {
                xyz[1] = (xyz[1] * 7.787f) + (16.0f / 116.0f);
            }

            if (xyz[2] > .008856f)
            {
                xyz[2] = (float)Math.Pow(xyz[2], 1.0 / 3.0);
            }
            else
            {
                xyz[2] = (xyz[2] * 7.787f) + (16.0f / 116.0f);
            }

            lab[0] = (116.0f * xyz[1]) - 16.0f;
            lab[1] = 500.0f * (xyz[0] - xyz[1]);
            lab[2] = 200.0f * (xyz[1] - xyz[2]);
            Debug.Log("L:" + (int)lab[0]);
            Debug.Log("A:" + (int)lab[1]);
            Debug.Log("B:" + (int)lab[2]);
            Debug.Log("W:" + (int)color[3]);

            return new Vector4(lab[0], lab[1], lab[2], color[3]);
        }


而不是
.0055
,它应该是
0.055

反伽马运算错误:

rgb[0] = (float)Math.Pow((rgb[0] + .0055) / 1.055, 2.4);
应该是:

rgb[0] = (float)Math.Pow((rgb[0] + .055) / 1.055, 2.4);
见: