English

How To: Convert color values between the RGB and CMYK color models using Avenue?

Procedure

Summary

The following scripts are an example of using Avenue to convert color values between the RGB and CMYK color models.

Procedure

RGB To CMYK

Copy and paste the following script into a script editor and name it 'Call CMYK'.

Code:

'--------------------------------------------------------------
' Example of how to call the script "aColor.CMYK_To_RGB" as
'

' Get the CMYK values from the user
'
cmykStringList = MsgBox.MultiInput(
"Enter the CMYK value:",
"CMYK to RGB conversion",
{"C", "M", "Y", "K"},
{} )
' Convert the strings to numbers
'
cmykNumberList = {}
for each item in cmykStringList
if (item.IsNumber) then
cmykNumberList.Add(item.AsNumber)
else
MsgBox.Error("Entries must all be numbers","")
exit
end
end
' Call the script to do the conversion
'
theReturn = av.Run("aColor.CMYK_To_RGB", cmykNumberList)
if (theReturn.Is(string)) then
' There was an error!
MsgBox.Error(theReturn,"")
exit
end

R = theReturn.Get(0)
G = theReturn.Get(1)
B = theReturn.Get(2)

MsgBox.Info(
"CMKY: " +
cmykStringList.Get(0).AsString ++
cmykStringList.Get(1).AsString ++
cmykStringList.Get(2).AsString ++
cmykStringList.Get(3).AsString + NL +
"RGB: " +
R.SetFormat("d").AsString ++
G.SetFormat("d").AsString ++
B.SetFormat("d").AsString, "")

'--------------------------------------------------------------
' End Script


Copy and paste the following script into a script editor and name it 'aColor.CMYK_To_RGB'.
Code:

'--------------------------------------------------------------
'' @script aColor.CMYK_To_RGB
'
'' @fileName cmyk2rgb.ave
'
'' @precis This script will convert color values in
'' CMYK to the RGB color model. Both models are based
'' on the values obtained from Arc/Info SHOW and COLOREDIT
'
'' @description This script will convert Cyan, Magenta, Yellow, Black
'' values into the Red, Green, Blue color model. The algorithm
'' for this script was sourced from "Computer Graphics Principals
'' and Practice" (full reference show below). There is a slight
'' variation between the model in the book and the model used
'' by Arc/Info, so I have adopted the A/I model and noted the
'' variations. From these algorithms, it would be possible to create
'' a dialog similar to the COLOREDIT dialog in Arc/Info.
'
'' The CMYK values expected as input must be in the range [0,100]
'' The RGB values are all output in the range [0,255]
'
' -----------------------------------------------------------------
'' @note History: Mark Chilcott - December 1998
'
'' @note Reference: The algorithm for this script was sourced from:
'' Computer Graphics - Principles and Practice
'' Second Edition
'' Foley, van Dam, Feiner and Hughes
'' © Addison-Wesley Publishing Company, Inc
'
'
'' @note This script was tested for the "Standard" model against a
'' number of ranges, in ARCPLOT (eg, ArcPlot > show cmyk rgb 255 30 60)
'' with the correct results being observed. It was also tested against
'' ArcPlot COLOREDIT.
''
'' @note The algorithm in the book has a RGB range of [0,1] while
'' A/I and ArcView use a range of [0,255]. Simply divide the RGB
'' values by 255 and use the equations to obtain the correct result.
'
'' @note The CMYK values in A/I are in the range [0,100] while the
'' ones in the book are in the range [0,1]. Simply multiply by
' 100 to get the A/I results.
'
'' @note In COLOREDIT in A/I you will note that by changing the
'' RGB or HSV values will not change the K value. This is reflected
'' in this script. Note that the SHOW command in A/I also displays
'' this behaviour. Note also, when the K (black) values is moved in
'' COLOREDIT, the RGB and HSV values will be affected. This is also
'' reflected in this script.
'
' -----------------------------------------------------------------

' Get the CMYK values ...
'
'' @note The parameter list MUST be a list
'' of 4 numbers in the range [0,100]
'

paramList = SELF

' Make sure there are 4 entries in the list, they are
' all numbers, and they are in the correct range. If not
' then return an appropriate error message.
'

if (paramList.Count <> 4) then
return("Error: Incorrect number of parameters")
end

for each item in paramList
if (item.Is(Number).Not) then
return("Error: At least one param not a number")
end

if ( (item < 0) or (item > 100) ) then
return("Error: At least one param was out of range [0,100]")
end

end
C = paramList.Get(0)
M = paramList.Get(1)
Y = paramList.Get(2)
K = paramlist.Get(3)

' The standard RGB color model in the book uses the range [0,1] while
' ArcView stores the values in the range [0,255], so have to convert
'
' Following from the book
' Given r,g,b each in range [0-1]
' Desired:
' c in [0,100]
' m in [0,100]
' y in [0,100]
' k in [0,100]

'' For the CMK model:
'' C = 1 - R
'' M = 1 - G
'' Y = 1 - B

'' For the CMYK model:
'' K = min(C,M,Y)
'' C = C - K
'' M = M - K
'' Y = Y - K

' Calculate the Value
' See note for explanation of variation to
' book formula to meet what A/I does. Therefore,
' from the above:
' R = (1 - [(C + K)/100]) * 255
' G = (1 - [(M + K)/100]) * 255
' B = (1 - [(Y + K)/100]) * 255
' If the value of (C+K), (M+K) or (Y+K) exceeds 100
' (which is higher than it should be) then the corresponding
' R, G or B value will become 0, and the calculation of the
' remaining values calculated on the above formula, but using
' the value of K as the difference between max(C,M,Y) and 100
' in the above formula. This is the way COLOREDIT seems to work.
' If say C=M=max and C+K > 100, then both R and G would become 0.

if ( ((C + K) > 100) or
((M + K) > 100) or
((Y + K) > 100) ) then
' Initialise the RGB values
R = -99
G = -99
B = -99

' Find the max of (C,M,Y)
theMax = C.Max(M)
theMax = theMax.Max(Y)

' Set the value of RGB to 0 if it
' is the max value.
if (theMax = C) then
R = 0
end
if (theMax = M) then
G = 0
end
if (theMax = K) then
Y = 0
end

' Find the new value for K
newK = 100 - theMax

' Calculate RGB if not already set to 0
if (R <> 0) then
R = (1 - ((C + newK)/100)) * 255
end
if (G <> 0) then
G = (1 - ((M + newK)/100)) * 255
end
if (B <> 0) then
B = (1 - ((Y + newK)/100)) * 255
end

else
R = (1 - ((C + K)/100)) * 255
G = (1 - ((M + K)/100)) * 255
B = (1 - ((Y + K)/100)) * 255
end


returnList = {R, G, B}

return returnList

' -------------------------------------------------------------------
' End Script

Compile both scripts. Run the 'Call CMYK' script. It asks to input RGB values. This script will then pass these values in a list, as the SELF object to the called script, 'aColor.CMYK_To_RGB'. This script then returns the CMYK values back to the calling script and the values are displayed in a message box.

CMYK to RGB

Copy and paste the following script into a script editor and name it 'Call RGB'.

Code:

' -------------------------------------------------------------------
' Example of how to call the script "aColor.RBG_To_CMYK" as
'

' Get the RGB values from the user
'
rgbStringList = MsgBox.MultiInput(
"Enter the RGB value:",
"RGB to CMYK conversion",
{"R", "G", "B"},
{} )
' Convert the strings to numbers
'
rgbNumberList = {}
for each item in rgbStringList
if (item.IsNumber) then
rgbNumberList.Add(item.AsNumber)
else
MsgBox.Error("Entries must all be numbers","")
exit
end
end
' Call the script to do the conversion
'
theReturn = av.Run("aColor.RGB_To_CMYK", rgbNumberList)
if (theReturn.Is(string)) then
' There was an error!
MsgBox.Error(theReturn,"")
exit
end
C = theReturn.Get(0)
M = theReturn.Get(1)
Y = theReturn.Get(2)
K = theReturn.Get(3)

MsgBox.Info(
"RGB: " +
rgbStringList.Get(0).AsString ++
rgbStringList.Get(1).AsString ++
rgbStringList.Get(2).AsString + NL +
"CMKY: " +
C.SetFormat("d").AsString ++
M.SetFormat("d").AsString ++
Y.SetFormat("d").AsString ++
K.SetFormat("d").AsString, "")
' -------------------------------------------------------------------
' End Script

Copy and paste the following script into a script editor and name it 'aColor.RGB_To_CMYK'.

Code:

' -------------------------------------------------------------------
'' @script aColor.RGB_To_CMYK
'
'' @fileName rgb2cmyk.ave
'
'' @precis This script will convert color values in
'' RGB to the CMY and CMYK color model.
'
'' @description This script will convert Red, Green, Blue color
'' values into the Cyan, Magenta, Yellow, Black model. The algorithm
'' for this script was sourced from "Computer Graphics Principals
'' and Practice" (full reference show below). There is a slight
'' variation between the model in the book and the model used
'' by Arc/Info, so I have adopted the A/I model and noted the
'' variations. From these algorithms, it would be possible to create
'' a dialog similar to the COLOREDIT dialog in Arc/Info.
'
'' The RGB values expected as input must be in the range [0,255]
'' The CMYK values are all output in the range [0,100]
'
' -----------------------------------------------------------------
'' @note History: Mark Chilcott - December 1998
'
'' @note Reference: The algorithm for this script was sourced from:

'' Computer Graphics - Principles and Practice
'' Second Edition
'' Foley, van Dam, Feiner and Hughes
'' © Addison-Wesley Publishing Company, Inc
'
'
'' @note This script was tested for the "Standard" model against a
'' number of ranges, in ARCPLOT (eg, ArcPlot > show cmyk rgb 255 30 60)
'' with the correct results being observed. It was also tested against
'' ArcPlot COLOREDIT.
''
'' @note The algorithm in the book has a RGB range of [0,1] while
'' A/I and ArcView use a range of [0,255]. Simply divide the RGB
'' values by 255 and use the equations to obtain the correct result.
'
'' @note The CMYK values in A/I are in the range [0,100] while the
'' ones in the book are in the range [0,1]. Simply multiply by
' 100 to get the A/I results.
'
'' @note In COLOREDIT in A/I you will note that by changing the
'' RGB or HSV values will not change the K value. This is reflected
'' in this script. Note that the SHOW command in A/I also displays
'' this behaviour. Note also, when the K (black) values is moved in
'' COLOREDIT, the RGB and HSV values will be affected. This is also
'' reflected in this script.
'
' -----------------------------------------------------------------
' Get the RGB values ...
'
'' @note The parameter list MUST be a list
'' of 3 numbers in the range [0,255]
'
paramList = SELF

' Make sure there are 3 entries in the list, they are
' all numbers, and they are in the correct range. If not
' then return an appropriate error message.
'

if (paramList.Count <> 3) then
return("Error: Incorrect number of parameters")
end

for each item in paramList
if (item.Is(Number).Not) then
return("Error: At least one param not a number")
end

if ( (item < 0) or (item > 255) ) then
return("Error: At least one param was out of range [0,255]")
end
end
R = paramList.Get(0)
G = paramList.Get(1)
B = paramList.Get(2)
' Convert the RGB values from the 255 to a fraction in the range [0,1]
' The standard color model in the book uses the range [0,1] while
' ArcView stores the values in the range [0,255], so have to convert to
' the fraction for the formula to work.
'
redValue = R/255
greenValue = G/255
blueValue = B/255

' Following from the book
' Given r,g,b each in range [0-1]
' Desired:
' c in [0,100]
' m in [0,100]
' y in [0,100]
' k in [0,100]

'' For the CMK model:
'' C = 1 - R
'' M = 1 - G
'' Y = 1 - B

'' For the CMYK model:
'' K = min(C,M,Y)
'' C = C - K
'' M = M - K
'' Y = Y - K
' Calculate the Value
' See note for explanation of variation to
' book formula to meet what A/I does
cyanValue = 100*(1 - redValue)
magentaValue = 100*(1 - greenValue)
yellowValue = 100*(1 - blueValue)
blackValue = 0

returnList = {cyanValue, magentaValue, yellowValue, blackValue}

return returnList

' -------------------------------------------------------------------
' End Script


Compile both scripts. Run the 'Call RGB' script. It asks to input CMYK values. This script then passes these values in a list, as the SELF object to the called script, 'aColor.RGB_To_CMYK'. This script returns the RGB values back to the calling script and the values is displayed in a message box.

Related Information