RGBConverter icon indicating copy to clipboard operation
RGBConverter copied to clipboard

Truncated division

Open nkint opened this issue 9 years ago • 2 comments

Hi! I have slightly modified your library to match existing data structure I used for led lighting.

So that I had int[3] as rgb value instead of byte[3], inside a struct. I'm not so expert in C programming but seems that this change messed up the division between double and int and hslToRgb gives me only or 255-255-255 or 0-0-0.

I have casted all number in double (adding a .0 after each number) and avoided to use 1 as hue/saturation/light value but 0.99 and everything works ok.

Like this snippet of code:

void hslToRgb(double h, double s, double l, double rgb[]) {
  double r, g, b;

  if (s == 0) {
    r = g = b = l; // achromatic
  } else {
    double q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
    double p = 2 * l - q;
    r = hue2rgb(p, q, h + 1.0 / 3.0);
    g = hue2rgb(p, q, h);
    b = hue2rgb(p, q, h - 1.0 / 3.0);
  }

  rgb[0] = r * 255;
  rgb[1] = g * 255;
  rgb[2] = b * 255;
}

double hue2rgb(double p, double q, double t) {
  if (t < 0) t += 1;
  if (t > 1.0) t -= 1;
  if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
  if (t < 1.0 / 2.0) return q;
  if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
  return p;
}

Now it works like a charm, thanks!

nkint avatar Nov 26 '14 16:11 nkint

I see the hslToRgb and hu2rgb function looks very close to the above. Has this issue been resolved? Can this issue be closed?

abetusk avatar Jun 20 '19 17:06 abetusk

As noted in the main README I consider this abandoned. Feel free to fork your own and fix it there.

ratkins avatar Jun 20 '19 19:06 ratkins