Q: 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.

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 of the RGB values are all 0, then the CMY values are all 0 and the K value is 1.



#include<stdio.h>
int main()
{
    float r, g, b, rf, gf, bf, max, w, c, y, m , k;         /*k stands for black*/

    printf("Enter the value of Red(0 to 255): ");
    scanf("%f", &r);

    printf("Enter the value of Green(0 to 255): ");
    scanf("%f", &g);

    printf("Enter the value of Blue(0 to 255): ");
    scanf("%f", &b);

    rf = r/255;
    gf = g/255;
    bf = b/255;

    printf("\nRed: %f\nGreen: %f\nBlue: %f\n", rf, gf, bf);

    /*find maximum amoung all of them*/

    max = rf;
    if (max<gf)
        max = gf;
    if (max<bf)
        max = bf;

    /*w stands for white*/
    w = max;
    printf("White: %f\n\n", w);
    c = (w-rf)/w;
    m = (w-gf)/w;
    y = (w-bf)/w;

    k = 1- w;

    printf("The value of Cyan: %f\n", c);
    printf("The value of Magenta: %f\n", m);
    printf("The value of Yellow: %f\n", y);
    printf("The value of Black: %f\n", k);

    return 0;
}

-----------------------------------------
Output:

Enter the value of Red(0 to 255): 20
Enter the value of Green(0 to 255): 30
Enter the value of Blue(0 to 255): 60

Red: 0.078431
Green: 0.117647
Blue: 0.235294
White: 0.235294

The value of Cyan: 0.666667
The value of Magenta: 0.500000
The value of Yellow: 0.000000
The value of Black: 0.764706

Comments

  1. These blogs are amazing and highly helpful for the readers coming here. I have read all of these published articles and found them great thank you so much.
    Custom Logo Designing Canada

    ReplyDelete

Post a Comment

Popular posts from this blog

Q: The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate area & perimeter of the rectangle and area & circumference of the circle.

Q: If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)