rgbToHsv method

void rgbToHsv (Vector4 rgbColor, Vector4 hsvColor)

Convert rgbColor from rgb color model to the hue, saturation, and value (HSV) color model and store it in hsvColor.

Implementation

static void rgbToHsv(Vector4 rgbColor, Vector4 hsvColor) {
  final double max = math.max(math.max(rgbColor.r, rgbColor.g), rgbColor.b);
  final double min = math.min(math.min(rgbColor.r, rgbColor.g), rgbColor.b);
  final double d = max - min;
  final double v = max;
  final double s = max == 0.0 ? 0.0 : d / max;
  double h = 0.0;

  if (max != min) {
    if (max == rgbColor.r) {
      h = (rgbColor.g - rgbColor.b) / d +
          (rgbColor.g < rgbColor.b ? 6.0 : 0.0);
    } else if (max == rgbColor.g) {
      h = (rgbColor.b - rgbColor.r) / d + 2.0;
    } else {
      h = (rgbColor.r - rgbColor.g) / d + 4.0;
    }

    h /= 6.0;
  }

  hsvColor.setValues(h, s, v, rgbColor.a);
}