Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- using namespace std;
- int r,g,b =0;
- vector <string> colors;
- int red = 0;
- int green =0;
- int blue = 0;
- int broiCvetove = 0;
- string rgb2hex(int r, int g, int b, bool with_head = false);
- string rgb2hex(int r, int g, int b, bool with_head)
- {
- stringstream ss;
- if (with_head)
- ss << "#";
- ss << hex << (r << 16 | g << 8 | b);
- return ss.str();
- }
- void HexToRGB(string inputString) // input example: #ff00ff
- {
- //cout << "hextorgb inputString: " << inputString << endl;
- inputString.erase(0,1); // removing the hashtag
- int num = stoi(inputString,0,16);
- int RGB = num;
- r = RGB >> 16;
- g = (RGB & 0x00ff00) >> 8;
- b = (RGB & 0x0000ff);
- RGB = 0;
- RGB |= r <<16;
- RGB |= b <<8;
- RGB |= g;
- // cout << "red: " << r <<endl;
- }
- void currentAverageColor()
- {
- int redSum = 0;
- int greenSum =0;
- int blueSum =0;
- for(int i =0; i<broiCvetove; i++)
- {
- string currentString = colors[i];
- HexToRGB(currentString);
- redSum = redSum + r;
- greenSum = greenSum + g;
- blueSum = blueSum + b;
- // cout << "red sum : " << redSum << endl;
- }
- red = redSum/broiCvetove;
- // cout << "red: " << endl;
- green = greenSum/broiCvetove;
- blue = blueSum/broiCvetove;
- // cout << "red: " << red << "green " << green<< "blue: " << blue<< endl;
- }
- int main()
- {
- int broiTestove =0;
- cin >> broiTestove;
- cin.ignore();
- string input, newString;
- while(broiTestove--)
- {
- cin >> broiCvetove;
- cin.ignore();
- getline(cin, input);
- stringstream stream;
- stream.str(input);
- while(stream >> newString)
- {
- colors.push_back(newString);
- }
- for(int i =0; i<colors.size();i++)
- {
- // cout << colors[i] << endl;
- }
- currentAverageColor();
- auto result = rgb2hex(red,green, blue);
- //cout << "hex: " << result << endl;
- cout << '#' <<result << endl;
- colors.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement