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