rgbToHsl static method

void rgbToHsl(
  1. Vector4 rgbColor,
  2. Vector4 hslColor
)

Convert rgbColor from rgb color model to the hue, saturation, and lightness (HSL) color model and store it in hslColor.

Implementation

static void rgbToHsl(Vector4 rgbColor, Vector4 hslColor) {
  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 l = (max + min) / 2.0;
  var h = 0.0;
  var s = 0.0;

  if (max != min) {
    final d = max - min;

    s = l > 0.5 ? d / (2.0 - max - min) : d / (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;
  }

  hslColor.setValues(h, s, l, rgbColor.a);
}