luc020.c (1921B)
1 /* In digital world colors are specified in Red-Green-Blue (RGB) format, 2 with values of R, G, B varying on an integer scale from 0 to 255. In print 3 publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, 4 with values of C, M, Y, and K varying on a real scale from 0.0 to 1.0. 5 Write a program that converts RGB color to CMYK color as per the following formulae: 6 White = Max(Red/255, Green/255, Blue/255) 7 Cyan = (White-Red/255) / White 8 Magenta = (White-Green/255) / White 9 Yellow = (White-Blue/255) / White 10 Black = 1 - White 11 Note that if the RGB values are all 0, then the CMY values are all 0 and the K value is 1. */ 12 /* Let Us C, Chap- 4, Page - 71, Qn No.: D(b) */ 13 14 #include <stdio.h> 15 16 // declaring function 17 double get_white(double red, double green, double blue) 18 { 19 double max; 20 max = red / 255; 21 if (max < (green / 255)) 22 max = green / 255; 23 if (max < (blue / 255)) 24 max = blue / 255; 25 return max; 26 } 27 28 int main() 29 { 30 double r, g, b, w, c = 0, m = 0, y = 0, k = 0; 31 printf("Enter the RGB color code in 'R G B' format : "); 32 scanf("%lf %lf %lf", &r, &g, &b); 33 34 // checking for invalid input (negetive input) 35 if (r < 0 || g < 0 || b < 0) 36 { 37 printf("\nRGB color code can not be a negetive number."); 38 return 1; 39 } 40 41 // checking for invalid input (out of range color code) 42 if (r > 255 || g > 255 || b > 255) 43 { 44 printf("\nRGB color code can be maximum (255, 255, 255)."); 45 return 1; 46 } 47 48 // converting RGB color code to CMYK color code 49 if (r == 0 && g == 0 && b == 0) 50 k = 1; 51 else 52 { 53 w = get_white(r, g, b); 54 c = (w - (r / 255)) / w; 55 m = (w - (g / 255)) / w; 56 y = (w - (b / 255)) / w; 57 k = 1 - w; 58 } 59 60 printf("\nRGB color (%g, %g, %g) equivalent to CMYK color (%g, %g, %g, %g).", r, g, b, c, m, y, k); 61 return 0; 62 }