【发布时间】:2017-03-11 18:25:09
【问题描述】:
我知道这个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;
如果我正在处理的颜色是原色,它可以正常工作,但在任何其他情况下都不会。是什么让它无处不在?我是否走在正确的轨道上?
【问题讨论】:
-
我想知道您的整数除法是否是问题的一部分。您不希望倍数为浮点数/小数并截断/舍入最终值吗?
-
“不起作用”是什么意思?
-
@shawnt00 我不这么认为。我很确定 .NET 可以解决这个问题。通过除以双精度数或浮点数然后转换为整数得到相同的结果,但我得到相同的结果。
-
@Paul Abbott 我已经用 gif 更新了帖子,显示了这个问题。
-
int multiplier = 255 / M;- 这真的是整数除法吗?
标签: c# colors rgb color-management color-theory