hsl2rgb static method

List<int> hsl2rgb(
  1. num hue,
  2. num saturation,
  3. num lightness
)

Implementation

static List<int> hsl2rgb(num hue, num saturation, num lightness) {
  if (saturation <= 1 && lightness <= 1) {
    // if both <=1 we assume that are 0-1 range, NOTE that means if REALLT want (0.01 s then you need to pass it like that and not as 1 in to 100 scale)
    saturation *= 100;
    lightness *= 100;
  }
  hue = hue / 360;
  saturation = saturation / 100;
  lightness = lightness / 100;
  num t2;
  num t3;
  num val;

  if (saturation == 0) {
    final iVal = (lightness * 255).round();
    return [iVal, iVal, iVal];
  }

  if (lightness < 0.5) {
    t2 = lightness * (1 + saturation);
  } else {
    t2 = lightness + saturation - lightness * saturation;
  }

  var t1 = 2 * lightness - t2;

  final rgb = [0, 0, 0];
  for (var i = 0; i < 3; i++) {
    t3 = hue + 1 / 3 * -(i - 1);
    if (t3 < 0) {
      t3++;
    }

    if (t3 > 1) {
      t3--;
    }

    if (6 * t3 < 1) {
      val = t1 + (t2 - t1) * 6 * t3;
    } else if (2 * t3 < 1) {
      val = t2;
    } else if (3 * t3 < 2) {
      val = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
    } else {
      val = t1;
    }

    rgb[i] = (val * 255).round();
  }

  return rgb;
}