如何“自然地”混合颜色 用C#?
我必须以自然的方式混合一些颜色。 这意味着
blue + yellow = green
blue + red = purple
等等。 我得到的颜色是 RGB 值。 当我尝试混合它们时,我得到了正确的“RGB”结果,例如
green + red = yellow
yellow + blue = white
但不是正确的“自然湿漆”结果。 有什么好主意如何以自然的方式混合 RGB?
如果有人知道 Microsoft.Xna.Framework.Graphics 命名空间中的解决方案,那就太好了,但通用解决方案也会有所帮助:)
@Jay Bazuzi:
请发布一个代码示例来显示 你想做什么。
当然 - 这是我混合两种 RGB 颜色的函数。
public Color colorMixer(Color c1, Color c2)
{
int _r = Math.Min((c1.R + c2.R),255);
int _g = Math.Min((c1.G + c2.G),255);
int _b = Math.Min((c1.B + c2.B),255);
return new Color(Convert.ToByte(_r),
Convert.ToByte(_g),
Convert.ToByte(_b));
}
到目前为止,我在这个线程中读到的内容非常有前途 - 我将把 C1 和 C2 转换为 Lab*,混合它们 - 将其转换回 RGB 并返回该颜色。
I have to mix some colors in a natural way. This means
blue + yellow = green
blue + red = purple
And so on. I got the colors as RGB-Values. When I try to mix them I got the right "RGB"-results like
green + red = yellow
yellow + blue = white
But not the right "natural-wet-paint"-results. Any good idea how to mix RGB in a natural way?
It would be great if someone knew a solution within the Microsoft.Xna.Framework.Graphics
namespace but a generic solution would also help :)
@Jay Bazuzi:
Please post a code sample that shows
what you're trying to do.
Sure - this is my function for mixing the two RGB-Colors.
public Color colorMixer(Color c1, Color c2)
{
int _r = Math.Min((c1.R + c2.R),255);
int _g = Math.Min((c1.G + c2.G),255);
int _b = Math.Min((c1.B + c2.B),255);
return new Color(Convert.ToByte(_r),
Convert.ToByte(_g),
Convert.ToByte(_b));
}
What I have read so far in this thread is very promising - I will convert C1 and C2 to Lab*, mix them - convert it back to RGB and return that color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“天然湿漆”有点含糊; 建议的 CMYK 混合不起作用,因为您仍在添加颜色。
如果您想要像 Photoshop 中那样的结果(如 Jon B 所检查),您需要使用 L*a*b* 空间。用于将 RGB 与 Lab 相互转换的公式以及说明 在这里。
实验室空间经过专门设计,使得线性变化与人眼感知的一定量的颜色变化相对应。 这很重要,因为例如我们对绿色比其他颜色更敏感,因为我们根据色调和亮度等感知不同的变化。
尝试当前建议的任何其他方法不仅会导致您不想要的颜色,而且也不会代表“看起来恒定”的颜色变化,特别是当您将其用于诸如渐变之类的恒定变化很重要的东西时。
"Natural wet paint" is a little ambiguous; the mixing of CMYK as suggested won't work because you're still adding colors.
If you want results like in Photoshop (as Jon B checked) you need to use L*a*b* space. Formulas for converting RGB to/from Lab and a description is here.
Lab space was specifically designed so that linear changes correspond to what the human eye perceives as a certain amount of color change. This is important because e.g. we are more sensitive to green than other colors, because we perceive changes differently depending both on hue and lightness, etc..
Trying any other methods currently being suggested will not only result in colors you don't want, but also won't represent a "constant-looking" change in color, especially if you use this for something where constant-change matters like a gradient.
您可以使用 WIC 免费获得 cmyk<->rgb 转换。
但它只是 .NET 3.0You get cmyk<->rgb conversion for free with WIC.
but it's .NET 3.0 only