hsl2rgb static method

List<int> hsl2rgb(
  1. int h,
  2. double s,
  3. double l
)

Converts HSL values to RGB values.

Returns a list of doubles representing the RGB values. r, g, b

Implementation

static List<int> hsl2rgb(int h, double s, double l) {
  if (h < 0) {
    h = 0;
  } else if (h > 360) {
    h = 360;
  }

  double S;
  if (s > 1.0) {
    S = s / 100;
  } else {
    S = s;
  }

  double L;
  if (l > 1.0) {
    L = l / 100;
  } else {
    L = l;
  }

  double c = (1 - (2 * L - 1).abs()) * S;
  double x = c * (1 - (((h / 60) % 2) - 1).abs());
  double m = L - c / 2;

  double r = 0;
  double g = 0;
  double b = 0;

  // Calculate RGB values based on hue.
  if (h >= 0 && h < 60) {
    r = c;
    g = x;
    b = 0;
  } else if (h >= 60 && h < 120) {
    r = x;
    g = c;
    b = 0;
  } else if (h >= 120 && h < 180) {
    r = 0;
    g = c;
    b = x;
  } else if (h >= 180 && h < 240) {
    r = 0;
    g = x;
    b = c;
  } else if (h >= 240 && h < 300) {
    r = x;
    g = 0;
    b = c;
  } else if (h >= 300 && h < 360) {
    r = c;
    g = 0;
    b = x;
  }

  // Convert to a linear RGB color space.
  r = (r + m) * 255;
  g = (g + m) * 255;
  b = (b + m) * 255;

  return [r.round(), g.round(), b.round()];
}