custom_gradient.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright 2022 HorrorTroll <https://github.com/HorrorTroll>
  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. extern HSV gradient_0;
  17. extern HSV gradient_100;
  18. extern bool reflected_gradient;
  19. static HSV INTERPOLATE_HSV(float step, HSV gradient_0, HSV gradient_100) {
  20. uint8_t cw, ccw;
  21. HSV color;
  22. cw = (gradient_0.h >= gradient_100.h) ? 255 + gradient_100.h - gradient_0.h : gradient_100.h - gradient_0.h; // Hue range is 0 to 255.
  23. ccw = (gradient_0.h >= gradient_100.h) ? gradient_0.h - gradient_100.h : 255 + gradient_0.h - gradient_100.h;
  24. if( cw < ccw ) { // going clockwise
  25. color.h = gradient_0.h + (uint8_t)(step * cw);
  26. } else { // Going counter clockwise
  27. color.h = gradient_0.h - (uint8_t)(step * ccw);
  28. }
  29. color.s = gradient_0.s + step * (gradient_100.s - gradient_0.s);
  30. // Scale V with global RGB Matrix's V, so users can still control overall brightness with RGB_VAI & RGB_VAD0
  31. color.v = round((gradient_0.v + step * (gradient_100.v - gradient_0.v)) * ((float)rgb_matrix_config.hsv.v / 255));
  32. return color;
  33. }
  34. static HSV CUSTOM_GRADIENT_math(uint8_t led_x, uint8_t min_x, uint8_t max_x) {
  35. float step = (float)led_x / (max_x - min_x);
  36. float mid_gradient_pos = 0.5;
  37. if( reflected_gradient ) {
  38. if( step <= mid_gradient_pos ) {
  39. return INTERPOLATE_HSV(step * (1/mid_gradient_pos), gradient_0, gradient_100);
  40. } else {
  41. return INTERPOLATE_HSV((step - mid_gradient_pos) * (1/(1-mid_gradient_pos)), gradient_100, gradient_0);
  42. }
  43. } else {
  44. return INTERPOLATE_HSV(step, gradient_0, gradient_100);
  45. }
  46. }
  47. static bool CUSTOM_GRADIENT(effect_params_t* params) {
  48. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  49. uint8_t min_x = 0; // X coordinate of the left-most LED
  50. uint8_t max_x = 224; // X coordinate of the right-most LED
  51. for (uint8_t i = led_min; i < led_max; i++) {
  52. RGB_MATRIX_TEST_LED_FLAGS();
  53. HSV hsv_orig = CUSTOM_GRADIENT_math(g_led_config.point[i].x, min_x, max_x);
  54. RGB rgb = hsv_to_rgb(hsv_orig);
  55. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  56. }
  57. return led_max < RGB_MATRIX_LED_COUNT;
  58. }