rgbToHsv static method

void rgbToHsv(
  1. Vector4 rgbColor,
  2. 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 max = math.max(math.max(rgbColor.r, rgbColor.g), rgbColor.b);
  final min = math.min(math.min(rgbColor.r, rgbColor.g), rgbColor.b);
  final d = max - min;
  final v = max;
  final s = max == 0.0 ? 0.0 : d / max;
  var 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);
}