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.8 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. #include "util.h"
  20. RGB hsv_to_rgb_impl(HSV hsv, bool use_cie) {
  21. RGB rgb;
  22. uint8_t region, remainder, p, q, t;
  23. uint16_t h, s, v;
  24. if (hsv.s == 0) {
  25. #ifdef USE_CIE1931_CURVE
  26. if (use_cie) {
  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. }
  33. #else
  34. rgb.r = hsv.v;
  35. rgb.g = hsv.v;
  36. rgb.b = hsv.v;
  37. #endif
  38. return rgb;
  39. }
  40. h = hsv.h;
  41. s = hsv.s;
  42. #ifdef USE_CIE1931_CURVE
  43. if (use_cie) {
  44. v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
  45. } else {
  46. v = hsv.v;
  47. }
  48. #else
  49. v = hsv.v;
  50. #endif
  51. region = h * 6 / 255;
  52. remainder = (h * 2 - region * 85) * 3;
  53. p = (v * (255 - s)) >> 8;
  54. q = (v * (255 - ((s * remainder) >> 8))) >> 8;
  55. t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
  56. switch (region) {
  57. case 6:
  58. case 0:
  59. rgb.r = v;
  60. rgb.g = t;
  61. rgb.b = p;
  62. break;
  63. case 1:
  64. rgb.r = q;
  65. rgb.g = v;
  66. rgb.b = p;
  67. break;
  68. case 2:
  69. rgb.r = p;
  70. rgb.g = v;
  71. rgb.b = t;
  72. break;
  73. case 3:
  74. rgb.r = p;
  75. rgb.g = q;
  76. rgb.b = v;
  77. break;
  78. case 4:
  79. rgb.r = t;
  80. rgb.g = p;
  81. rgb.b = v;
  82. break;
  83. default:
  84. rgb.r = v;
  85. rgb.g = p;
  86. rgb.b = q;
  87. break;
  88. }
  89. return rgb;
  90. }
  91. RGB hsv_to_rgb(HSV hsv) {
  92. #ifdef USE_CIE1931_CURVE
  93. return hsv_to_rgb_impl(hsv, true);
  94. #else
  95. return hsv_to_rgb_impl(hsv, false);
  96. #endif
  97. }
  98. RGB hsv_to_rgb_nocie(HSV hsv) {
  99. return hsv_to_rgb_impl(hsv, false);
  100. }
  101. #ifdef RGBW
  102. void convert_rgb_to_rgbw(rgb_led_t *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