且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C中将RGB颜色更改为对立面?

更新时间:2023-01-27 22:52:10

他们似乎是矛盾的:

They seemed contradictory:
引用:

将RGB颜色改为对立

引用:

查看一个色轮,看看我是否能够将像素的颜色移动到方向盘的三分之一

look at a color wheel to see if I was able to move the pixel's color a third of the wheel



它标题是正确的,那不仅仅是一个问题:


It the title is correct, then isn't it just a matter of:

newR = 255 - oldR
newG = 255 - oldG
newB = 255 - oldB



,无论编程语言如何。


regardless of the programming language.


在不到30秒的时间内快速搜索CodeProject,为这个宝石提供了服务:图像处理基础 - 幕后 [ ^ ]
A quick search of CodeProject in less than 30 seconds served up this gem: Fundamentals of Image Processing - behind the scenes[^]


如果你想要倒置颜色同时影响色调和亮度,可以使用简单的反转。



只需从十六进制转换为RGB并反转每个通道,



R = R * -1 + 255

G = G * -1 + 255

B = B * -1 + 255




然后转换回到十六进制。



如果你想要相反的色调,而不影响亮度,最直接的方法是从十六进制转换为HSL和将色调旋转180度。



从十六进制转换为HSL并旋转180度。



H = H + 180%360



然后转换回十六进制。



Taken来自此处 [ ^ ]。
If you want the inverted color which affects both hue and lightness, a simple invert will do.

Just convert from hex to RGB and invert each channel,

R = R *-1 + 255
G = G *-1 + 255
B = B *-1 + 255


And then convert back to hex.

If you want the opposite hue, without affecting lightness, the most straightforward way is to convert from hex to HSL and rotate the hue 180 degrees.

Convert from hex to HSL and rotate H 180 degrees.

H = H +180 % 360

And then convert back to hex.

Taken from here[^] .