如何反转颜色? [英] How do I invert a colour?
问题描述
我知道这不会直接反转颜色,只会反对颜色。我想知道是否有人知道一种简单的方法(几行代码)来将任何给定颜色的颜色反转?
I know that this won't directly invert a colour, it will just 'oppose' it. I was wondering if anyone knew a simple way (a few lines of code) to invert a colour from any given colour?
目前我有这个方法(这是 t恰好是反转的定义,因为如果我将其传递为灰色/灰色,它将返回极其相似的内容,例如127、127、127):
At the moment I have this (which isn't exactly the definition of an invert, because if I pass it a grey / gray colour it will return something extremely similar e.g. 127, 127, 127):
const int RGBMAX = 255;
Color InvertMeAColour(Color ColourToInvert)
{
return Color.FromArgb(RGBMAX - ColourToInvert.R,
RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B);
}
推荐答案
这取决于您的工作意思是反转颜色
It depends on what do you mean by "inverting" a color
您的代码提供了负颜色。
Your code provides a "negative" color.
您在寻找吗?将红色转换为青色,将绿色转换为紫色,将蓝色转换为黄色(依此类推)?如果是这样,您需要在 HSV 模式下转换RGB颜色(您会发现此处
Are you looking for transform red in cyan, green in purple, blue in yellow (and so on) ? If so, you need to convert your RGB color in HSV mode (you will find here to make the transformation).
然后,您只需要反转色相值(将 Hue
更改为 360-Hue
)并转换回RGB模式。
Then you just need to invert the Hue value (change Hue
by 360-Hue
) and convert back to RGB mode.
编辑:正如Alex Semeniuk所说,更改 Hue
by (Hue + 180)%360
是一个更好的解决方案(它不会反转色相,但会在色环上找到相反的颜色)
as Alex Semeniuk has mentioned, changing Hue
by (Hue + 180) % 360
is a better solution (it does not invert the Hue, but find the opposite color on the color circle)
这篇关于如何反转颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!