如何用C#“自然地”混合颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/398224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mix colors "naturally" with C#?
提问by crono
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
等等。我将颜色作为 RGB 值。当我尝试混合它们时,我得到了正确的“RGB”结果,例如
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?
但不是正确的“自然湿油漆”结果。知道如何以自然的方式混合 RGB 吗?
It would be great if someone knew a solution within the Microsoft.Xna.Framework.Graphics
namespace but a generic solution would also help :)
如果有人知道Microsoft.Xna.Framework.Graphics
命名空间内的解决方案,那就太好了,但通用解决方案也会有所帮助:)
@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.
当然 - 这是我混合两种 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));
}
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.
到目前为止,我在该线程中读到的内容非常有希望 - 我将 C1 和 C2 转换为 L ab*,将它们混合 - 将其转换回 RGB 并返回该颜色。
采纳答案by Jason Cohen
"Natural wet paint" is a little ambiguous; the mixing of CMYK as suggested won't work because you're still adding colors.
“天然湿漆”有点含糊;建议的 CMYK 混合不起作用,因为您仍在添加颜色。
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.
如果你想要像 Photoshop 一样的结果(如 Jon B 所检查的),你需要使用 L*a*b* 空间。RGB 与 Lab 之间的转换公式和说明在这里。
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.
尝试当前建议的任何其他方法不仅会导致您不想要的颜色,而且不会代表颜色的“持续外观”变化,尤其是如果您将其用于诸如渐变之类的持续变化很重要的事情时.
回答by Bart
回答by Mark Cidade
There is a good article for converting RGB to Lab in C# at http://www.codeproject.com/KB/recipes/colorspace1.aspx.
在http://www.codeproject.com/KB/recipes/colorspace1.aspx 上有一篇关于将 RGB 转换为C# 中的L ab的好文章。