ConsoleColorRGB constructor

ConsoleColorRGB(
  1. num red,
  2. num? green,
  3. num? blue
)

When red, green and blue are integers, expects them to be in range 0 - 255. Values out of range will be clamped.

When red, green and blue are doubles, expects them to be in range 0 - 1. Values out of range will be clamped.

Integer and double values can be mixed.

When red is an integer and green and blue are null, expects red to be a rgb color code.

Examples:

final color = ConsoleColorRGB(255, 0, 0); // will interpret as rgb(255, 0, 0)
final color = ConsoleColorRGB(1, 0, 0); // will interpret as rgb(255, 0, 0)
final color = ConsoleColorRGB(0xFF0000); // will interpret as rgb(255, 0, 0)
final color = ConsoleColorRGB(0xFF0000, 0); // not allowed
final color = ConsoleColorRGB(300, 0, 0); // will be clamped to rgb(255, 0, 0)
final color = ConsoleColorRGB(1.5, -1, 0); // will be clamped to rgb(255, 0, 0)

Implementation

factory ConsoleColorRGB(num red, num? green, num? blue) {
  if (red is int && (green == null || blue == null)) {
    assert(blue == null && green == null);
    return ConsoleColorRGB._(red >> 16 & 0xFF, red >> 8 & 0xFF, red & 0xFF);
  } else {
    return ConsoleColorRGB._(
      red is int ? red.clamp(0, 255) : (red.clamp(0, 1) * 255).round(),
      green is int ? green.clamp(0, 255) : (red.clamp(0, 1) * 255).round(),
      blue is int ? blue.clamp(0, 255) : (red.clamp(0, 1) * 255).round(),
    );
  }
}