Advertisement
193030

2017. G(RGB) OK

Apr 24th, 2020
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7.  
  8. int r,g,b =0;
  9. vector <string> colors;
  10. int red = 0;
  11. int green =0;
  12. int blue = 0;
  13.  
  14. int broiCvetove = 0;
  15.  
  16. string rgb2hex(int r, int g, int b, bool with_head = false);
  17.  
  18. string rgb2hex(int r, int g, int b, bool with_head)
  19. {
  20.     stringstream ss;
  21.     if (with_head)
  22.         ss << "#";
  23.     ss << hex << (r << 16 | g << 8 | b);
  24.     return ss.str();
  25. }
  26.  
  27. void HexToRGB(string inputString) // input example: #ff00ff
  28. {
  29.     //cout << "hextorgb inputString: " << inputString << endl;
  30.     inputString.erase(0,1); // removing the hashtag
  31.     int num = stoi(inputString,0,16);
  32.     int RGB = num;
  33.     r = RGB >> 16;
  34.     g = (RGB & 0x00ff00) >> 8;
  35.  
  36.     b = (RGB & 0x0000ff);
  37.  
  38.     RGB = 0;
  39.     RGB |= r <<16;
  40.     RGB |= b <<8;
  41.     RGB |= g;
  42.    // cout << "red: " << r <<endl;
  43.  
  44.  
  45. }
  46.  
  47. void currentAverageColor()
  48. {
  49.  
  50.     int redSum = 0;
  51.     int greenSum =0;
  52.     int blueSum =0;
  53.     for(int i =0; i<broiCvetove; i++)
  54.     {
  55.         string currentString  = colors[i];
  56.         HexToRGB(currentString);
  57.         redSum = redSum + r;
  58.         greenSum = greenSum + g;
  59.         blueSum = blueSum + b;
  60.     //    cout << "red sum : " << redSum << endl;
  61.  
  62.     }
  63.     red = redSum/broiCvetove;
  64.   //  cout << "red: " << endl;
  65.     green = greenSum/broiCvetove;
  66.     blue = blueSum/broiCvetove;
  67.   //  cout << "red: " << red << "green " << green<< "blue: " << blue<<  endl;
  68.  
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76. int main()
  77.  
  78. {
  79.  
  80.  
  81. int broiTestove =0;
  82. cin >> broiTestove;
  83. cin.ignore();
  84.  
  85.  
  86.  
  87. string input, newString;
  88.  
  89.  
  90.  while(broiTestove--)
  91.     {
  92.     cin >> broiCvetove;
  93.     cin.ignore();
  94.     getline(cin, input);
  95.     stringstream stream;
  96.     stream.str(input);
  97.     while(stream >> newString)
  98.     {
  99.         colors.push_back(newString);
  100.     }
  101.     for(int i =0; i<colors.size();i++)
  102.     {
  103.  
  104.        // cout << colors[i] << endl;
  105.     }
  106.     currentAverageColor();
  107.     auto result = rgb2hex(red,green, blue);
  108.     //cout << "hex: " << result << endl;
  109.     cout << '#' <<result << endl;
  110.     colors.clear();
  111.     }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement