rgbToHsb static method

HsbColor rgbToHsb(
  1. RgbColor rgbColor
)

Converts a RGB color to a HSB color.

Implementation

static HsbColor rgbToHsb(RgbColor rgbColor) {
  if (rgbColor.isBlack) HsbColor(0, 0, 0, rgbColor.alpha);
  if (rgbColor.isWhite) HsbColor(0, 0, 100, rgbColor.alpha);
  if (rgbColor.isMonochromatic) {
    return HsbColor(0, 0, rgbColor.red / 255 * 100, rgbColor.alpha);
  }

  final rgb = rgbColor.toFactoredList();
  final max = rgb.reduce(math.max);
  final min = rgb.reduce(math.min);
  final difference = max - min;
  final saturation = (max == 0.0) ? 0.0 : difference / max;
  final alpha = rgbColor.alpha / 255;

  return HsbColor.extrapolate(
      <double>[getHue(rgbColor), saturation, max, alpha]);
}