diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java index 4d80b783e12..284a749d5c2 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java @@ -364,9 +364,10 @@ public ColorPalette(RGB background, String registryKey, RGB defaultRGB) { } else { selected = defaultRGB; } - normal = DiffColors.interpolate(selected, background, DiffColors.BORDER_SCALE); - fill = DiffColors.interpolate(selected, background, DiffColors.FILL_SCALE); - textFill = DiffColors.interpolate(selected, background, DiffColors.TEXT_FILL_SCALE); + boolean dark = DiffColors.isDarkTheme(); + normal = DiffColors.interpolate(selected, background, DiffColors.borderScale(dark)); + fill = DiffColors.interpolate(selected, background, DiffColors.fillScale(dark)); + textFill = DiffColors.interpolate(selected, background, DiffColors.detailScale(dark)); } } diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DiffColors.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DiffColors.java index 564e2e95301..da430cf6aa7 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DiffColors.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DiffColors.java @@ -14,6 +14,7 @@ package org.eclipse.compare.internal; import org.eclipse.swt.graphics.RGB; +import org.eclipse.ui.PlatformUI; /** * The shared color palette for diff rendering. Both the traditional two-way @@ -35,9 +36,33 @@ private DiffColors() { /** The hunk fill band; the lightest of the three. */ public static final double FILL_SCALE = 0.9; - /** Border color of an addition/deletion hunk; darker than the fill band. */ + private static final String INFORMATION_BACKGROUND_COLOR = "org.eclipse.ui.workbench.INFORMATION_BACKGROUND"; //$NON-NLS-1$ + + /** Returns {@code true} when the active workbench theme is dark. */ + public static boolean isDarkTheme() { + var reg = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry(); + var color = reg.getRGB(INFORMATION_BACKGROUND_COLOR); + return color != null && color.red + color.green + color.blue < 3 * 128; + } + + /** Scale for the border/stroke color; adapts to dark vs. light themes. */ + public static double borderScale(boolean dark) { + return dark ? 0.3 : BORDER_SCALE; + } + + /** Scale for the line-band fill; adapts to dark vs. light themes. */ + public static double fillScale(boolean dark) { + return dark ? 0.79 : FILL_SCALE; + } + + /** Scale for the word-level detail highlight; adapts to dark vs. light themes. */ + public static double detailScale(boolean dark) { + return dark ? 0.5 : TEXT_FILL_SCALE; + } + + /** Border color of an addition/deletion hunk; adapts to dark vs. light themes. */ public static RGB borderColor(RGB diffColor, RGB background) { - return interpolate(diffColor, background, BORDER_SCALE); + return interpolate(diffColor, background, borderScale(isDarkTheme())); } /** diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffCodeMiningProvider.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffCodeMiningProvider.java index c7ff124dabf..7692ecf93f0 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffCodeMiningProvider.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffCodeMiningProvider.java @@ -157,8 +157,9 @@ public CompletableFuture> provideCodeMinings(ITextVi this.foldButtonColor.dispose(); } // the word-level diff is tinted stronger so it stands out against the band - this.detailedDiffColor = new Color(interpolate(deletionColor, background, DiffColors.TEXT_FILL_SCALE)); - this.deletionBackgroundColor = new Color(interpolate(deletionColor, background, DiffColors.FILL_SCALE)); + boolean dark = DiffColors.isDarkTheme(); + this.detailedDiffColor = new Color(interpolate(deletionColor, background, DiffColors.detailScale(dark))); + this.deletionBackgroundColor = new Color(interpolate(deletionColor, background, DiffColors.fillScale(dark))); this.borderColor = new Color(UnifiedDiffText.borderColor(deletionColor, background)); this.foldSeparatorColor = new Color(separatorBackground(background)); this.foldButtonColor = new Color(buttonBackground(background)); diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java index b6d14021740..225aa42ba16 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/unifieddiff/internal/UnifiedDiffManager.java @@ -31,6 +31,7 @@ import org.eclipse.compare.contentmergeviewer.ITokenComparator; import org.eclipse.compare.contentmergeviewer.TokenComparator; import org.eclipse.compare.internal.CompareMessages; +import org.eclipse.compare.internal.DiffColors; import org.eclipse.compare.internal.DocLineComparator; import org.eclipse.compare.rangedifferencer.IRangeComparator; import org.eclipse.compare.rangedifferencer.RangeDifference; @@ -1822,7 +1823,8 @@ public UnifiedDiffPaintListener(ITextViewer viewer, IAnnotationModel model, Unif color = JFaceResources.getColorRegistry().getRGB("ADDITION_COLOR"); //$NON-NLS-1$ } RGB background = UnifiedDiffCodeMiningProvider.getBackground(); - RGB interpolated = UnifiedDiffCodeMiningProvider.interpolate(color, background, 0.9); + boolean dark = DiffColors.isDarkTheme(); + RGB interpolated = DiffColors.interpolate(color, background, DiffColors.fillScale(dark)); this.additionBackgroundColor = new Color(interpolated); this.borderColor = new Color(UnifiedDiffText.borderColor(color, background)); } diff --git a/team/bundles/org.eclipse.compare/resources/css/dark.css b/team/bundles/org.eclipse.compare/resources/css/dark.css index 3fe06399e98..8349594f4d4 100644 --- a/team/bundles/org.eclipse.compare/resources/css/dark.css +++ b/team/bundles/org.eclipse.compare/resources/css/dark.css @@ -1,7 +1,7 @@ IEclipsePreferences#org-eclipse-ui-editors:org-eclipse-compare { preferences: - 'inlineDiffColor=27,40,30' - 'detailedInlineDiffColor=24,50,27' - 'deletionInlineDiffColor=52,27,30' - 'detailedDeletionInlineDiffColor=74,24,27' + 'inlineDiffColor=23,51,26' + 'detailedInlineDiffColor=15,79,17' + 'deletionInlineDiffColor=77,24,26' + 'detailedDeletionInlineDiffColor=142,15,17' } diff --git a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllCompareTests.java b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllCompareTests.java index 9fed2f0de76..97e85efbc96 100644 --- a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllCompareTests.java +++ b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllCompareTests.java @@ -23,6 +23,7 @@ @Suite @SelectClasses({ TextMergeViewerTest.class, + DiffColorsTest.class, LineReaderTest.class, StreamMergerTest.class, DocLineComparatorTest.class, diff --git a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DiffColorsTest.java b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DiffColorsTest.java new file mode 100644 index 00000000000..6a9719c3497 --- /dev/null +++ b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DiffColorsTest.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2026 Eclipse contributors and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eclipse contributors - initial implementation + *******************************************************************************/ +package org.eclipse.compare.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; + +import org.eclipse.compare.internal.DiffColors; +import org.eclipse.core.runtime.Platform; +import org.eclipse.swt.graphics.RGB; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("restriction") +public class DiffColorsTest { + + @Test + public void testLightThemeKeepsExistingPalette() { + RGB addition = new RGB(0, 128, 0); + RGB background = new RGB(255, 255, 255); + assertEquals(new RGB(153, 204, 153), + DiffColors.interpolate(addition, background, DiffColors.borderScale(false))); + assertEquals(new RGB(229, 242, 229), + DiffColors.interpolate(addition, background, DiffColors.fillScale(false))); + assertEquals(new RGB(204, 229, 204), + DiffColors.interpolate(addition, background, DiffColors.detailScale(false))); + } + + @Test + public void testDarkPaletteMatchesAnnotationColors() throws Exception { + String css; + try (var stream = Platform.getBundle("org.eclipse.compare").getEntry("resources/css/dark.css").openStream()) { + css = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + } + RGB background = new RGB(30, 31, 34); + RGB addition = new RGB(0, 128, 0); + RGB deletion = new RGB(255, 0, 0); + assertEquals(annotationColor(css, "inlineDiffColor"), + DiffColors.interpolate(addition, background, DiffColors.fillScale(true))); + assertEquals(annotationColor(css, "detailedInlineDiffColor"), + DiffColors.interpolate(addition, background, DiffColors.detailScale(true))); + assertEquals(annotationColor(css, "deletionInlineDiffColor"), + DiffColors.interpolate(deletion, background, DiffColors.fillScale(true))); + assertEquals(annotationColor(css, "detailedDeletionInlineDiffColor"), + DiffColors.interpolate(deletion, background, DiffColors.detailScale(true))); + } + + private static RGB annotationColor(String css, String key) { + var matcher = Pattern.compile("'" + key + "=(\\d+),(\\d+),(\\d+)'").matcher(css); + assertTrue(matcher.find(), "annotation preference must be present: " + key); + return new RGB(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), + Integer.parseInt(matcher.group(3))); + } + + @Test + public void testCustomizedBaseColorIsPreserved() { + RGB customized = new RGB(123, 76, 210); + RGB background = new RGB(30, 31, 34); + assertEquals(new RGB(76, 53, 122), + DiffColors.interpolate(customized, background, DiffColors.detailScale(true))); + assertEquals(new RGB(123, 76, 210), customized); + } +}