GhosttyTerminalResolvedStyle.fromNativeStyle constructor

GhosttyTerminalResolvedStyle.fromNativeStyle({
  1. required VtStyle style,
  2. required List<Color> palette,
  3. required Color defaultForeground,
  4. required Color defaultBackground,
})

Implementation

factory GhosttyTerminalResolvedStyle.fromNativeStyle({
  required VtStyle style,
  required List<Color> palette,
  required Color defaultForeground,
  required Color defaultBackground,
}) {
  Color resolveStyleColor(
    VtStyleColor color, {
    required Color fallback,
    required List<Color> palette,
  }) {
    if (!color.isSet) {
      return fallback;
    }
    final rgb = color.rgb;
    if (rgb != null) {
      return Color.fromARGB(0xFF, rgb.r, rgb.g, rgb.b);
    }
    final index = color.paletteIndex;
    if (index == null) {
      return fallback;
    }
    if (index >= 0 && index < palette.length) {
      return palette[index];
    }
    return GhosttyTerminalPalette.xterm.resolve(
      GhosttyTerminalColor.palette(index),
      fallback: fallback,
    );
  }

  final hasExplicitForeground = style.foreground.isSet;
  final hasExplicitBackground = style.background.isSet;
  final hasExplicitUnderlineColor = style.underlineColor.isSet;
  const transparent = Color(0x00000000);

  var foreground = hasExplicitForeground
      ? resolveStyleColor(
          style.foreground,
          fallback: defaultForeground,
          palette: palette,
        )
      : transparent;
  var background = hasExplicitBackground
      ? resolveStyleColor(
          style.background,
          fallback: defaultBackground,
          palette: palette,
        )
      : transparent;

  if (style.inverse) {
    final swappedForeground = background;
    background = hasExplicitForeground
        ? foreground
        : (foreground == transparent ? defaultForeground : foreground);
    foreground = hasExplicitBackground
        ? swappedForeground
        : (swappedForeground == transparent
              ? defaultBackground
              : swappedForeground);
  }
  if (style.invisible) {
    foreground = background == transparent ? defaultBackground : background;
  }
  if (style.faint) {
    foreground = foreground.withValues(alpha: 0.72);
  }
  if (foreground == transparent) {
    foreground = defaultForeground;
  }

  return GhosttyTerminalResolvedStyle(
    foreground: foreground,
    background: background,
    underlineColor: resolveStyleColor(
      style.underlineColor,
      fallback: hasExplicitUnderlineColor ? defaultForeground : transparent,
      palette: palette,
    ),
    foregroundToken: _toTerminalColor(style.foreground),
    backgroundToken: _toTerminalColor(style.background),
    underlineColorToken: _toTerminalColor(style.underlineColor),
    hasExplicitUnderlineColor: hasExplicitUnderlineColor,
    hasExplicitForeground: hasExplicitForeground,
    hasExplicitBackground: hasExplicitBackground,
    inverse: style.inverse,
    invisible: style.invisible,
    faint: style.faint,
    blink: style.blink,
    bold: style.bold,
    italic: style.italic,
    overline: style.overline,
    strikethrough: style.strikethrough,
    underline: style.underline,
  );
}