opacity method

AnsiColourRgb opacity(
  1. double amount, {
  2. AnsiInk over = const AnsiColourRgb(0, 0, 0),
})

Blends this colour over another one, like drawing it semi-transparent.

amount goes from 0 (only over shows) to 1 (only this colour shows); values outside 0 to 1 fail an assert. over defaults to black. Both colours are converted with toRgb first, so the result is always an AnsiColourRgb.

// Half-strength red on a white background.
final pink = const AnsiColour(9).opacity(
  0.5,
  over: const AnsiColourRgb(255, 255, 255),
);

Implementation

AnsiColourRgb opacity(
  double amount, {
  AnsiInk over = const AnsiColourRgb(0, 0, 0),
}) {
  assert(amount >= 0 && amount <= 1, 'amount must be 0-1');

  final AnsiColourRgb front = toRgb();
  final AnsiColourRgb back = over.toRgb();
  int mix(int f, int b) => ((f * amount) + (b * (1 - amount))).round();

  return AnsiColourRgb(
    mix(front.red, back.red),
    mix(front.green, back.green),
    mix(front.blue, back.blue),
  );
}