Routines (alphabetical) > Routines: C > CMYK_CONVERT

# CMYK_CONVERT

The CMYK_CONVERT procedure converts from the CMYK (cyan-magenta-yellow-black) color model to RGB (red-green-blue) and vice versa.

The procedure uses the following method to convert from CMYK to RGB:

R = (255 - C) (1 - K/255)

G = (255 - M) (1 - K/255)

B = (255 - Y) (1 - K/255)

To convert from RGB to CMYK, the procedure uses the following method:

K = minimum of (R, G, B)

C = 255 [1 - R/(255 - K)] (if K=255 then C=0)

M = 255 [1 - G/(255 - K)] (if K=255 then M=0)

Y = 255 [1 - B/(255 - K)] (if K=255 then Y=0)

In both cases the CMYK and RGB values are assumed to be in the range 0 to 255.

Note: There is no single method that is used for CMYK/RGB conversion. The method used by CMYK_CONVERT is the simplest and, depending on printing inks and screen colors, might not be optimal in all situations.

This routine is written in the IDL language. Its source code can be found in the file `cmyk_convert.pro` in the `lib` subdirectory of the IDL distribution.

## Example

This example converts an image file’s RGB data to CMYK, displays the image using different color tables, converts CMYK back to RGB, and displays the image again.

file = FILEPATH( 'rose.jpg', SUBDIRECTORY=['examples','data'] )
READ_JPEG, file, image
red = REFORM( image[0,*,*] )
green = REFORM( image[1,*,*] )
blue = REFORM( image[2,*,*] )
; Convert from RGB to CMYK
CMYK_CONVERT, c, m, y, k, red, green, blue, /TO_CMYK
; Display using cyan (green + blue) color table
IIMAGE, GREEN=c, BLUE=c, VIEW_GRID=[2,3], DIM=[600,800]
; Display using magenta (red + blue) color table
IIMAGE, RED=m, BLUE=m, /VIEW_NEXT
; Display using yellow (red + green) color table
IIMAGE, RED=y, GREEN=y, /VIEW_NEXT
; Display using inverted grayscale (like ink)
IIMAGE, 255b-k, /VIEW_NEXT
; Convert from CMYK back to RGB
CMYK_CONVERT, c, m, y, k, r, g, b
IIMAGE, image, /VIEW_NEXT
IIMAGE, RED=r, GREEN=g, BLUE=b, /VIEW_NEXT

## Syntax

CMYK_CONVERT, C, M, Y, K, R, G, B [, /TO_CMYK]

## Arguments

### C, M, Y, K

To convert from CMYK to RGB, set these arguments to scalars or arrays containing the CMYK values in the range 0-255. To convert from RGB to CMYK (with the TO_CMYK keyword set), set these arguments to named variables that will contain the converted values.

### R, G, B

To convert from CMYK to RGB, set these arguments to named variables that will contain the converted values. To convert from RGB to CMYK (with the TO_CMYK keyword set), set these arguments to scalars or arrays containing the RGB values.

## Keywords

### TO_CMYK

If this keyword is set, the values contained in the RGB arguments are converted to CMYK. The default is to convert from CMYK to RGB.

## Version History

6.2 | Introduced

## See Also

COLOR_QUAN