hsv method

HsvColor hsv()

Convert to HsvColor.

Implementation

HsvColor hsv() {
  final num mx = max(max(red, green), blue),
      mn = min(min(red, green), blue), ch = mx - mn;
  final int lead = red > green ?
      (red > blue ? 0 : 2) : (green > blue ? 1 : 2);
  final num hue = 60 * (ch == 0 ? 0 :
    lead == 0 ? ((green - blue) / ch) % 6 :
    lead == 1 ? (blue - red) / ch + 2 : (red - green) / ch + 4);
  final num val = mx  / 255;
  final num sat = mx == 0 ? 0 : ch / mx;
  return HsvColor(hue, sat, val, alpha);
}