color.c 1.8 KB

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