Hey guys,
So I really need help writing a c++ program that is supposed to read in values for Red Green and Blue colors between 0 and 255, and then use a different function to convert them to Cyan Magenta Yellow and blacK color using set formulas.

1. Write a function that reads an RGB value and rejects invalid values.
2. Write a function max(a, b, c) that returns the maximum of its three parameters.
3. Write a function RGBtoCMYK(r, g, b, c, m, y, k) that receives RGB values as input parameters, and returns CMYK values as reference parameters.

So far my code reads in the numbers from the array but I cant figure out how to use the given formulas to change the data from RGB colors to CMYK colors.

Any kind of help is appreciated.
Thanks a lot.
Comment if you have any questions.

/*
Name: Colors
Author:
Date:
Description:
*/

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

ifstream infile ("h.txt");

void readRGB(int r,int g,int b)
{
 int red[9];

 for (r=0; r <9; r++)



    while (!infile.eof() && infile >> red[r]
)

 cout << red[r] << endl;
}
int max (int a, int b, int c)
{
  int max = a;

   if (b > max)
   max = b;

   else if (c > max)
   max = c;

 return max;
}

void RGBtoCMYK (int r, int g, int b, int c, int m , int y, int k)
{
    int w;
  if (r == 0 && g == 0 && b == 0)  {
   c = 0;
   m = 0;
   y = 0;
   k = 1;
   cout << c << m << y << k << endl;
}
else  {
w = max (r/255,g/255,b/255);
c = (w-(r/255))/ w;
m = (w-(g/255)) /w;
y = (w-(b/255)) / w;
k = 1 - w;
cout << c << m << y << k << endl;
}
}
int main ()
{
 int r,b,g;
 float c, m, y, k;
 if ( !infile.is_open() ) {
        cout << "ERROR: Unable to open file" << endl;
        exit(1);
    }

    readRGB(r,g,b);
    RGBtoCMYK(r, g, b, c,m, y, k);


}

Recommended Answers

All 4 Replies

Maybe this can help, here is a quick snippet to convert CMYK to RGB. If you take it apart and use this logic backwards then you'll get a decent RGB TO CMYK.

c = 10;
m = 20;
y = 100;
k = 31;

/* 
rgb result from above cmyk will be:
r = 158.355 
g = 140.76 
b = 0
*/

k = k / 100;

// Get the rgb as floats (0 to 255)
r = (1 - min((c / 100) * (1 - k) + 1 * k, 1) ) * 255;
g = (1 - min((m / 100) * (1 - k) + 1 * k, 1) ) * 255;
b = (1 - min((y / 100) * (1 - k) + 1 * k, 1) ) * 255;

Your max funciton is incorrect, you have to test all three arguments, becouse you never know which one is the maximum. You test c only if b<a which is wrong.

else if (c > max)
should be
if(c > max)

commented: I doubt that infinityblade is working on the same project. +0

Never tested this but the formula seems correct :S
RGB to CMY: http://www.easyrgb.com/index.php?X=MATH&H=11#text11
CMY to CMYK: http://www.easyrgb.com/index.php?X=MATH&H=13#text13

EDIT: Oh wow.. didn't realize this was a grave dig -_-

void RGBToCMYK(int R, int G, int B, int &C, int &M, int &Y, int &K)
{
    C = 1 - (R / 255);
    M = 1 - (G / 255);
    Y = 1 - (B / 255);

    int TempK = 1;

    if (C < TempK) TempK = C;
    if (M < TempK) TempK = M;
    if (Y < TempK) TempK = Y;
    if (TempK == 1)
    {
        C = M = Y = 0;
    }
    else
    {
        C = (C - TempK) / (1 - TempK);
        M = (M - TempK) / (1 - TempK);
        Y = (Y - TempK) / (1 - TempK);
    }
    K = TempK;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.