Advertisement
cristiano002

RGB NA HSL

Jun 21st, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. int RGB_na_HSL(float R, float G, float B){
  2.         float _R, _G, _B, H, S, L,  max, min, delta;
  3.         int i; // co jest maxem
  4.  
  5.  
  6.         if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
  7.                 _R = R / 255;
  8.                 _G = G / 255;
  9.                 _B = B / 255;
  10.  
  11.                 max = _R; i = 1;
  12.                 if (_G > max) {max = _G; i = 2;}
  13.                 if (_B > max) {max = _B; i = 3;}
  14.  
  15.                 min = _R;
  16.                 if (_G < min) min = _G;
  17.                 if (_B < min) min = _B;
  18.  
  19.                 delta = max - min;
  20.  
  21.                 // OBLICZANIE L
  22.                 L = (max + min) / 2;
  23.  
  24.                 // OBLICZANIE S
  25.                 if (delta == 0) S = 0;
  26.                 else S = delta / (1 - abs(2 * L - 1));
  27.                
  28.                 // OBLICZANIE H
  29.                 switch (i){
  30.                 case 1:
  31.                         H = (_G - _B) / delta;
  32.                         H = fmodf(H, 6);
  33.                         H *= 60;
  34.                         break;
  35.                 case 2:
  36.                         H = 60 * (((_B - _R) / delta) + 2);
  37.                         break;
  38.                 case 3:
  39.                         H = 60 * (((_R - _G) / delta) + 4);
  40.                         break;
  41.                 }
  42.  
  43.                 // Zaokrąglenie
  44.                 cout.setf(ios::fixed);
  45.                 cout.precision(1);
  46.  
  47.                 // Poprawki procentowe
  48.                 S *= 100;
  49.                 L *= 100;
  50.                 cout << "RGB(" << R << ", " << G << ", " << B << ")->HSL(" << H << "st, " << S << "%, " << L << "%)" << endl << endl;
  51.                 return 0;
  52.         }
  53.         else return -1;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement