jackw01 / led-control

Advanced WS2812/SK6812 RGB/RGBW LED controller with on-the-fly Python animation programming, web code editor/control interface, 1D, 2D, and 3D display support, and E1.31 sACN support
https://jackw01.github.io/led-control/
MIT License
162 stars 35 forks source link

[WIP] Add hsv to rgb conversion function in restricted list #25

Open ohthehugemanatee opened 2 years ago

ohthehugemanatee commented 2 years ago

So far the only functions that return RGB colors are ones that ignore the palette. This PR adds a function (shamelessly learned from stackoverflow) for converting HSV to RGB. I don't think the reverse is necessary, since the palette functions return HSV and the whole primary pattern can return RGB without problem.

One thing I'm not sure about though: does prev_state always come in HSV? Or if the previous loop returned RGB, will we get RGB on the second loop? If it's the latter case, this PR needs some work to either:

ohthehugemanatee commented 2 years ago

Marking this as Work In Progress because on testing, it looks like prev_state does preserve whatever the previous return type was. Still usable if you do the conversion inside an if t==0: block but that's not nice.

jackw01 commented 2 years ago

This would be much faster if implemented in C (add this to led_render.h):

color_rgb_float hsv_to_rgb(float h, float s, float v) {
  if (s == 0.0) return (color_rgb_float){v, v, v};
  float f = h * 6.0;
  int i = (int)f;
  f -= i;
  i %= 6;
  float p = v * (1.0 - s);
  float q = v * (1.0 - s * f);
  float t = v * (1.0 - s * (1.0 - f));
  if (i == 0) return (color_rgb_float){v, t, p};
  else if (i == 1) return (color_rgb_float){q, v, p};
  else if (i == 2) return (color_rgb_float){p, v, t};
  else if (i == 3) return (color_rgb_float){p, q, v};
  else if (i == 4) return (color_rgb_float){t, p, v};
  else return (color_rgb_float){v, p, q};
}

Just run swig -python ./ledcontrol/driver/ledcontrol_rpi_ws281x_driver.i && sudo python3 setup.py develop to rebuild the extension module and add driver.hsv_to_rgb to the restricted_globals dictionary.