color.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. v = hsv.v;
  36. region = h * 6 / 255;
  37. remainder = (h * 2 - region * 85) * 3;
  38. p = (v * (255 - s)) >> 8;
  39. q = (v * (255 - ((s * remainder) >> 8))) >> 8;
  40. t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
  41. switch (region) {
  42. case 6:
  43. case 0:
  44. rgb.r = v;
  45. rgb.g = t;
  46. rgb.b = p;
  47. break;
  48. case 1:
  49. rgb.r = q;
  50. rgb.g = v;
  51. rgb.b = p;
  52. break;
  53. case 2:
  54. rgb.r = p;
  55. rgb.g = v;
  56. rgb.b = t;
  57. break;
  58. case 3:
  59. rgb.r = p;
  60. rgb.g = q;
  61. rgb.b = v;
  62. break;
  63. case 4:
  64. rgb.r = t;
  65. rgb.g = p;
  66. rgb.b = v;
  67. break;
  68. default:
  69. rgb.r = v;
  70. rgb.g = p;
  71. rgb.b = q;
  72. break;
  73. }
  74. #ifdef USE_CIE1931_CURVE
  75. rgb.r = pgm_read_byte(&CIE1931_CURVE[rgb.r]);
  76. rgb.g = pgm_read_byte(&CIE1931_CURVE[rgb.g]);
  77. rgb.b = pgm_read_byte(&CIE1931_CURVE[rgb.b]);
  78. #endif
  79. return rgb;
  80. }