Use GitHub's diff colours for changed lines and +/- counts

Added and removed lines take GitHub's green and red tints, with the code in
the label colour and the +/- marker and counts in GitHub's strong green and
red, in both themes and hosts.
This commit is contained in:
2026-10-31 19:54:48 +00:00
parent 53be2e5334
commit 23ff72934e
3 changed files with 67 additions and 32 deletions
@@ -16,34 +16,47 @@ ThemeData hostTextTheme(BuildContext context) => ThemeData(
platform: Theme.of(context).platform,
);
/// Native semantic roles, never a bespoke palette or added row tint. The exact
/// recorded +/- markers also distinguish changes without relying on colour.
({Color text, Color? background}) diffLineColors(
/// GitHub's diff palette (Primer): each changed line on a green or red tint,
/// its +/- marker and the +N/-N counts in the strong green or red. The code
/// itself keeps the label colour, as on GitHub, and the exact recorded
/// markers also distinguish changes without relying on colour.
({Color text, Color marker, Color? background}) diffLineColors(
BuildContext context,
ChangeLineKind kind,
) => switch (kind) {
ChangeLineKind.add => (
text: addedColor(context),
background: addedColor(context).withValues(alpha: 0.12),
),
ChangeLineKind.remove => (
text: removedColor(context),
background: removedColor(context).withValues(alpha: 0.12),
),
ChangeLineKind.context => (text: labelColor(context), background: null),
};
) {
final dark = _isDark(context);
return switch (kind) {
ChangeLineKind.add => (
text: labelColor(context),
marker: addedColor(context),
background: dark ? const Color(0x262EA043) : const Color(0xFFDAFBE1),
),
ChangeLineKind.remove => (
text: labelColor(context),
marker: removedColor(context),
background: dark ? const Color(0x1AF85149) : const Color(0xFFFFEBE9),
),
ChangeLineKind.context => (
text: labelColor(context),
marker: labelColor(context),
background: null,
),
};
}
/// Added lines and counts: the platform's green, readable in both themes.
Color addedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.systemGreen.resolveFrom(context)
: (Theme.of(context).brightness == Brightness.dark
? const Color(0xFF81C995)
: const Color(0xFF1E8E3E));
bool _isDark(BuildContext context) =>
(usesCupertino(context)
? CupertinoTheme.brightnessOf(context)
: Theme.of(context).brightness) ==
Brightness.dark;
/// Removed lines and counts: the platform's red.
Color removedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error;
/// Added lines and counts: GitHub's addition green.
Color addedColor(BuildContext context) =>
_isDark(context) ? const Color(0xFF3FB950) : const Color(0xFF1A7F37);
/// Removed lines and counts: GitHub's deletion red.
Color removedColor(BuildContext context) =>
_isDark(context) ? const Color(0xFFF85149) : const Color(0xFFD1242F);
final _statToken = RegExp(r'(?<![\w-])([+-]\d+)(?![\w])');
@@ -114,7 +114,6 @@ class _LineRow extends StatelessWidget {
? ' ' * (width * 2 + 1)
: '${gutterText(line.oldLine, width)} ${gutterText(line.newLine, width)}';
final colors = diffLineColors(context, line.kind);
final content = '${diffLineSymbol(line.kind)} ${line.text}';
return Container(
color: colors.background,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
@@ -126,7 +125,16 @@ class _LineRow extends StatelessWidget {
style: base.copyWith(color: mutedColor(context)),
),
TextSpan(
text: content,
text: diffLineSymbol(line.kind),
style: base.copyWith(
color: colors.marker,
fontWeight: line.kind == ChangeLineKind.context
? null
: FontWeight.w700,
),
),
TextSpan(
text: ' ${line.text}',
style: base.copyWith(color: colors.text),
),
],
@@ -88,15 +88,29 @@ void main() {
await tester.pumpWidget(
materialHost(const SizedBox.shrink(), brightness: brightness),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(Scaffold));
final add = diffLineColors(context, ChangeLineKind.add);
final remove = diffLineColors(context, ChangeLineKind.remove);
// Additions are green and removals red, each line on a light tint.
expect(add.background, isNotNull);
expect(remove.background, isNotNull);
expect(add.text, isNot(remove.text), reason: '$brightness text');
expect(add.text.g, greaterThan(add.text.r), reason: 'green addition');
expect(remove.text, Theme.of(context).colorScheme.error);
// GitHub's palette: green and red line tints and markers.
final dark = brightness == Brightness.dark;
expect(
add.background,
dark ? const Color(0x262EA043) : const Color(0xFFDAFBE1),
);
expect(
remove.background,
dark ? const Color(0x1AF85149) : const Color(0xFFFFEBE9),
);
expect(
add.marker,
dark ? const Color(0xFF3FB950) : const Color(0xFF1A7F37),
);
expect(
remove.marker,
dark ? const Color(0xFFF85149) : const Color(0xFFD1242F),
);
expect(add.text, Theme.of(context).colorScheme.onSurface);
}
},
);