You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6KB

  1. #include "trowaSoftUtilities.hpp"
  2. const char * TROWA_NOTES[TROWA_SEQ_NUM_NOTES] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
  3. // Split a string
  4. std::vector<std::string> str_split(const std::string& s, char delimiter)
  5. {
  6. std::vector<std::string> tokens;
  7. std::string token;
  8. std::istringstream tokenStream(s);
  9. while (std::getline(tokenStream, token, delimiter))
  10. {
  11. tokens.push_back(token);
  12. }
  13. return tokens;
  14. }
  15. namespace trowaSoft
  16. {
  17. void TSColorToHSL(NVGcolor color, TSColorHSL* hsv)
  18. {
  19. float min;
  20. float max;
  21. min = (color.r < color.g) ? ((color.r < color.b) ? color.r : color.b) : ((color.g < color.b) ? color.g : color.b);
  22. max = (color.r > color.g) ? ((color.r > color.b) ? color.r : color.b) : ((color.g > color.b) ? color.g : color.b);
  23. float delta = max - min;
  24. //HSL: 0.7, 0.5, 0.3
  25. //RGB 0.21, 0.15, 0.45
  26. // min = 0.15
  27. // max = 0.45
  28. // delta = 0.30
  29. // Hue:
  30. // r-b/delta/6.0 + 2.0/3.0 =
  31. // (0.21-0.15)/0.30/6.0 + 2.0/3.0 = 0.244
  32. // Sat:
  33. // delta/max = 0.30/0.45 = 0.667 --> NO
  34. // 0.7, 0.5, 0.3 --> 0.7, 0.67, 0.46
  35. hsv->lum = (max+min)/2.0;
  36. if (max == 0.0 || delta < 0.0001)
  37. {
  38. hsv->s = 0;
  39. hsv->h = 0;
  40. }
  41. else {
  42. if (hsv->lum < 0.5)
  43. hsv->s = delta / (max + min);
  44. else
  45. hsv->s = delta / (2 - max - min);
  46. if (max == color.r) {
  47. float tmp = ((color.g - color.b) / delta) / 6.0;
  48. hsv->h = (tmp - (int)tmp) / 6.0;
  49. }
  50. else if (max == color.g) {
  51. hsv->h = (color.b - color.r) / delta / 6.0 + 1.0/3.0;
  52. }
  53. else {
  54. hsv->h = (color.r - color.g) / delta / 6.0 + 2.0 / 3.0;
  55. }
  56. if (hsv->h < 0)
  57. hsv->h += 1.0;
  58. }
  59. return;
  60. }
  61. }