Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- using namespace std;
- void readRGB(int &, int &, int &); //Function Definitions
- void RGBtoCMYK(int, int, int, float &, float &, float &, float &);
- ifstream infile;
- int main()
- {
- int r, g, b;
- float c, m, y, k;
- float blue, green, red;
- ifstream infile("c:/colorsdot.txt"); //Reading from file
- if (!infile.is_open()) { // Condition if the file is not open
- cout << "Error. Unable to open file.";
- system ("PAUSE");
- exit(1);
- }
- ofstream outfile("cmyk.txt");
- cout.setf(ios::fixed,ios::floatfield);
- cout.precision(1);
- //infile >> red >> green >> blue;
- //readRGB(r, g, b);
- //infile >> red >> green >> blue;
- //readRGB(r, g, b);
- RGBtoCMYK( r, g, b, y, m, c, k); // Calling the RGBtoCMYK
- //function to caclulate the cmyk values
- outfile << y << " " << m << " " << c << " " << k << endl;
- cout << y << " " << m << " " << c << " " << k << endl;
- //readRGB(r, g, b);
- infile.close(); //closing the input file
- outfile.close(); // closing the output file
- system("PAUSE");
- return EXIT_SUCCESS;
- }
- void readRGB(int &r, int &g, int &b){
- int red, green, blue;
- infile >> red;
- infile >> green;
- infile >> blue;
- if (red >= 0 && red <= 255)
- r = red;
- else cout << "Error invalid input" << endl;
- if (green >= 0 && green <= 255)
- g = green;
- else cout << "Error invalid input" << endl;
- if (blue >= 0 && blue <= 255)
- b = blue;
- else cout << "Error invalid input" << endl;
- } //end of function readRGB
- int largest(int x, int y, int z)
- {
- if (x > y && x > z)
- return x;
- else if (y > z)
- return y;
- else
- return z;
- }
- void RGBtoCMYK(int r, int g, int b, double &c, double &m, double &y, double &k) // function RGBtoCMYK(r, g, b, c, m, y, k) that receives RGB values as input parameters, and returns CMYK values as reference parameters
- {
- double w;
- if(r == 0 && g == 0 && b == 0) { // If the RGB values are all 0 then the CMY values are all 0 and the K value is 1
- c = 0;
- m = 0;
- y = 0;
- k = 1;
- }
- else {
- w = largest(r / 255.0, g / 255.0, b / 255.0);
- c = (w - (r / 255.0)) / w;
- m = (w - (g / 255.0)) / w;
- y = (w - (b / 255.0)) / w;
- k = 1.0 - w;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement