hsvToRgb function

List<int> hsvToRgb(
  1. num hue,
  2. num saturation,
  3. num brightness
)

Convert an HSV color to RGB, where h is specified in normalized degrees 0, 1 (where 1 is 360-degrees); s and l are in the range 0, 1. Returns a list r, g, b with values in the range 0, 255.

Implementation

List<int> hsvToRgb(num hue, num saturation, num brightness) {
  if (saturation == 0) {
    final gray = (brightness * 255.0).round();
    return [gray, gray, gray];
  }

  final num h = (hue - hue.floor()) * 6.0;
  final f = h - h.floor();
  final num p = brightness * (1.0 - saturation);
  final num q = brightness * (1.0 - saturation * f);
  final num t = brightness * (1.0 - (saturation * (1.0 - f)));

  switch (h.toInt()) {
    case 0:
      return [
        (brightness * 255.0).round(),
        (t * 255.0).round(),
        (p * 255.0).round()
      ];
    case 1:
      return [
        (q * 255.0).round(),
        (brightness * 255.0).round(),
        (p * 255.0).round()
      ];
    case 2:
      return [
        (p * 255.0).round(),
        (brightness * 255.0).round(),
        (t * 255.0).round()
      ];
    case 3:
      return [
        (p * 255.0).round(),
        (q * 255.0).round(),
        (brightness * 255.0).round()
      ];
    case 4:
      return [
        (t * 255.0).round(),
        (p * 255.0).round(),
        (brightness * 255.0).round()
      ];
    case 5:
      return [
        (brightness * 255.0).round(),
        (p * 255.0).round(),
        (q * 255.0).round()
      ];
    default:
      throw ImageException('invalid hue');
  }
}