hsvToRGB function

({int b, int g, int r}) hsvToRGB(
  1. double h,
  2. double s,
  3. double v
)

Implementation

({int r, int g, int b}) hsvToRGB(double h, double s, double v) {
  final chroma = v * s;
  final huePrime = h / 60.0;
  final x = chroma * (1 - (huePrime % 2 - 1).abs());
  final m = v - chroma;

  double r, g, b;

  if (huePrime >= 0 && huePrime < 1) {
    r = chroma;
    g = x;
    b = 0;
  } else if (huePrime >= 1 && huePrime < 2) {
    r = x;
    g = chroma;
    b = 0;
  } else if (huePrime >= 2 && huePrime < 3) {
    r = 0;
    g = chroma;
    b = x;
  } else if (huePrime >= 3 && huePrime < 4) {
    r = 0;
    g = x;
    b = chroma;
  } else if (huePrime >= 4 && huePrime < 5) {
    r = x;
    g = 0;
    b = chroma;
  } else {
    r = chroma;
    g = 0;
    b = x;
  }

  final red = ((r + m) * 255).round();
  final green = ((g + m) * 255).round();
  final blue = ((b + m) * 255).round();

  return (r: red, g: green, b: blue);
}