浏览代码

Simplify HSV->RGB calculation

De-dupe repeated code
Jordi Orlando 8 年之前
父节点
当前提交
ea2d2f5d58
共有 1 个文件被更改,包括 12 次插入14 次删除
  1. 12 14
      quantum/rgblight.c

+ 12 - 14
quantum/rgblight.c

@@ -55,57 +55,56 @@ uint8_t rgblight_inited = 0;
 
 
 
 
 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
-  /* Convert hue, saturation and brightness ( HSB/HSV ) to RGB. The DIM_CURVE is
-  used only on brightness/value and on saturation (inverted). This looks the
-  most natural. */
-  uint8_t r = 0, g = 0, b = 0;
+  // Convert hue, saturation, and value (HSV/HSB) to RGB. DIM_CURVE is used only
+  // on value and saturation (inverted). This looks the most natural.
+  uint8_t r = 0, g = 0, b = 0, base, color;
 
 
   val = pgm_read_byte(&DIM_CURVE[val]);
   val = pgm_read_byte(&DIM_CURVE[val]);
   sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
   sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
 
 
-  uint8_t base;
-
   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
     r = val;
     r = val;
     g = val;
     g = val;
     b = val;
     b = val;
   } else {
   } else {
     base = ((255 - sat) * val) >> 8;
     base = ((255 - sat) * val) >> 8;
+    color = (val - base) * (hue % 60) / 60;
 
 
     switch (hue / 60) {
     switch (hue / 60) {
       case 0:
       case 0:
         r = val;
         r = val;
-        g = (((val - base) * hue) / 60) + base;
+        g = base + color;
         b = base;
         b = base;
         break;
         break;
       case 1:
       case 1:
-        r = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        r = val - color;
         g = val;
         g = val;
         b = base;
         b = base;
         break;
         break;
       case 2:
       case 2:
         r = base;
         r = base;
         g = val;
         g = val;
-        b = (((val - base) * (hue % 60)) / 60) + base;
+        b = base + color;
         break;
         break;
       case 3:
       case 3:
         r = base;
         r = base;
-        g = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        g = val - color;
         b = val;
         b = val;
         break;
         break;
       case 4:
       case 4:
-        r = (((val - base) * (hue % 60)) / 60) + base;
+        r = base + color;
         g = base;
         g = base;
         b = val;
         b = val;
         break;
         break;
       case 5:
       case 5:
         r = val;
         r = val;
         g = base;
         g = base;
-        b = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        b = val - color;
         break;
         break;
     }
     }
   }
   }
-  setrgb(r,g,b, led1);
+
+  setrgb(r, g, b, led1);
 }
 }
 
 
 void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
 void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
@@ -346,7 +345,6 @@ void rgblight_set(void) {
   }
   }
 }
 }
 
 
-
 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
 
 
 // Animation timer -- AVR Timer3
 // Animation timer -- AVR Timer3