blendColor function

int blendColor(
  1. int top,
  2. int bottom
)

Blends two ARGB colors (resolving Color.ansi indices via legacy VGA when blended under a translucent overlay).

Implementation

int blendColor(int top, int bottom) {
  if (bottom == 0) return top;
  if (top == 0) return bottom;
  if (top.isAnsiIndexed) return top;

  final aTop = (top >> 24) & 0xFF;
  if (aTop == 255) return top;

  final resolvedBottom = bottom.isAnsiIndexed
      ? TerminalColorTheme.legacyVga.resolveAnsi256(bottom.ansiIndex).argb
      : bottom;

  final rTop = (top >> 16) & 0xFF;
  final gTop = (top >> 8) & 0xFF;
  final bTop = top & 0xFF;

  final aBottom = (resolvedBottom >> 24) & 0xFF;
  final rBottom = (resolvedBottom >> 16) & 0xFF;
  final gBottom = (resolvedBottom >> 8) & 0xFF;
  final bBottom = resolvedBottom & 0xFF;

  final invAlpha = 255 - aTop;
  final effectiveBottomA = (aBottom * invAlpha) ~/ 255;
  final outA = aTop + effectiveBottomA;
  if (outA == 0) return 0;

  final outR = (rTop * aTop + rBottom * effectiveBottomA) ~/ outA;
  final outG = (gTop * aTop + gBottom * effectiveBottomA) ~/ outA;
  final outB = (bTop * aTop + bBottom * effectiveBottomA) ~/ outA;

  return (outA << 24) | (outR << 16) | (outG << 8) | outB;
}