rgbToHsl 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 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 l = (max + min) / 2.0;
  double h = 0.0;
  double s = 0.0;

  if (max != min) {
    final double 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);
}