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.

87 lines
2.1 KiB

  1. /* Copyright 2017 Jason Williams
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "color.h"
  17. #include "led_tables.h"
  18. #include "progmem.h"
  19. RGB hsv_to_rgb(HSV hsv) {
  20. RGB rgb;
  21. uint8_t region, remainder, p, q, t;
  22. uint16_t h, s, v;
  23. if (hsv.s == 0) {
  24. #ifdef USE_CIE1931_CURVE
  25. rgb.r = rgb.g = rgb.b = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
  26. #else
  27. rgb.r = hsv.v;
  28. rgb.g = hsv.v;
  29. rgb.b = hsv.v;
  30. #endif
  31. return rgb;
  32. }
  33. h = hsv.h;
  34. s = hsv.s;
  35. #ifdef USE_CIE1931_CURVE
  36. v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
  37. #else
  38. v = hsv.v;
  39. #endif
  40. region = h * 6 / 255;
  41. remainder = (h * 2 - region * 85) * 3;
  42. p = (v * (255 - s)) >> 8;
  43. q = (v * (255 - ((s * remainder) >> 8))) >> 8;
  44. t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
  45. switch (region) {
  46. case 6:
  47. case 0:
  48. rgb.r = v;
  49. rgb.g = t;
  50. rgb.b = p;
  51. break;
  52. case 1:
  53. rgb.r = q;
  54. rgb.g = v;
  55. rgb.b = p;
  56. break;
  57. case 2:
  58. rgb.r = p;
  59. rgb.g = v;
  60. rgb.b = t;
  61. break;
  62. case 3:
  63. rgb.r = p;
  64. rgb.g = q;
  65. rgb.b = v;
  66. break;
  67. case 4:
  68. rgb.r = t;
  69. rgb.g = p;
  70. rgb.b = v;
  71. break;
  72. default:
  73. rgb.r = v;
  74. rgb.g = p;
  75. rgb.b = q;
  76. break;
  77. }
  78. return rgb;
  79. }