main.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <QCoreApplication>
  2. bool validColor(std::string color);
  3. std::string okay(std::string s);
  4. int main(int argc, char *argv[])
  5. {
  6. QCoreApplication a(argc, argv);
  7. validColor("rgb(0,,0)");
  8. return a.exec();
  9. }
  10. std::string okay(std::string s) {
  11. std::string temp;
  12. for(int i = 0; i<s.size(); i++) {
  13. if(std::isdigit(s[i])) temp.push_back(s[i]);
  14. }
  15. return temp;
  16. }
  17. std::string fokay(std::string s) {
  18. std::string temp;
  19. for(int i = 0; i<s.size(); i++) {
  20. // floating pt can have a decimal
  21. if(std::isdigit(s[i]) || ('.' == s[i])) temp.push_back(s[i]);
  22. // again, if the first character is +/- this is ok
  23. if(0 == temp.size() && (('-' == s[i]) || ('+' == s[i]))) temp.push_back(s[i]);
  24. }
  25. return temp;
  26. }
  27. /* Given an RGB(A) CSS color, determine whether its
  28. * format is valid or not. Create a function that takes a string
  29. * (e.g. "rgb(0, 0, 0)") and return true if it's format is correct,
  30. * otherwise return false.
  31. * Alpha is between 0 and 1.
  32. * There are a few edge cases. Check out the Tests tab to know more.
  33. */
  34. bool validColor(std::string color) {
  35. // [!] I DO NOT AGREE WITH THIS TEST (whitespace_before_parenthesis)
  36. if(std::string::npos != color.find("rgb ")) return false;
  37. // [!] THIS TEST IS HIGHLY QUESTIONABLE (rgb_with_4_numbers / rgba_with_3_numbers)
  38. int expected = std::string::npos != color.find("rgba") ?4 :3;
  39. // [!] DEFINITELY NOT OK (numbers_below_0)
  40. if(std::string::npos != color.find('-')) return false;
  41. // [?] IDK, THIS IS MAYBE OKAY (numbers_above_100_percent)
  42. bool checkPercent = std::string::npos != color.find('%');
  43. // find the first comma, store the preceding number
  44. std::vector <std::string> rgb;
  45. for(int i = 0; i<3; i++){
  46. int position = color.find(',');
  47. // if a comma isn't found, stop.
  48. if(std::string::npos == position)break;
  49. // empty string is invalid parameter
  50. if(!position) return false;
  51. // a comma was found, copy everything before comma.
  52. rgb.push_back(okay(color.substr(0,position)));
  53. // delete the commma and everything before it.
  54. color = color.substr(position+1,std::string::npos);
  55. }
  56. // this is the third/fourth number
  57. if(color.size()){rgb.push_back(fokay(color));}
  58. if(rgb.size() != expected) return false;
  59. for(int j = 0;j<rgb.size();j++){
  60. if(j == 3){
  61. float rgbA = std::stof(rgb[3]);
  62. if(0>rgbA || rgbA>1){return false;}
  63. } else {
  64. int aNumber = std::stoi(rgb[j], nullptr, 10);
  65. // if aNumber is greater than 255 or less than 0, then its not valid
  66. if(0>aNumber || aNumber>255) return false;
  67. // [?] see (numbers_above_100_percent)
  68. if(checkPercent && aNumber>100) return false;
  69. }
  70. }
  71. return true;
  72. }