Example 19: RGB to CMYK Color Conversion

Learn how to convert RGB color to CMYK color.

Problem

In the digital world, colours are specified in Red-Green-Blue (RGB) format, with R, G, and B values varying on an integer scale from 0 to 255. On the other hand, in print publishing, the colours are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with C, M, Y, and K values varying on a real scale from 0.0 to 1.0.

Write a program that converts RGB color to CMYK color as per the following formulae:

White=Max(Red255,Green255,Blue255)White=Max(\frac{Red}{255},\frac{Green}{255},\frac{Blue}{255})

Cyan=(WhiteRed/255White)Cyan=(\frac{White-Red/255}{White})

Magenta=(WhiteGreen/255White)Magenta=(\frac{White-Green/255}{White})

Yellow=(WhiteBlue/255White)Yellow=(\frac{White-Blue/255}{White})

Black=1WhiteBlack=1-White

Remember to print the output on the console in the following manner:
  • Invalid RGB Values
  • CMYK = cyan, magenta, yellow, black

Note: If the RGB values are all 0, then the CMY values are all 0, and the K value is 1.

Get hands-on with 1400+ tech skills courses.