C Program To Convert (R, G, B) To (C, M, Y, K) Color Format


In digital world colors are specified in Red-Green-Blue (RGB) format, with values of R, G, B varying on an integer scale from 0 to 255. In print publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with values of C, M, Y and K varying on a real scale from 0.0 to 1.0. Write a C program that converts RGB color to CMYK color as per the following formulae:

White = Max(Red/255, Green/255, Blue/255);
Cyan = (White – Red/255) / White;
Magenta = (White – Green/255) / White;
Yellow = ( White – Blue/255) / White;
Black = 1-White

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

Related Read:
if else statement in C
Relational Operators In C
Logical Operators In C

Expected Output for the Input

User Input:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
41
14
41

Output:
CMYK = (0.000000, 0.658537, 0.000000, 0.839216)

Video Tutorial: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format



YouTube Link: https://www.youtube.com/watch?v=JgXAyBP8w8w [Watch the Video In Full Screen.]

Source Code: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     float red, green, blue;  
  6.     float white, cyan, magenta, yellow, black;  
  7.     float max;  
  8.   
  9.     printf("Enter values for Red, Green and Blue(RGB) in Range 0 - 255\n");  
  10.     scanf("%f%f%f", &red, &green, &blue);  
  11.   
  12.     if(red == 0 && green == 0 && blue == 0)  
  13.     {  
  14.         cyan   = magenta = yellow = 0;  
  15.         black  = 1;  
  16.         black  = 1;  
  17.     }  
  18.     else  
  19.     {  
  20.         red    = red   / 255;  
  21.         green  = green / 255;  
  22.         blue   = blue  / 255;  
  23.   
  24.         max    = red;  
  25.         if(green > max)  
  26.         {  
  27.             max = green;  
  28.         }  
  29.   
  30.         if(blue > max)  
  31.         {  
  32.             max = blue;  
  33.         }  
  34.   
  35.         white   = max;  
  36.   
  37.         cyan    = (white - red)  / white;  
  38.         magenta = (white - green)/ white;  
  39.         yellow  = (white - blue) / white;  
  40.   
  41.         black   = 1 - white;  
  42.   
  43.     }  
  44.   
  45.     printf("CMYK = (%f, %f, %f, %f)\n",  
  46.             cyan, magenta, yellow, black);  
  47.   
  48.     return 0;  
  49. }  

Output 1:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
255
255
255
CMYK = (0.000000, 0.000000, 0.000000, 0.000000)

Output 2:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
0
0
0
CMYK = (0.000000, 0.000000, 0.000000, 1.000000)

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *