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.

121 lines
2.9 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_impl(HSV hsv, bool use_cie) {
  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. if (use_cie) {
  26. rgb.r = rgb.g = rgb.b = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
  27. } else {
  28. rgb.r = hsv.v;
  29. rgb.g = hsv.v;
  30. rgb.b = hsv.v;
  31. }
  32. #else
  33. rgb.r = hsv.v;
  34. rgb.g = hsv.v;
  35. rgb.b = hsv.v;
  36. #endif
  37. return rgb;
  38. }
  39. h = hsv.h;
  40. s = hsv.s;
  41. #ifdef USE_CIE1931_CURVE
  42. if (use_cie) {
  43. v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
  44. } else {
  45. v = hsv.v;
  46. }
  47. #else
  48. v = hsv.v;
  49. #endif
  50. region = h * 6 / 255;
  51. remainder = (h * 2 - region * 85) * 3;
  52. p = (v * (255 - s)) >> 8;
  53. q = (v * (255 - ((s * remainder) >> 8))) >> 8;
  54. t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
  55. switch (region) {
  56. case 6:
  57. case 0:
  58. rgb.r = v;
  59. rgb.g = t;
  60. rgb.b = p;
  61. break;
  62. case 1:
  63. rgb.r = q;
  64. rgb.g = v;
  65. rgb.b = p;
  66. break;
  67. case 2:
  68. rgb.r = p;
  69. rgb.g = v;
  70. rgb.b = t;
  71. break;
  72. case 3:
  73. rgb.r = p;
  74. rgb.g = q;
  75. rgb.b = v;
  76. break;
  77. case 4:
  78. rgb.r = t;
  79. rgb.g = p;
  80. rgb.b = v;
  81. break;
  82. default:
  83. rgb.r = v;
  84. rgb.g = p;
  85. rgb.b = q;
  86. break;
  87. }
  88. return rgb;
  89. }
  90. RGB hsv_to_rgb(HSV hsv) {
  91. #ifdef USE_CIE1931_CURVE
  92. return hsv_to_rgb_impl(hsv, true);
  93. #else
  94. return hsv_to_rgb_impl(hsv, false);
  95. #endif
  96. }
  97. RGB hsv_to_rgb_nocie(HSV hsv) { return hsv_to_rgb_impl(hsv, false); }
  98. #ifdef RGBW
  99. # ifndef MIN
  100. # define MIN(a, b) ((a) < (b) ? (a) : (b))
  101. # endif
  102. void convert_rgb_to_rgbw(LED_TYPE *led) {
  103. // Determine lowest value in all three colors, put that into
  104. // the white channel and then shift all colors by that amount
  105. led->w = MIN(led->r, MIN(led->g, led->b));
  106. led->r -= led->w;
  107. led->g -= led->w;
  108. led->b -= led->w;
  109. }
  110. #endif