将RGB转换为RGBW

问题描述 投票:4回答:3

我知道这个has already been asked,但那里的anwser不起作用。我花了一个多小时寻找一个公式或算法,却一无所获。结果,我开始编写自己的算法,以最有效的方式将RGB转换为RGBW。这就是我目前得到的:

        //'Ri', 'Gi', and 'Bi' correspond to the Red, Green, and Blue inputs.
        var M = Math.Max(Ri, Math.Max(Gi, Bi)); //The maximum value between R,G, and B.
        int Wo =0; //White output
        int Ro=0; //Red output
        int Go=0; //Green output
        int Bo=0; //Blue output

        int av = 0; //Average between the two minimum values
        int hR = 0; //Red with 100% hue
        int hG = 0; //Green with 100% hue
        int hB = 0; //Blue with 100% hue

        //These 4 lines serve to figure out what the input color is with 100% hue.
        float multiplier = 255.0f / M;
        hR = Convert.ToInt32(Ri * multiplier);
        hG = Convert.ToInt32(Gi * multiplier);
        hB = Convert.ToInt32(Bi * multiplier);

        //Depending on the maximum value, get an average of the least used colors, weighted for their importance in the overall hue.
        //This is the problematic part
        if (M == Ri)
           av = (Bi*hB + Gi*hG) / (hB+hG);
        else if (M == Gi)
            av = (Ri*hR + Bi*hB) / (hR+hB);
        else if (M == Bi)
            av = (Gi*hG + Ri*hR) / (hG+hR);

        //Set the rgbw colors
        Wo = av;
        Bo = Bi - av;
        Ro = Ri - av;
        Go = Gi - av;
        if (Wo < 1) Wo = 0;
        if (Bo < 1) Bo = 0;
        if (Ro < 1) Ro = 0;
        if (Go < 1) Go = 0;
        if (Wo > 255) Wo = 255;
        if (Bo > 255) Bo = 255;
        if (Ro > 255) Ro = 255;
        if (Go > 255) Go = 255;

如果我正在处理的颜色是原色,它可以正常工作,但在任何其他情况下都不行。什么会使它在任何地方都有效?我是否走在正确的轨道上?

编辑:这是我遇到的问题的.gif文件。 RGBW值一直在底部enter image description here

c# colors rgb color-management color-theory
3个回答
7
投票

我终于想出如何将RGB转换为RGBW,结果我以前的方法完全错误:

//Get the maximum between R, G, and B
float tM = Math.Max(Ri, Math.Max(Gi, Bi));

//If the maximum value is 0, immediately return pure black.
if(tM == 0)
   { return new rgbwcolor() { r = 0, g = 0, b = 0, w = 0 }; }

//This section serves to figure out what the color with 100% hue is
float multiplier = 255.0f / tM;
float hR = Ri * multiplier;
float hG = Gi * multiplier;
float hB = Bi * multiplier;  

//This calculates the Whiteness (not strictly speaking Luminance) of the color
float M = Math.Max(hR, Math.Max(hG, hB));
float m = Math.Min(hR, Math.Min(hG, hB));
float Luminance = ((M + m) / 2.0f - 127.5f) * (255.0f/127.5f) / multiplier;

//Calculate the output values
int Wo = Convert.ToInt32(Luminance);
int Bo = Convert.ToInt32(Bi - Luminance);
int Ro = Convert.ToInt32(Ri - Luminance);
int Go = Convert.ToInt32(Gi - Luminance);

//Trim them so that they are all between 0 and 255
if (Wo < 0) Wo = 0;
if (Bo < 0) Bo = 0;
if (Ro < 0) Ro = 0;
if (Go < 0) Go = 0;
if (Wo > 255) Wo = 255;
if (Bo > 255) Bo = 255;
if (Ro > 255) Ro = 255;
if (Go > 255) Go = 255;
return new rgbwcolor() { r = Ro, g = Go, b = Bo, w = Wo };

enter image description here

任何优化的想法都非常欢迎:)


1
投票

我认为问题在于:

    if (M == Ri)
       av = (Bi*hB + Gi*hG) / (hB+hG);
    else if (M == Gi)
        av = (Ri*hR + Bi*hB) / (hR+hB);
    else if (M == Bi)
        av = (Gi*hG + Ri*hR) / (hG+hR);

你在这里做整数除法。所有变量都是整数,因此除法将是整数除法。

    if (M == Ri)
       av = (int)((float)(Bi*hB + Gi*hG) / (hB+hG));
    else if (M == Gi)
        av = (int)((float)(Ri*hR + Bi*hB) / (hR+hB));
    else if (M == Bi)
        av = (int)((float)(Gi*hG + Ri*hR) / (hG+hR));

这将进行浮点除法,并应该为您提供所需的答案。你仍然可能会发现你有舍入错误 - 在这种情况下将float更改为double

将乘数计算更改为float:

float multiplier = 255.0 / M;

只解决了问题的一半,但现在导致另一个并发症作为你的测试:

if (M == Ri)
else if (M == Gi)
else if (M == Bi)

现在不太可能是真的。您需要在测试中添加舍入误差因子。

if (Math.Abs(M - Ri) < epsilon)
else if (Math.Abs(M - Gi) < epsilon)
else if (Math.Abs(M - Bi) < epsilon)

其中epsilon是一个合适的小值(通常我建议10e-6,但你可以试验。

此外,如果M不足以接近任何RGB值,则不设置av - 它始终保持设置为零。这也将为您提供您所看到的一切都是黑色的结果


0
投票

我可能有点偏离这里,但作为一名电子工程师,我看到LED输出光然后编码XD。无论如何输出白色可以通过“照亮”所有rgb或白色通道来完成,但是保持相同的强度(以及从我的角度看的LED的良好状态)所有4个通道可以混合输出与过驱动白色相同的白色渠道。我怀疑此时有任何问题,但稍后可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.