
  function rgb2hsv () {
    r=$1
    g=$2
    b=$3

    var max, min, h, s, v, d;

    max = Math.max(r, g, b);
    min = Math.min(r, g, b);
    v = max;

    d = max - min;
    s = max === 0 ? 0 : d / max;

    if (max === min) {
      h = 0; // achromatic
    } else {
      switch (max) {
      case r: h = (g - b) / d + (g < b ? 6 : 0); break;
      case g: h = (b - r) / d + 2; break;
      case b: h = (r - g) / d + 4; break;
      }
      h /= 6;
    }

    // map top 360,100,100
    h = round(h * 360);
    s = round(s * 100);
    v = round(v * 100);

    return [h, s, v];
  };