Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ public CompletableFuture<List<? extends ICodeMining>> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
8 changes: 4 additions & 4 deletions team/bundles/org.eclipse.compare/resources/css/dark.css
Original file line number Diff line number Diff line change
@@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@Suite
@SelectClasses({
TextMergeViewerTest.class,
DiffColorsTest.class,
LineReaderTest.class,
StreamMergerTest.class,
DocLineComparatorTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading