VB中如何将RGB颜色与CMYK颜色互转,能否给出一个转换函数。谢谢!
网上有几个函数的例子,可是都看不懂,高手能否讲解一下。谢谢!!!!!!相关代码如下:
============================================================================================(代码1)为了说明 adobe 的 RGB 转换 CMYK 的过程,不得不先涉及到一些色彩转换程序方面的东西。
CMYK_ADOBE_COLORSPACE:
r = (k*c)/maxval;
g = (k*m)/maxval;
b = (k*y)/maxval;
以上是 adobe 的 CMYK 到 RGB转换公式,下面再来看看 RGB 如何 转换为 CMYK
下面的语句是经过改写的适用于 Flash 的 actionscript// RGB to CMY 色彩转换
function rgbtocmy () {
mycyan = 255 - myred;
mymagenta = 255 - mygreen;
myyellow = 255 - myblue;
correctCMYK();
}
// CMY to RGB 色彩转换
function cmytorgb () {
myred = 255 - mycyan;
mygreen = 255 - mymagenta;
myblue = 255 - myyellow;
correctCMYK();
}
// CMYK 色彩修正
function correctCMYK () {
if (rep_v != "ERR") {
var temp = Math.min(Math.min(mycyan, mymagenta), myyellow);
if (temp != 0) {
temp2 = Math.round((rep_v / 100) * temp);
rep_k = Math.round((temp2 / 255) * 100);
rep_c = Math.round(((mycyan - temp2) / 255) * 100);
rep_m = Math.round(((mymagenta - temp2) / 255) * 100);
rep_y = Math.round(((myyellow - temp2) / 255) * 100);
} else {
rep_c = Math.round((mycyan / 255) * 100);
rep_m = Math.round((mymagenta / 255) * 100);
rep_y = Math.round((myyellow / 255) * 100);
rep_k = 0;
}
}
} =================================================================================代码1里面用的是FLASH宏的代码,大概意思看得懂,但是这里的 if (rep_v != "ERR") 却不是很明白rep_v 是常数还是什么,在网上也找不到资料。
网上有几个函数的例子,可是都看不懂,高手能否讲解一下。谢谢!!!!!!相关代码如下:
============================================================================================(代码1)为了说明 adobe 的 RGB 转换 CMYK 的过程,不得不先涉及到一些色彩转换程序方面的东西。
CMYK_ADOBE_COLORSPACE:
r = (k*c)/maxval;
g = (k*m)/maxval;
b = (k*y)/maxval;
以上是 adobe 的 CMYK 到 RGB转换公式,下面再来看看 RGB 如何 转换为 CMYK
下面的语句是经过改写的适用于 Flash 的 actionscript// RGB to CMY 色彩转换
function rgbtocmy () {
mycyan = 255 - myred;
mymagenta = 255 - mygreen;
myyellow = 255 - myblue;
correctCMYK();
}
// CMY to RGB 色彩转换
function cmytorgb () {
myred = 255 - mycyan;
mygreen = 255 - mymagenta;
myblue = 255 - myyellow;
correctCMYK();
}
// CMYK 色彩修正
function correctCMYK () {
if (rep_v != "ERR") {
var temp = Math.min(Math.min(mycyan, mymagenta), myyellow);
if (temp != 0) {
temp2 = Math.round((rep_v / 100) * temp);
rep_k = Math.round((temp2 / 255) * 100);
rep_c = Math.round(((mycyan - temp2) / 255) * 100);
rep_m = Math.round(((mymagenta - temp2) / 255) * 100);
rep_y = Math.round(((myyellow - temp2) / 255) * 100);
} else {
rep_c = Math.round((mycyan / 255) * 100);
rep_m = Math.round((mymagenta / 255) * 100);
rep_y = Math.round((myyellow / 255) * 100);
rep_k = 0;
}
}
} =================================================================================代码1里面用的是FLASH宏的代码,大概意思看得懂,但是这里的 if (rep_v != "ERR") 却不是很明白rep_v 是常数还是什么,在网上也找不到资料。
Public 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 Property Public 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 Property
Public 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 Property
Public 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 Property
Public Property Get cmyY() As Byte cmyY = 255 - B End Property Public 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 Property
Private 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 Function
Public Property Let rgbR(NewVal As Byte) R = NewVal End Property Public Property Get rgbR() As Byte rgbR = R End Property
Public Property Get rgbG() As Byte rgbG = G End Property
Public 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 Property
Public 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 Sub
Public 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
代码2
Option Explicit
Private R As Byte
Private G As Byte
Private B As Byte Public Property Get cmyC() As Byte
cmyC = 255 - R
End Property
ublic 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 Property Public 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 Property Public 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 Property Public 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 Property Public Property Get cmyY() As Byte
cmyY = 255 - B
End Property Public 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 Property Private 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 Function Public Property Let rgbR(NewVal As Byte)
R = NewVal
End Property Public Property Get rgbR() As Byte
rgbR = R
End Property Public Property Get rgbG() As Byte
rgbG = G
End Property Public 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 Property Public 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 Sub Public 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
RGB转CMYK是很简单的:
CYMK一般是使用在冲印和印刷中的
C: 青色
Y: 黄色
M: 品色
K: 黑色
CYMK和RGB的关系是这样的:
白色 W = RGB(255,255,255)
C = W - R
Y = W - B
M = W - G
K = RGB(0,0,0)或者:
C = B + G
Y = R + G
M = R + B可以看出,R,G,B如果都为255的话,混在一起是白色,因此在冲印中RGB色系称为加色法,越大越亮
而C,M,Y都为255,混在一起是黑色,因此称为减色法,越大越黑在程序里取得一个点的RGB值之后,
将其红色和蓝色值相加,得到 M,
红色和绿色相加,得到 黄色 Y,
绿色加蓝色相加,得到 青色 C现在楼主明白了吧