Skip to content

Commit

Permalink
More improvements to theme handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Dec 4, 2020
1 parent 8061520 commit 03ce793
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 55 deletions.
Expand Up @@ -44,8 +44,7 @@ public class ParameterExpandItem extends Item {
int visiblePosition = -1;
int selectablePosition = -1;
int closePosition = -1;
Color backgroundColor =
ThemeHelper.isDark() ? GamaColors.system(SWT.COLOR_DARK_GRAY) : IGamaColors.VERY_LIGHT_GRAY.color();
Color backgroundColor = ThemeHelper.isDark() ? IGamaColors.DARK_GRAY.color() : IGamaColors.VERY_LIGHT_GRAY.color();

private static int imageHeight = 16, imageWidth = 16;
boolean isPaused = false;
Expand Down
Expand Up @@ -14,9 +14,6 @@
import java.util.HashMap;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;

import msi.gama.util.GamaColor;
Expand Down Expand Up @@ -90,51 +87,51 @@ public RGB getRGB() {
static HashMap<RGB, GamaUIColor> colors = new HashMap<>();

static Color computeInactive(final Color c) {
final RGB data = c.getRGB();
final float[] hsb = data.getHSB();
final float[] newHsb = new float[3];
final var data = c.getRGB();
final var hsb = data.getHSB();
final var newHsb = new float[3];
newHsb[0] = hsb[0];
newHsb[1] = hsb[1] / 2;
newHsb[2] = Math.min(1.0f, hsb[2] + 0.2f);
final RGB newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
final var newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
return getColor(newData.red, newData.green, newData.blue);
}

static Color computeDarker(final Color c) {
final RGB data = c.getRGB();
final float[] hsb = data.getHSB();
final float[] newHsb = new float[3];
final var data = c.getRGB();
final var hsb = data.getHSB();
final var newHsb = new float[3];
newHsb[0] = hsb[0];
newHsb[1] = hsb[1];
newHsb[2] = Math.max(0.0f, hsb[2] - 0.1f);
final RGB newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
final var newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
return getColor(newData.red, newData.green, newData.blue);
}

static Color computeReverse(final Color c) {
final RGB data = c.getRGB();
final var data = c.getRGB();
return getColor(255 - data.red, 255 - data.green, 255 - data.blue);
}

static Color computeLighter(final Color c) {
final RGB data = c.getRGB();
final float[] hsb = data.getHSB();
final float[] newHsb = new float[3];
final var data = c.getRGB();
final var hsb = data.getHSB();
final var newHsb = new float[3];
newHsb[0] = hsb[0];
newHsb[1] = hsb[1];
newHsb[2] = Math.min(1f, hsb[2] + 0.2f);
final RGB newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
final var newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
return getColor(newData.red, newData.green, newData.blue);
}

static Color computeGray(final Color c) {
final RGB data = c.getRGB();
final float[] hsb = data.getHSB();
final float[] newHsb = new float[3];
final var data = c.getRGB();
final var hsb = data.getHSB();
final var newHsb = new float[3];
newHsb[0] = hsb[0];
newHsb[1] = 0.0f;
newHsb[2] = hsb[2];
final RGB newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
final var newData = new RGB(newHsb[0], newHsb[1], newHsb[2]);
return getColor(newData.red, newData.green, newData.blue);
}

Expand All @@ -149,20 +146,25 @@ public static GamaUIColor get(final java.awt.Color color) {

public static GamaUIColor get(final RGB rgb) {
if (rgb == null) { return null; }
GamaUIColor c = colors.get(rgb);
var c = colors.get(rgb);
if (c == null) {
final Color cc = getColor(rgb.red, rgb.green, rgb.blue);
final var cc = getColor(rgb.red, rgb.green, rgb.blue);
c = new GamaUIColor(cc);
colors.put(rgb, c);
}
return c;
}

public static GamaUIColor get(final Color color) {
if (color == null) { return null; }
return get(color.getRGB());
}

public static GamaUIColor get(final int r, final int g, final int b) {
final int r1 = r < 0 ? 0 : r > 255 ? 255 : r;
final int g1 = g < 0 ? 0 : g > 255 ? 255 : g;
final int b1 = b < 0 ? 0 : b > 255 ? 255 : b;
final RGB rgb = new RGB(r1, g1, b1);
final var r1 = r < 0 ? 0 : r > 255 ? 255 : r;
final var g1 = g < 0 ? 0 : g > 255 ? 255 : g;
final var b1 = b < 0 ? 0 : b > 255 ? 255 : b;
final var rgb = new RGB(r1, g1, b1);
return get(rgb);
}

Expand All @@ -174,10 +176,10 @@ public static GamaUIColor get(final int... c) {
if (c.length >= 3) {
return get(c[0], c[1], c[2]);
} else {
final int rgb = c[0];
final int red = rgb >> 16 & 0xFF;
final int green = rgb >> 8 & 0xFF;
final int blue = rgb & 0xFF;
final var rgb = c[0];
final var red = rgb >> 16 & 0xFF;
final var green = rgb >> 8 & 0xFF;
final var blue = rgb & 0xFF;
return get(red, green, blue);
}
}
Expand All @@ -189,10 +191,10 @@ public static GamaUIColor get(final int... c) {
* @return
*/
public static GamaUIColor get(final GamaIcon icon) {
final Image image = icon.image();
final ImageData data = image.getImageData();
final PaletteData palette = data.palette;
final int pixelValue = data.getPixel(0, 0);
final var image = icon.image();
final var data = image.getImageData();
final var palette = data.palette;
final var pixelValue = data.getPixel(0, 0);
return get(palette.getRGB(pixelValue));
}

Expand All @@ -202,7 +204,7 @@ public static GamaUIColor get(final GamaIcon icon) {
*/
public static boolean isDark(final Color color) {
return luminanceOf(color) < 130;
};
}

public static int luminanceOf(final Color color) {
return (int) (0.299 * color.getRed() * color.getRed() / 255 + 0.587 * color.getGreen() * color.getGreen() / 255
Expand Down
Expand Up @@ -11,9 +11,13 @@
**********************************************************************************************/
package ummisco.gama.ui.resources;

import static msi.gama.application.workbench.ThemeHelper.isDark;
import static ummisco.gama.ui.resources.GamaColors.get;
import static ummisco.gama.ui.resources.GamaColors.system;
import static ummisco.gama.ui.resources.GamaIcons.create;

import org.eclipse.swt.SWT;

import msi.gama.application.workbench.ThemeHelper;
import ummisco.gama.ui.resources.GamaColors.GamaUIColor;

/**
Expand All @@ -25,20 +29,21 @@
*/
public interface IGamaColors {

GamaUIColor BLUE = GamaColors.get(GamaIcons.create("palette/palette.blue2")).validate();
GamaUIColor ERROR = GamaColors.get(GamaIcons.create("palette/palette.red2")).validate();
GamaUIColor OK = GamaColors.get(GamaIcons.create("palette/palette.green2")).validate();
GamaUIColor WARNING = GamaColors.get(GamaIcons.create("palette/palette.orange2")).validate();
GamaUIColor NEUTRAL = GamaColors.get(GamaIcons.create("palette/palette.gray2")).validate();
GamaUIColor TOOLTIP = GamaColors.get(GamaIcons.create("palette/palette.yellow2")).validate();
GamaUIColor GRAY_LABEL = GamaColors.get(0x88, 0x88, 0x88).validate();
GamaUIColor VERY_LIGHT_GRAY = GamaColors.get(245, 245, 245).validate();
GamaUIColor VERY_DARK_GRAY = GamaColors.get(20, 20, 20).validate();
GamaUIColor WHITE =
new GamaUIColor(GamaColors.system(SWT.COLOR_WHITE), GamaColors.system(SWT.COLOR_WHITE)).validate();
GamaUIColor BLACK = new GamaUIColor(GamaColors.system(SWT.COLOR_BLACK)).validate();
GamaUIColor PARAMETERS_BACKGROUND =
(ThemeHelper.isDark() ? GamaColors.get(120, 120, 120) : GamaColors.get(255, 255, 255)).validate();
GamaUIColor DARK_ORANGE = GamaColors.get(225, 92, 15).validate();
GamaUIColor BLUE = isDark() ? GamaColors.get(get(create("palette/palette.blue2")).lighter())
: get(create("palette/palette.blue2"));
GamaUIColor ERROR = isDark() ? GamaColors.get(get(create("palette/palette.red2")).lighter())
: get(create("palette/palette.red2"));
GamaUIColor OK = isDark() ? GamaColors.get(get(create("palette/palette.green2")).lighter())
: get(create("palette/palette.green2"));
GamaUIColor WARNING = get(create("palette/palette.orange2"));
GamaUIColor NEUTRAL = get(create("palette/palette.gray2"));
GamaUIColor TOOLTIP = get(create("palette/palette.yellow2"));
GamaUIColor GRAY_LABEL = get(0x88, 0x88, 0x88);
GamaUIColor VERY_LIGHT_GRAY = get(245, 245, 245);
GamaUIColor DARK_GRAY = new GamaUIColor(system(SWT.COLOR_DARK_GRAY));
GamaUIColor WHITE = new GamaUIColor(system(SWT.COLOR_WHITE), system(SWT.COLOR_WHITE));
GamaUIColor BLACK = new GamaUIColor(system(SWT.COLOR_BLACK));
GamaUIColor PARAMETERS_BACKGROUND = (isDark() ? get(120, 120, 120) : get(255, 255, 255));
GamaUIColor DARK_ORANGE = get(225, 92, 15);

}
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;

import msi.gama.application.workbench.ThemeHelper;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.common.preferences.Pref;
import msi.gama.metamodel.shape.GamaPoint;
Expand All @@ -55,6 +56,7 @@
import ummisco.gama.ui.parameters.EditorFactory;
import ummisco.gama.ui.resources.GamaFonts;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.WorkbenchHelper;
import ummisco.gama.ui.views.toolbar.Selector;
Expand Down Expand Up @@ -117,13 +119,14 @@ private GamaPreferencesView(final Shell parent) {
gridLayout.marginWidth = gridLayout.marginHeight = 5;
gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 5;
shell.setLayout(gridLayout);
// shell.setBackground(ThemeHelper.isDark() ? IGamaColors.WHITE.color() : IGamaColors.BLACK.color());
buildContents();
}

private void buildContents() {
tabFolder = new CTabFolder(shell, SWT.TOP | SWT.NO_TRIM);
tabFolder.setBorderVisible(true);
tabFolder.setBackgroundMode(SWT.INHERIT_DEFAULT);
// tabFolder.setBackgroundMode(SWT.INHERIT_DEFAULT);
tabFolder.setMRUVisible(true);
tabFolder.setSimple(false); // rounded tabs
tabFolder.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
Expand All @@ -145,6 +148,8 @@ private void buildContentsFor(final CTabItem tab, final Map<String, List<Pref>>
contents.add(viewer);
final var data = new GridData(SWT.FILL, SWT.FILL, true, true);
// viewer.setBackgroundMode(SWT.INHERIT_DEFAULT);
viewer.setBackground(
!ThemeHelper.isDark() ? IGamaColors.VERY_LIGHT_GRAY.color() : IGamaColors.DARK_GRAY.darker());
viewer.setLayoutData(data);
// ?
viewer.computeSize(tab.getBounds().x, SWT.DEFAULT);
Expand Down Expand Up @@ -214,7 +219,7 @@ private void buildGroupContents(final Composite compo, final List<Pref> list, fi
final var comps = new Composite[nbColumns];
for (var i = 0; i < nbColumns; i++) {
comps[i] = new Composite(compo, SWT.BORDER);
comps[i].setBackground(compo.getBackground());
// comps[i].setBackground(compo.getBackground());
GridLayoutFactory.fillDefaults().numColumns(2).spacing(0, 0).equalWidth(false).applyTo(comps[i]);
GridDataFactory.fillDefaults().grab(true, true).applyTo(comps[i]);
}
Expand Down

0 comments on commit 03ce793

Please sign in to comment.