c# - 将 RGB 转换为 RGBW

标签 c# colors rgb color-management color-theory

我知道这个has already been asked ,但那里给出的答案不起作用。我花了一个多小时寻找公式或算法,但一无所获。因此,我开始编写自己的算法,以尽可能最有效的方式将 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

最佳答案

我终于想通了如何将 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

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

关于c# - 将 RGB 转换为 RGBW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312216/

相关文章:

android - Renderscript 致命信号 11 (SIGSEGV) 代码 1 (SEGV_MAPERR) 故障地址

c# - Blazor Textfield Oninput 用户键入延迟

php - 在 sudo 模式下运行 emacs 时保留用户的配置

iphone - iOS RGB 颜色不正确?

python - 如何在 json.dump 生成的字符串中显示颜色?

javascript - 将半透明转换为纯色的算法

python - 我需要检测屏幕的一小部分区域的颜色(红色)(我可以计算出坐标)。 (Python)

c# - C#Selenium如何单击嵌入的youtube视频的播放按钮

c# - 访问属性的最佳方式

c# - 无法将文件 ‘{0}' 作为数据库 '{1}' 附加。先写代码。本地 SQL EXPRESS 实例。 VS 2010 SP1。 window XP