Advertisement
Guest User

555

a guest
Apr 29th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void readRGB(int &, int &, int &); //Function Definitions
  9. void RGBtoCMYK(int, int, int, float &, float &, float &, float &);
  10. ifstream infile;
  11.  
  12. int main()
  13.  
  14. {
  15.  
  16. int r, g, b;
  17. float c, m, y, k;
  18. float blue, green, red;
  19.  
  20.  
  21.  
  22. ifstream infile("c:/colorsdot.txt"); //Reading from file
  23.  
  24. if (!infile.is_open()) { // Condition if the file is not open
  25. cout << "Error. Unable to open file.";
  26. system ("PAUSE");
  27. exit(1);
  28. }
  29.  
  30. ofstream outfile("cmyk.txt");
  31.  
  32.  
  33. cout.setf(ios::fixed,ios::floatfield);
  34. cout.precision(1);
  35. //infile >> red >> green >> blue;
  36. //readRGB(r, g, b);
  37.  
  38.  
  39.  
  40. //infile >> red >> green >> blue;
  41. //readRGB(r, g, b);
  42.  
  43. RGBtoCMYK( r, g, b, y, m, c, k); // Calling the RGBtoCMYK
  44. //function to caclulate the cmyk values
  45. outfile << y << " " << m << " " << c << " " << k << endl;
  46. cout << y << " " << m << " " << c << " " << k << endl;
  47. //readRGB(r, g, b);
  48.  
  49.  
  50.  
  51.  
  52.  
  53. infile.close(); //closing the input file
  54. outfile.close(); // closing the output file
  55.  
  56. system("PAUSE");
  57. return EXIT_SUCCESS;
  58.  
  59. }
  60.  
  61. void readRGB(int &r, int &g, int &b){
  62.  
  63. int red, green, blue;
  64.  
  65. infile >> red;
  66. infile >> green;
  67. infile >> blue;
  68.  
  69.  
  70. if (red >= 0 && red <= 255)
  71. r = red;
  72.  
  73. else cout << "Error invalid input" << endl;
  74.  
  75. if (green >= 0 && green <= 255)
  76. g = green;
  77.  
  78. else cout << "Error invalid input" << endl;
  79.  
  80. if (blue >= 0 && blue <= 255)
  81. b = blue;
  82.  
  83. else cout << "Error invalid input" << endl;
  84.  
  85.  
  86. } //end of function readRGB
  87.  
  88.  
  89. int largest(int x, int y, int z)
  90. {
  91. if (x > y && x > z)
  92. return x;
  93. else if (y > z)
  94. return y;
  95. else
  96. return z;
  97.  
  98. }
  99. 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
  100.  
  101. {
  102.  
  103. double w;
  104.  
  105.  
  106.  
  107. 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
  108.  
  109. c = 0;
  110. m = 0;
  111. y = 0;
  112. k = 1;
  113.  
  114. }
  115.  
  116. else {
  117.  
  118. w = largest(r / 255.0, g / 255.0, b / 255.0);
  119. c = (w - (r / 255.0)) / w;
  120. m = (w - (g / 255.0)) / w;
  121. y = (w - (b / 255.0)) / w;
  122. k = 1.0 - w;
  123.  
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement