C# 如何反转颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1165107/
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 do I invert a colour?
提问by Lloyd Powell
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?
我知道这不会直接反转颜色,它只会“反对”它。我想知道是否有人知道一种简单的方法(几行代码)从任何给定颜色反转颜色?
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):
目前我有这个(这不完全是反转的定义,因为如果我将它传递给灰色/灰色,它将返回非常相似的东西,例如 127、127、127):
const int RGBMAX = 255;
Color InvertMeAColour(Color ColourToInvert)
{
return Color.FromArgb(RGBMAX - ColourToInvert.R,
RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B);
}
采纳答案by ThibThib
It depends on what do you mean by "inverting" a color
这取决于“反转”颜色是什么意思
Your code provides a "negative" color.
您的代码提供了“负”颜色。
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 HSVmode (you will find hereto make the transformation).
您是否正在寻找青色中的红色、紫色中的绿色、黄色中的蓝色(等等)?如果是这样,您需要在HSV模式下转换您的 RGB 颜色(您可以在此处找到进行转换)。
Then you just need to invert the Hue value (change Hue
by 360-Hue
) and convert back to RGB mode.
然后您只需要反转色相值(更改Hue
为360-Hue
)并转换回 RGB 模式。
EDIT: 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)
编辑:亚历克斯Semeniuk提到,改变Hue
由(Hue + 180) % 360
是更好的解决方案(它不倒置色调,但发现在色环相反的颜色)
回答by korona
Try this:
尝试这个:
uint InvertColor(uint rgbaColor)
{
return 0xFFFFFF00u ^ rgbaColor; // Assumes alpha is in the rightmost byte, change as needed
}
回答by Kenan E. K.
Invert the bits of each component separately:
分别反转每个组件的位:
Color InvertMeAColour(Color ColourToInvert)
{
return Color.FromArgb((byte)~ColourToInvert.R, (byte)~ColourToInvert.G, (byte)~ColourToInvert.B);
}
EDIT: The ~ operator does not work with bytes automatically, cast is needed.
编辑: ~ 运算符不能自动处理字节,需要强制转换。
回答by Henk Holterman
What you already have is an RGB-Invert. There are other ways to classify colors and hence other definitions for the Inverse of a Color.
您已经拥有的是 RGB 反转。还有其他方法可以对颜色进行分类,因此还有其他颜色反转的定义。
But it sounds like maybe you want a contrasting Color, and there isn't a simple Inversion that is going to work for all colors including RGB(127, 127, 127).
但听起来您可能想要对比色,并且没有一个简单的反转可以适用于所有颜色,包括 RGB(127, 127, 127)。
What you need is 1) a conversion to HSV (see ThibThibs answer) and invert the Hue, but also 2) check if the Hue isn't to close to the middle and if so go to either fully bright or fully dark.
您需要的是 1) 转换为 HSV(参见 ThibThibs 答案)并反转色相,但还要 2)检查色相是否靠近中间,如果是,则转到完全亮或完全暗。
回答by Remy
You can use :
您可以使用 :
MyColor=Color.FromArgb(MyColor.ToArgb()^0xffffff);
It will invert MyColor.
它将反转 MyColor。
回答by Raxin
If you want to change every color, try a rotational function (shifting or adding) rather than a flipping function (inverting). In other words, consider the range of 0 to 255 for each single color (red, green, and blue) to be wrapped, connected at the tips like a circle of values. Then shift each color around the cirle by adding some value and doing mod 256. For example, if your starting value for red is 255, and you add 1, you get 0. If you shift all three colors by 128, you get dramatically different values for every original color in the picture, even the grays. Gray 127, 127, 127 becomes white 255, 255, 255. Gray 128, 128, 128 becomes black 0, 0, 0. There's a photographic effect like that called Solarization, discovered by accident by Man Ray in the 1930's.
如果您想更改每种颜色,请尝试使用旋转功能(移动或添加)而不是翻转功能(反转)。换句话说,考虑0 到255 范围内的每种颜色(红色、绿色和蓝色)被包裹,在尖端连接,就像一个值的圆圈。然后通过添加一些值并执行 mod 256 来移动圆形周围的每种颜色。例如,如果红色的起始值为 255,并且添加 1,则为 0。如果将所有三种颜色都移动 128,则会得到显着不同图片中每种原始颜色的值,甚至是灰色。灰色 127, 127, 127 变成白色 255, 255, 255。灰色 128, 128, 128 变成黑色 0, 0, 0. 有一种叫做 Solarization 的摄影效果,是由 Man Ray 在 1930 年代偶然发现的。
You can also do rotational operations on each color (red, green, blue) by a different amount to really mess up a picture.
您还可以对每种颜色(红色、绿色、蓝色)进行不同数量的旋转操作,以真正弄乱图片。
You can also do rotational operations on hue, shifting the hue of every original color by some amount on the hue circle, which alters all the colors without altering the brightness, so the shadows still look like shadows, making people look like Simpsons or Smurphs for example.
还可以对色相进行旋转操作,在色相环上将每一种原始颜色的色相偏移一定量,在不改变亮度的情况下改变所有颜色,因此阴影看起来仍然像阴影,使人们看起来像辛普森或蓝精灵例子。
The code for a shift by 128 could look like:
移位 128 的代码可能如下所示:
public static Color Invert(this Color c) => Color.FromArgb(c.R.Invert(), c.G.Invert(), c.B.Invert());
public static byte Invert(this byte b) {
unchecked {
return (byte)(b + 128);
}
}
回答by Christos Lytras
The most simple and lazy way I made it to work with having not only triple 12x, but mixed values, is this:
我使用它的最简单和懒惰的方法不仅是三倍 12 倍,而且是混合值,是这样的:
Color invertedColor= Color.FromArgb(fromColor.ToArgb() ^ 0xffffff);
if (invertedColor.R > 110 && invertedColor.R < 150 &&
invertedColor.G > 110 && invertedColor.G < 150 &&
invertedColor.B > 110 && invertedColor.B < 150)
{
int avg = (invertedColor.R + invertedColor.G + invertedColor.B) / 3;
avg = avg > 128 ? 200 : 60;
invertedColor= Color.FromArgb(avg, avg, avg);
}
Now, invertedColor
has a different color that the original, even if we have a 128, 128, 128 or close to it color value.
现在,invertedColor
具有与原始颜色不同的颜色,即使我们有 128、128、128 或接近它的颜色值。
回答by Alexandru-Codrin Panaite
I've created this simple function for VB.NET:
我为 VB.NET 创建了这个简单的函数:
Public Shared Function Invert(ByVal Culoare As Color) As Color
Return Color.FromArgb(Culoare.ToArgb() Xor &HFFFFFF)
End Function
And this one for C#:
这是 C# 的:
public static Color Invert(Color Culoare)
{
return Color.FromArgb(Culoare.ToArgb() ^ 0xFFFFFF);
}