如何从像素中获取HSV和CMYK颜色?
我已经制作了一个颜色选择工具,可以从像素中获取ARGB颜色。我已经弄清楚如何获得HTML和HEX颜色,但无法弄清楚如何获得HSV或CMYK。我找到的所有文章都在C#中。我尝试过转换一些(使用Web转换器),但我总是遇到问题。这里是用于获取ARGB和HTML颜色的代码:
PXL_COLOR = bmp.GetPixel(0,0).ToString'' ARGB
PXL_HTML = System.Drawing.ColorTranslator.ToHtml(bmp.GetPixel(0,0))''HTML
有人可以帮忙吗?
I''ve made a color picker tool that gets a ARGB color from a pixel. I''ve figured out how to get HTML and HEX colors, but can''t figure out how to get HSV or CMYK. All the articles that I''ve found are in C#. I''ve tried converting a few(with web converters), but I always run into a problem. Here''s the code that I''m using to get the ARGB and HTML colors:
PXL_COLOR = bmp.GetPixel(0, 0).ToString ''ARGB
PXL_HTML = System.Drawing.ColorTranslator.ToHtml(bmp.GetPixel(0, 0)) ''HTML
Can someone please help?
如果您有RGB,您可以计算CMYK和HSV值。
看一下这个链接: http://www.rapidtables.com/convert/color /index.htm [ ^ ]
例如:
R = 50 = 50/255 = 0.196
G = 100 = 100/255 = 0.392
B = 150 = 150/255 = 0.588
K = 1-max(0.196,0.392,0.588)= 0.412
C =(1-0.196-0.412)/(1-0.412)= 0.667
M =(1-0.392-0.412)/(1-0.412)= 0.333
Y =(1-0.58 8-0.412)/(1-0.412)= 0
If you have RGB you can calculate the CMYK and HSV values.
Have a look at this link: http://www.rapidtables.com/convert/color/index.htm[^]
Example:
R=50 = 50/255 = 0.196
G=100 = 100/255 = 0.392
B=150 = 150/255 = 0.588
K = 1-max(0.196, 0.392, 0.588) = 0.412
C = (1-0.196-0.412)/(1-0.412) = 0.667
M = (1-0.392-0.412)/(1-0.412) = 0.333
Y = (1-0.588-0.412)/(1-0.412) = 0
没问题。
您可以使用Math.Max但它只接受2个值进行比较。 Max比较多个值并返回最大值。
这应该可以解决问题:)
No Problem.
You can use Math.Max but it only accepts 2 values to compare. Max compares multiple values and retuns the largest one.
This should do the trick :)
Structure CMYK
Dim C As Double
Dim M As Double
Dim Y As Double
Dim K As Double
End Structure
Structure RGB
Dim R As Double
Dim G As Double
Dim B As Double
End Structure
Private Function RGBtoCMYK(ByVal rgbIn As RGB) As CMYK
Dim cmykOut As CMYK
With rgbIn
.R = .R / 255
.G = .G / 255
.B = .B / 255
End With
'math.max only accepts 2 aguments to compare.
'Put all the values in an array and get the .Max of the array
Dim arr(2) As Double
arr(0) = rgbIn.R
arr(1) = rgbIn.G
arr(2) = rgbIn.B
With cmykOut
.K = 1 - arr.Max
.C = (1 - rgbIn.R - .K) / (1 - .K)
.M = (1 - rgbIn.G - .K) / (1 - .K)
.Y = (1 - rgbIn.B - .K) / (1 - .K)
End With
Return cmykOut
End Function
你可以使用像
You can call the funtion usings something like
Dim colorIn As RGB
With colorIn
.R = 50
.G = 100
.B = 150
End With
Dim colorOut As CMYK = RGBtoCMYK(colorIn)