[名称] 颜色RGB/HSB/CMYK/Lab[数据来源] enmity/csdn[内容简介] 空[源代码内容]Private R As Byte Private G As Byte Private B As Byte Public Property Get cmyC() As Byte cmyC = 255 - R End PropertyPublic Property Get cmyM() As Byte cmyM = 255 - G End Property Public Property Get cmykK() As Integer cmykK = Minimum(255 - R, 255 - G, 255 - B) / 2.55 End PropertyPublic Property Get cmykC() As Integer Dim MyR As Integer, Div As Integer MyR = R / 2.55 Div = (100 - cmykK) If Div = 0 Then Div = 1 cmykC = ((100 - MyR - cmykK) / Div) * 100 End PropertyPublic Property Get cmykM() As Integer Dim MyG As Integer, Div As Integer MyG = G / 2.55 Div = (100 - cmykK) If Div = 0 Then Div = 1 cmykM = ((100 - MyG - cmykK) / Div) * 100 End PropertyPublic Property Get cmykY() As Integer Dim MyB As Integer, Div As Integer MyB = B / 2.55 Div = (100 - cmykK) If Div = 0 Then Div = 1 cmykY = ((100 - MyB - cmykK) / Div) * 100 End PropertyPublic Property Get cmyY() As Byte cmyY = 255 - B End PropertyPublic Property Get hlsH() As Integer Dim MyR As Single, MyG As Single, MyB As Single Dim Max As Single, Min As Single Dim Delta As Single, MyVal As Single MyR = R / 255: MyG = G / 255: MyB = B / 255 Max = Maximum(MyR, MyG, MyB) Min = Minimum(MyR, MyG, MyB) If Max <> Min Then Delta = Max - Min Select Case Max Case MyR MyVal = (MyG - MyB) / Delta Case MyG MyVal = 2 + (MyB - MyR) / Delta Case MyB MyVal = 4 + (MyR - MyG) / Delta End Select End If MyVal = (MyVal + 1) * 60 If MyVal < 0 Then MyVal = MyVal + 360 hlsH = MyVal Debug.Print hlsH End Property Public Property Get hlsL() As Integer hlsL = ((Maximum(R, G, B) + Minimum(R, G, B)) / 2) / 2.55 End Property Public Property Get hlsS() As Integer Dim MyR As Single, MyG As Single, MyB As Single Dim Max As Single, Min As Single, MyS As Single MyR = R / 255: MyG = G / 255: MyB = B / 255 Max = Maximum(MyR, MyG, MyB) Min = Minimum(MyR, MyG, MyB) If Max <> Min Then If hlsL <= 50 Then MyS = (Max - Min) / (Max + Min) Else MyS = (Max - Min) / (2 - Max - Min) End If hlsS = MyS * 100 End If End PropertyPrivate Function Minimum(ParamArray Vals()) Dim n As Integer, MinVal MinVal = Vals(0) For n = 0 To UBound(Vals) If Vals(n) < MinVal Then MinVal = Vals(n) Next n Minimum = MinVal End Function Private Function Maximum(ParamArray Vals()) Dim n As Integer, MaxVal For n = 0 To UBound(Vals) If Vals(n) > MaxVal Then MaxVal = Vals(n) Next n Maximum = MaxVal End FunctionPublic Property Let rgbR(NewVal As Byte) R = NewVal End Property Public Property Get rgbR() As Byte rgbR = R End PropertyPublic Property Get rgbG() As Byte rgbG = G End PropertyPublic Property Get rgbB() As Byte rgbB = B End Property Public Property Get ycbcrY() As Byte ycbcrY = R * 0.2989 + G * 0.5866 + B * 0.1145 End Property Public Property Get ycbcrCb() As Byte Dim MyCb As Integer MyCb = -0.1687 * R - 0.3313 * G + 0.5 * B + 128 ycbcrCb = IIf(MyCb <= 255, MyCb, 255) End Property Public Property Get ycbcrCr() As Byte Dim MyCr As Integer MyCr = 0.5 * R - 0.4187 * G - 0.0813 * B + 128 ycbcrCr = IIf(MyCr <= 255, MyCr, 255) End PropertyPublic Property Let rgbG(NewVal As Byte) G = NewVal End Property Public Property Let rgbB(NewVal As Byte) B = NewVal End Property Public Sub SetCMY(C As Integer, M As Integer, Y As Integer) R = 255 - C G = 255 - M B = 255 - Y End Sub Public Sub SetHLS(H As Integer, L As Integer, S As Integer) Dim MyR As Single, MyG As Single, MyB As Single Dim MyH As Single, MyL As Single, MyS As Single Dim Min As Single, Max As Single, Delta As Single MyH = (H / 60) - 1: MyL = L / 100: MyS = S / 100 If MyS = 0 Then MyR = MyL: MyG = MyL: MyB = MyL Else If MyL <= 0.5 Then Min = MyL * (1 - MyS) Else Min = MyL - MyS * (1 - MyL) End If Max = 2 * MyL - Min Delta = Max - Min
Select Case MyH Case Is < 1 MyR = Max If MyH < 0 Then MyG = Min MyB = MyG - MyH * Delta Else MyB = Min MyG = MyH * Delta + MyB End If Case Is < 3 MyG = Max If MyH < 2 Then MyB = Min MyR = MyB - (MyH - 2) * Delta Else MyR = Min MyB = (MyH - 2) * Delta + MyR End If Case Else MyB = Max If MyH < 4 Then MyR = Min MyG = MyR - (MyH - 4) * Delta Else MyG = Min MyR = (MyH - 4) * Delta + MyG End If End Select End If R = MyR * 255: G = MyG * 255: B = MyB * 255 End Sub Public Sub SetCMYK(C As Integer, M As Integer, Y As Integer, K As Integer) Dim MyC As Single, MyM As Single, MyY As Single, MyK As Single MyC = C / 100: MyM = M / 100: MyY = Y / 100: MyK = K / 100 R = (1 - (MyC * (1 - MyK) + MyK)) * 255 G = (1 - (MyM * (1 - MyK) + MyK)) * 255 B = (1 - (MyY * (1 - MyK) + MyK)) * 255 End SubPublic Sub SetYCbCr(Y As Integer, Cb As Integer, Cr As Integer) Dim MyR As Integer, MyG As Integer, MyB As Integer MyR = Y + 1.402 * (Cr - 128) MyG = Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128) MyB = Y + 1.772 * (Cb - 128) If MyR > 255 Then MyR = 255 If MyG > 255 Then MyG = 255 If MyB > 255 Then MyB = 255 If MyR < 0 Then MyR = 0 If MyG < 0 Then MyG = 0 If MyB < 0 Then MyB = 0 R = MyR G = MyG B = MyB End Sub | HSB | CMYK PhotoShop | H=300,S=75,B=100 |C=38,M=62,Y=0,K=1 你的程序 | H=0,S=100,B=63 | C=0,M=75,Y=0,K=0 以上代码保存于: SourceCode Explorer(源代码数据库) 复制时间: 2003-02-06 23:04:22 软件版本: 1.0.819 软件作者: Shawls 个人主页: Http://Shawls.Yeah.Net E-Mail: [email protected] QQ: 9181729
但就是与PhotoShop计算的结果不一样
Private G As Byte
Private B As Byte
Public Property Get cmyC() As Byte
cmyC = 255 - R
End PropertyPublic Property Get cmyM() As Byte
cmyM = 255 - G
End Property
Public Property Get cmykK() As Integer
cmykK = Minimum(255 - R, 255 - G, 255 - B) / 2.55
End PropertyPublic Property Get cmykC() As Integer
Dim MyR As Integer, Div As Integer
MyR = R / 2.55 Div = (100 - cmykK)
If Div = 0 Then Div = 1 cmykC = ((100 - MyR - cmykK) / Div) * 100
End PropertyPublic Property Get cmykM() As Integer
Dim MyG As Integer, Div As Integer
MyG = G / 2.55 Div = (100 - cmykK)
If Div = 0 Then Div = 1 cmykM = ((100 - MyG - cmykK) / Div) * 100
End PropertyPublic Property Get cmykY() As Integer
Dim MyB As Integer, Div As Integer
MyB = B / 2.55 Div = (100 - cmykK)
If Div = 0 Then Div = 1 cmykY = ((100 - MyB - cmykK) / Div) * 100
End PropertyPublic Property Get cmyY() As Byte
cmyY = 255 - B
End PropertyPublic Property Get hlsH() As Integer
Dim MyR As Single, MyG As Single, MyB As Single
Dim Max As Single, Min As Single
Dim Delta As Single, MyVal As Single MyR = R / 255: MyG = G / 255: MyB = B / 255 Max = Maximum(MyR, MyG, MyB)
Min = Minimum(MyR, MyG, MyB) If Max <> Min Then
Delta = Max - Min
Select Case Max
Case MyR
MyVal = (MyG - MyB) / Delta
Case MyG
MyVal = 2 + (MyB - MyR) / Delta
Case MyB
MyVal = 4 + (MyR - MyG) / Delta
End Select
End If MyVal = (MyVal + 1) * 60
If MyVal < 0 Then MyVal = MyVal + 360 hlsH = MyVal
Debug.Print hlsH
End Property
Public Property Get hlsL() As Integer
hlsL = ((Maximum(R, G, B) + Minimum(R, G, B)) / 2) / 2.55
End Property
Public Property Get hlsS() As Integer
Dim MyR As Single, MyG As Single, MyB As Single
Dim Max As Single, Min As Single, MyS As Single MyR = R / 255: MyG = G / 255: MyB = B / 255 Max = Maximum(MyR, MyG, MyB)
Min = Minimum(MyR, MyG, MyB) If Max <> Min Then
If hlsL <= 50 Then
MyS = (Max - Min) / (Max + Min)
Else
MyS = (Max - Min) / (2 - Max - Min)
End If
hlsS = MyS * 100
End If
End PropertyPrivate Function Minimum(ParamArray Vals())
Dim n As Integer, MinVal MinVal = Vals(0) For n = 0 To UBound(Vals)
If Vals(n) < MinVal Then MinVal = Vals(n)
Next n Minimum = MinVal
End Function
Private Function Maximum(ParamArray Vals())
Dim n As Integer, MaxVal For n = 0 To UBound(Vals)
If Vals(n) > MaxVal Then MaxVal = Vals(n)
Next n Maximum = MaxVal
End FunctionPublic Property Let rgbR(NewVal As Byte)
R = NewVal
End Property
Public Property Get rgbR() As Byte
rgbR = R
End PropertyPublic Property Get rgbG() As Byte
rgbG = G
End PropertyPublic Property Get rgbB() As Byte
rgbB = B
End Property
Public Property Get ycbcrY() As Byte
ycbcrY = R * 0.2989 + G * 0.5866 + B * 0.1145
End Property
Public Property Get ycbcrCb() As Byte
Dim MyCb As Integer
MyCb = -0.1687 * R - 0.3313 * G + 0.5 * B + 128 ycbcrCb = IIf(MyCb <= 255, MyCb, 255)
End Property
Public Property Get ycbcrCr() As Byte
Dim MyCr As Integer
MyCr = 0.5 * R - 0.4187 * G - 0.0813 * B + 128 ycbcrCr = IIf(MyCr <= 255, MyCr, 255)
End PropertyPublic Property Let rgbG(NewVal As Byte)
G = NewVal
End Property
Public Property Let rgbB(NewVal As Byte)
B = NewVal
End Property
Public Sub SetCMY(C As Integer, M As Integer, Y As Integer)
R = 255 - C
G = 255 - M
B = 255 - Y
End Sub
Public Sub SetHLS(H As Integer, L As Integer, S As Integer)
Dim MyR As Single, MyG As Single, MyB As Single
Dim MyH As Single, MyL As Single, MyS As Single
Dim Min As Single, Max As Single, Delta As Single MyH = (H / 60) - 1: MyL = L / 100: MyS = S / 100
If MyS = 0 Then
MyR = MyL: MyG = MyL: MyB = MyL
Else
If MyL <= 0.5 Then
Min = MyL * (1 - MyS)
Else
Min = MyL - MyS * (1 - MyL)
End If
Max = 2 * MyL - Min
Delta = Max - Min
Select Case MyH
Case Is < 1
MyR = Max
If MyH < 0 Then
MyG = Min
MyB = MyG - MyH * Delta
Else
MyB = Min
MyG = MyH * Delta + MyB
End If
Case Is < 3
MyG = Max
If MyH < 2 Then
MyB = Min
MyR = MyB - (MyH - 2) * Delta
Else
MyR = Min
MyB = (MyH - 2) * Delta + MyR
End If
Case Else
MyB = Max
If MyH < 4 Then
MyR = Min
MyG = MyR - (MyH - 4) * Delta
Else
MyG = Min
MyR = (MyH - 4) * Delta + MyG
End If
End Select
End If R = MyR * 255: G = MyG * 255: B = MyB * 255
End Sub
Public Sub SetCMYK(C As Integer, M As Integer, Y As Integer, K As Integer)
Dim MyC As Single, MyM As Single, MyY As Single, MyK As Single MyC = C / 100: MyM = M / 100: MyY = Y / 100: MyK = K / 100 R = (1 - (MyC * (1 - MyK) + MyK)) * 255
G = (1 - (MyM * (1 - MyK) + MyK)) * 255
B = (1 - (MyY * (1 - MyK) + MyK)) * 255
End SubPublic Sub SetYCbCr(Y As Integer, Cb As Integer, Cr As Integer)
Dim MyR As Integer, MyG As Integer, MyB As Integer MyR = Y + 1.402 * (Cr - 128)
MyG = Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128)
MyB = Y + 1.772 * (Cb - 128) If MyR > 255 Then MyR = 255
If MyG > 255 Then MyG = 255
If MyB > 255 Then MyB = 255 If MyR < 0 Then MyR = 0
If MyG < 0 Then MyG = 0
If MyB < 0 Then MyB = 0 R = MyR
G = MyG
B = MyB
End Sub
| HSB | CMYK
PhotoShop | H=300,S=75,B=100 |C=38,M=62,Y=0,K=1
你的程序 | H=0,S=100,B=63 | C=0,M=75,Y=0,K=0
以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2003-02-06 23:04:22
软件版本: 1.0.819
软件作者: Shawls
个人主页: Http://Shawls.Yeah.Net
E-Mail: [email protected]
QQ: 9181729
主 题: 例子代码:颜色转换函数(RGB、HSB、CMYK、Lab)
作 者: enmity (灵感之源)
等 级:
信 誉 值: 100
所属论坛: VB 基础类
问题点数: 50
回复次数: 15
发表时间: 2002-3-29 13:47:37 回复人: zyl910(910:分儿,我来了!) ( ) 信誉:166 2002-3-29 22:05:26 得分:14
与PhotoShop算的不一样!
比如:R=255,G=64,B=255 | HSB | CMYK
PhotoShop | H=300,S=75,B=100 |C=38,M=62,Y=0,K=1
你的程序 | H=0,S=100,B=63 | C=0,M=75,Y=0,K=0
Top
回复人: zyl910(910:分儿,我来了!) ( ) 信誉:166 2002-3-29 22:07:26 得分:0
我见过许多这样的算法,算得结果跟你的差不多,就是与 颜色对话框、PhotoShop 算的不一样
Top
较准确的公式为:
(1)RGB -> CMYK
K=Min(1-R,1-G,1-B) //取最小值
C=(1-R-K)/(1-K)
M=(1-G-K)/(1-K)
Y=(1-B-K)/(1-K)(2)CMYK->RGB
R=(1-K)*(1-C)
G=(1-K)*(1-M)
B=(1-K)*(1-Y)说明:
(1)RGB,CMYK范围:0.0-1.0,不再此范围内时需转化。
如RGB范围:0-255,则除以255.0,
如:红Red 255,则R=255/255.0,如某色为Red 128,Green 100,Blue 50,则R=128/255.0,G=100/255.0,B=50/255.0
CMYK范围:0-100,则除以100.0)(2)经过上面公式计算出来的结果仍与Photoshop有差异(事实上,不同版本的Photoshop的RGB与CMYK之间的转换也有差异),这是由于Photoshop是根据印刷环境(油墨、印刷纸张、印刷工厂的环境等)的不同而作了ICC文件修正的。更多细节(参看:http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html)
这个网址我可是找了近20小时!缘由:
我本想在C#环境下开发印刷格式的300DPI CMYK TIF图像转化为RGB 72DPI 的JPG格式图像,虽然找到了公式,但仍无法得到满意的答案(主要原因是:CMYK图像读入后转RGB时严重变色,主要是变暗)。不知各位有否更好的解决方案?(强烈建议研究成果共享!)
CMYK是普遍的彩印用色,包括品红,亮蓝,黑,黄四种.有时间给你研究一下.
我所说的所谓“公式”是指的目前计算机程序处理时的较常用的转换公式(包括UNIX、Linux、Windows下)均用它们。
正如你所说,由于CMYK是基于减色效应原理,所以较为专业的软件如Photoshop均是采用ICC的映射文件而非简单的计算公式。
对于我们编程而言,上述的简单公式基本已经够用。谁有办法在C#读取中读取CMYK图像某像素的CMYK值啊?