Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ohthehugemanatee
Copy link
Contributor

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:

  • identify if prev_state is HSV vs RGB, or
  • ensure prev_state always comes in as HSV.

@ohthehugemanatee
Copy link
Contributor Author

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.

@ohthehugemanatee ohthehugemanatee changed the title Add hsv to rgb conversion function in restricted list [WIP] Add hsv to rgb conversion function in restricted list Nov 14, 2021
@jackw01
Copy link
Owner

jackw01 commented Nov 16, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants