custom_gradient.c (2805B)
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 17 extern hsv_t gradient_0; 18 extern hsv_t gradient_100; 19 extern bool reflected_gradient; 20 21 static hsv_t INTERPOLATE_HSV(float step, hsv_t gradient_0, hsv_t gradient_100) { 22 uint8_t cw, ccw; 23 hsv_t color; 24 25 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. 26 ccw = (gradient_0.h >= gradient_100.h) ? gradient_0.h - gradient_100.h : 255 + gradient_0.h - gradient_100.h; 27 28 if( cw < ccw ) { // going clockwise 29 color.h = gradient_0.h + (uint8_t)(step * cw); 30 } else { // Going counter clockwise 31 color.h = gradient_0.h - (uint8_t)(step * ccw); 32 } 33 34 color.s = gradient_0.s + step * (gradient_100.s - gradient_0.s); 35 36 // Scale V with global RGB Matrix's V, so users can still control overall brightness with RM_VALU & RM_VALD 37 color.v = round((gradient_0.v + step * (gradient_100.v - gradient_0.v)) * ((float)rgb_matrix_config.hsv.v / 255)); 38 39 return color; 40 } 41 42 static hsv_t CUSTOM_GRADIENT_math(uint8_t led_x, uint8_t min_x, uint8_t max_x) { 43 float step = (float)led_x / (max_x - min_x); 44 float mid_gradient_pos = 0.5; 45 46 if( reflected_gradient ) { 47 if( step <= mid_gradient_pos ) { 48 return INTERPOLATE_HSV(step * (1/mid_gradient_pos), gradient_0, gradient_100); 49 } else { 50 return INTERPOLATE_HSV((step - mid_gradient_pos) * (1/(1-mid_gradient_pos)), gradient_100, gradient_0); 51 } 52 53 } else { 54 return INTERPOLATE_HSV(step, gradient_0, gradient_100); 55 } 56 } 57 58 static bool CUSTOM_GRADIENT(effect_params_t* params) { 59 RGB_MATRIX_USE_LIMITS(led_min, led_max); 60 61 uint8_t min_x = 0; // X coordinate of the left-most LED 62 uint8_t max_x = 224; // X coordinate of the right-most LED 63 64 for (uint8_t i = led_min; i < led_max; i++) { 65 RGB_MATRIX_TEST_LED_FLAGS(); 66 67 hsv_t hsv_orig = CUSTOM_GRADIENT_math(g_led_config.point[i].x, min_x, max_x); 68 rgb_t rgb = hsv_to_rgb(hsv_orig); 69 70 rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b); 71 } 72 73 return led_max < RGB_MATRIX_LED_COUNT; 74 }