color.c 1.7 KB

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