rgbToHsi static method
Converts a RGB color to a HSI color.
Implementation
static HsiColor rgbToHsi(RgbColor rgbColor) {
if (rgbColor.isBlack) return HsiColor(0, 0, 0, rgbColor.alpha);
if (rgbColor.isWhite) return HsiColor(0, 0, 100, rgbColor.alpha);
if (rgbColor.isMonochromatic) {
return HsiColor(0, 0, rgbColor.red / 255 * 100, rgbColor.alpha);
}
final rgb = rgbColor.toPreciseList();
final sum = rgb.reduce((a, b) => a + b);
final red = rgb[0] / sum;
final green = rgb[1] / sum;
final blue = rgb[2] / sum;
var hue = math.acos((0.5 * ((red - green) + (red - blue))) /
math.sqrt(
((red - green) * (red - green)) + ((red - blue) * (green - blue))));
if (blue > green) hue = (2 * math.pi) - hue;
if (hue.isNaN) {
// Achromatic
hue = 0;
} else {
hue /= math.pi * 2;
}
final min = <double>[red, green, blue].reduce(math.min);
final saturation = 1 - 3 * min;
final intensity = sum / 3 / 255;
final alpha = rgbColor.alpha / 255;
return HsiColor.extrapolate(<double>[hue, saturation, intensity, alpha]);
}