Advertisement
cristiano002

Konwertery

Jun 21st, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int RGB_na_CMYK(float R, float G, float B){
  9.     float _R, _G, _B, C, M, Y, K, max;
  10.  
  11.     if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
  12.     _R = R / 255;
  13.     _G = G / 255;
  14.     _B = B / 255;
  15.  
  16.     max = _R;
  17.     if (_G > max) max = _G;
  18.     if (_B > max) max = _B;
  19.  
  20.     K = 1 - max;
  21.     C = (1 - _R - K) / (1 - K);
  22.     M = (1 - _G - K) / (1 - K);
  23.     Y = (1 - _B - K) / (1 - K);
  24.    
  25.     cout << "RGB(" << R << ", " << G << ", " << B << ")->CMYK(" << C << ", " << M << ", " << Y << ", " << K << ")" << endl << endl;
  26.     return 0;
  27.     }
  28.     else return -1;
  29. }
  30.  
  31. int CMYK_na_RGB(float C, float M, float Y, float K){
  32.     float R, G, B;
  33.  
  34.     if (C >= 0 && M >= 0 && Y >= 0 && K >= 0 && C <= 1 && M <= 1 && Y <= 1 && K <= 1){
  35.     R = 255 * (1 - C) * (1 - K);
  36.     G = 255 * (1 - M) * (1 - K);
  37.     B = 255 * (1 - Y) * (1 - K);
  38.  
  39.     cout << "CMYK(" << C << ", " << M << ", " << Y << ", " << K << ")->RGB(" << R << ", " << G << ", " << B << ")" << endl << endl;
  40.     return 0;
  41. }
  42.     else return -1;
  43. }
  44.  
  45. int RGB_na_HSL(float R, float G, float B){
  46.     float _R, _G, _B, H, S, L,  max, min, delta;
  47.     int i; // co jest maxem
  48.  
  49.  
  50.     if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
  51.         _R = R / 255;
  52.         _G = G / 255;
  53.         _B = B / 255;
  54.  
  55.         max = _R; i = 1;
  56.         if (_G > max) {max = _G; i = 2;}
  57.         if (_B > max) {max = _B; i = 3;}
  58.  
  59.         min = _R;
  60.         if (_G < min) min = _G;
  61.         if (_B < min) min = _B;
  62.  
  63.         delta = max - min;
  64.  
  65.         // OBLICZANIE L
  66.         L = (max + min) / 2;
  67.  
  68.         // OBLICZANIE S
  69.         if (delta == 0) S = 0;
  70.         else S = delta / (1 - abs(2 * L - 1));
  71.        
  72.         // OBLICZANIE H
  73.         switch (i){
  74.         case 1:
  75.             H = (_G - _B) / delta;
  76.             H = fmodf(H, 6);
  77.             H *= 60;
  78.             break;
  79.         case 2:
  80.             H = 60 * (((_B - _R) / delta) + 2);
  81.             break;
  82.         case 3:
  83.             H = 60 * (((_R - _G) / delta) + 4);
  84.             break;
  85.         }
  86.  
  87.         // Zaokrąglenie
  88.         //cout.setf(ios::fixed);
  89.         //cout.precision(1);
  90.  
  91.         // Poprawki procentowe
  92.         S *= 100;
  93.         L *= 100;
  94.         cout << "RGB(" << R << ", " << G << ", " << B << ")->HSL(" << H << "st, " << S << "%, " << L << "%)" << endl << endl;
  95.         return 0;
  96.     }
  97.     else return -1;
  98. }
  99.  
  100. int HSL_na_RGB(float H, float S, float L){
  101.  
  102.     float C, X, m, _R, _G, _B, R, G, B;
  103.  
  104.     if (H >= 0 && H < 360 && S >= 0 && S <= 100 &&  L>= 0 && L <= 100){
  105.  
  106.         float _L = L / 100;
  107.         float _S = S / 100;
  108.  
  109.         C = (1 - abs(2 * _L - 1)) * _S;
  110.         X = C * (1 - abs(fmodf((H / 60), 2) - 1));
  111.         m = _L - C / 2;
  112.  
  113.         if (H <= 60) { _R = C; _G = X; _B = 0; }
  114.         if (H >= 60 && H <= 120) { _R = X; _G = C; _B = 0; }
  115.         if (H >= 120 && H <= 180) { _R = 0; _G = C; _B = X; }
  116.         if (H >= 180 && H <= 240) { _R = 0; _G = X; _B = C; }
  117.         if (H >= 240 && H <= 300) { _R = X; _G = 0; _B = C; }
  118.         if (H >= 300 && H <= 360) { _R = C; _G = 0; _B = X; }
  119.  
  120.         R = _R + m;
  121.         G = _G + m;
  122.         B = _B + m;
  123.        
  124.         cout << "HSL(" << H << "st, " << S << "%, " << L << "%)->" << "RGB(" << R << ", " << G << ", " << B << ")" <<  endl << endl;
  125.  
  126.         return 0;
  127.     }
  128.     else return -1;
  129. }
  130.  
  131. int RGB_na_HSV(float R, float G, float B){
  132.     float _R, _G, _B, H, S, V, max, min, delta;
  133.     int i; // co jest maxem
  134.  
  135.  
  136.     if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
  137.         _R = R / 255;
  138.         _G = G / 255;
  139.         _B = B / 255;
  140.  
  141.         max = _R; i = 1;
  142.         if (_G > max) { max = _G; i = 2; }
  143.         if (_B > max) { max = _B; i = 3; }
  144.  
  145.         min = _R;
  146.         if (_G < min) min = _G;
  147.         if (_B < min) min = _B;
  148.  
  149.         delta = max - min;
  150.  
  151.         // OBLICZANIE V
  152.         V = max;
  153.  
  154.         // OBLICZANIE S
  155.         if (delta == 0) S = 0;
  156.         else S = delta / max;
  157.  
  158.         // OBLICZANIE H
  159.         switch (i){
  160.         case 1:
  161.             H = (_G - _B) / delta;
  162.             H = fmodf(H, 6);
  163.             H *= 60;
  164.             break;
  165.         case 2:
  166.             H = 60 * (((_B - _R) / delta) + 2);
  167.             break;
  168.         case 3:
  169.             H = 60 * (((_R - _G) / delta) + 4);
  170.             break;
  171.         }
  172.  
  173.        
  174.         // Poprawki procentowe
  175.         S *= 100;
  176.         V *= 100;
  177.         cout << "RGB(" << R << ", " << G << ", " << B << ")->HSV(" << H << "st, " << S << "%, " << V << "%)" << endl << endl;
  178.  
  179.         return 0;
  180.     }
  181.     else return -1;
  182. }
  183.  
  184. int main(){
  185.  
  186.     RGB_na_CMYK(255, 100, 255);
  187.     CMYK_na_RGB(1, 1, 0, 0);
  188.     RGB_na_HSL(255, 100, 100);
  189.     HSL_na_RGB(359, 50, 50);
  190.     RGB_na_HSV(255, 100, 100);
  191.    
  192.  
  193.  
  194.     system("PAUSE");
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement