rgb2hsl function

List<num> rgb2hsl (
  1. List<num> color
)

returns a hsl array

Implementation

List<num> rgb2hsl(List<num> color) {
  var r = color[0];
  var g = color[1];
  var b = color[2];

  var nr = r / 255;
  var ng = g / 255;
  var nb = b / 255;

  var max = [nr, ng, nb].reduce(math.max);
  var min = [nr, ng, nb].reduce(math.min);
  var h;
  var s;
  var l = (max + min) / 2;

  if (max == min) {
    h = 0;
    s = 0;
  } else {
    var d = max - min;
    s = (l > 0.5) ? d / (2 - max - min) : d / (max + min);

    if (max == nr) {
      h = (ng - nb) / d + (ng < nb ? 6 : 0);
    }
    if (max == ng) {
      h = (nb - nr) / d + 2;
    }
    if (max == nb) {
      h = (nr - ng) / d + 4;
    }

    h /= 6;
  }
  return [h, s, l];
}