Skip to content

Commit 5791ed3

Browse files
killingermakurtakov
authored andcommitted
Update Snippet384.java
Changed HSB disabling to a more genralized version, added eclipse mojo disabling and dark themes.
1 parent abaa864 commit 5791ed3

File tree

1 file changed

+157
-66
lines changed
  • examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets

1 file changed

+157
-66
lines changed

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet384.java

Lines changed: 157 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.*;
1515
import java.util.*;
1616
import java.util.List;
17+
import java.util.function.*;
1718

1819
import org.eclipse.swt.*;
1920
import org.eclipse.swt.graphics.*;
@@ -24,27 +25,34 @@ public class Snippet384 {
2425

2526
private static Display display = new Display();
2627

28+
private static final boolean ENABLE_DARK_MODE = false;
29+
30+
private static final Color LIGHT_THEME_BACKGROUND = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
31+
private static final Color DARK_THEME_GRAY = new Color(47, 47, 47);
32+
private static final Color DARK_THEME_GRAY_DARKER = new Color(72, 72, 76);
33+
2734
// colors and thresholds used in the current Win32/macOS algorithm
28-
private static final RGB GRAY_LOW = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW).getRGB(); // == RGB(160,
29-
// 160, 160)
30-
// on
31-
// Windows
32-
private static final RGB GRAY_HIGH = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB(); // == RGB(240,
33-
// 240, 240) on
34-
// Windows
35-
36-
private static final int WIN32_COCOA_THRESHOLD = 98304; // == 3 * 2^15, this is the real threshold used in
37-
// Win32/macOS
38-
private static final int BACKGROUND_THRESHOLD = GRAY_HIGH.red * GRAY_HIGH.red + GRAY_HIGH.green + GRAY_HIGH.green
39-
+ GRAY_HIGH.blue * GRAY_HIGH.blue; // == on windows 240^2 * 3
35+
private static final RGB GRAY_LOW = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW).getRGB(); // == RGB(160, 160, 160) on Windows
36+
37+
private static final RGB GRAY_HIGH = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB(); // == RGB(240, 240,240) on Windows
38+
39+
private static final int WIN32_COCOA_THRESHOLD = 98304; // == 3 * 2^15, this is the real threshold used in Win32/macOS
40+
41+
private static final int BACKGROUND_THRESHOLD = GRAY_HIGH.red * GRAY_HIGH.red + GRAY_HIGH.green + GRAY_HIGH.green + GRAY_HIGH.blue * GRAY_HIGH.blue; // == on windows 240^2 * 3
4042

4143
private static final int MAX_BRIGHTNESS = 256 * 256 * 3;
4244

4345
// change these to adjust the sliders default values
4446
private static final int ADJUSTABLE_THRESHOLD_LOW_DEFAULT = WIN32_COCOA_THRESHOLD;
4547
private static final int ADJUSTABLE_THRESHOLD_HIGH_DEFAULT = BACKGROUND_THRESHOLD;
4648
private static final RGB ADDITIONAL_GRAY_MID_DEFAULT = new RGB(214, 214, 214); // arbitrarily chosen
49+
4750
private static final int HSB_DISABLING_BRIGHTNESS_DEFAULT = 90;
51+
private static final int HSB_DISABLING_ALPHA_DEFAULT = 100;
52+
private static final int HSB_DISABLING_SATURATION_DEFAULT = 100;
53+
54+
private static final int ECLIPSE_RENDER_MOJOE_BRIGHTNESS_DEFAULT = 290;
55+
private static final int ECLIPSE_RENDER_MOJO_ALPHA_DEFAULT = 100;
4856

4957
public static void main(String[] args) {
5058

@@ -64,66 +72,80 @@ public static void main(String[] args) {
6472
}
6573

6674
Map<PixelTransformer, List<Label>> imageRowStorage = new HashMap<>();
67-
6875
List<Image> originalImages = loadImagesFromFolder(selectedDirectory, display);
6976

7077
// define PixelTransformers
7178
GTKDisablingTransformer gtkTransformer = new GTKDisablingTransformer();
72-
ThresholdBasedDisablingTransformer win32MacTransformer = new ThresholdBasedDisablingTransformer(GRAY_LOW,
73-
GRAY_HIGH, GRAY_HIGH, WIN32_COCOA_THRESHOLD, WIN32_COCOA_THRESHOLD);
74-
HSBConversionDisablingTransformer hsbTransformer = new HSBConversionDisablingTransformer(
75-
HSB_DISABLING_BRIGHTNESS_DEFAULT / 100f);
76-
ThresholdBasedDisablingTransformer adjThreshAdditGray = new ThresholdBasedDisablingTransformer(GRAY_LOW,
77-
ADDITIONAL_GRAY_MID_DEFAULT, GRAY_HIGH, ADJUSTABLE_THRESHOLD_LOW_DEFAULT,
78-
ADJUSTABLE_THRESHOLD_HIGH_DEFAULT);
79+
ThresholdBasedDisablingTransformer win32MacTransformer = new ThresholdBasedDisablingTransformer(GRAY_LOW, GRAY_HIGH, GRAY_HIGH, WIN32_COCOA_THRESHOLD, WIN32_COCOA_THRESHOLD);
80+
EclipseRenderMojoDisablingTransformer eclipseMojoTransformer = new EclipseRenderMojoDisablingTransformer(ECLIPSE_RENDER_MOJOE_BRIGHTNESS_DEFAULT / 100f, ECLIPSE_RENDER_MOJO_ALPHA_DEFAULT / 100.f);
81+
HSBConversionDisablingTransformer hsbTransformer = new HSBConversionDisablingTransformer(HSB_DISABLING_BRIGHTNESS_DEFAULT / 100.f, HSB_DISABLING_ALPHA_DEFAULT / 100.f, HSB_DISABLING_SATURATION_DEFAULT / 100.f);
82+
ThresholdBasedDisablingTransformer adjThreshAdditGray = new ThresholdBasedDisablingTransformer(GRAY_LOW, ADDITIONAL_GRAY_MID_DEFAULT, GRAY_HIGH, ADJUSTABLE_THRESHOLD_LOW_DEFAULT, ADJUSTABLE_THRESHOLD_HIGH_DEFAULT);
7983

84+
// create transformed icon rows
8085
Composite imagesComposite = new Composite(shell, SWT.NONE);
86+
imagesComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
8187
imagesComposite.setLayout(new GridLayout(2, false));
82-
83-
// create transformed icon rows
8488
addTransformationRow(imagesComposite, "Original Images:", rgba -> rgba, originalImages, imageRowStorage);
85-
addTransformationRow(imagesComposite, "(current) Win32/macOS:", win32MacTransformer, originalImages,
86-
imageRowStorage);
89+
addTransformationRow(imagesComposite, "(current) Win32/macOS:", win32MacTransformer, originalImages, imageRowStorage);
90+
addTransformationRow(imagesComposite, "(current) Eclipsde Pre-Disabling Mojo", eclipseMojoTransformer, originalImages, imageRowStorage);
8791
addTransformationRow(imagesComposite, "(current) GTK+:", gtkTransformer, originalImages, imageRowStorage);
88-
addTransformationRow(imagesComposite, "(proposed) HSB Adding Brightness:", hsbTransformer, originalImages,
89-
imageRowStorage);
90-
addTransformationRow(imagesComposite, "(proposed) Adjustable Threshold + 3rd Gray Tone:", adjThreshAdditGray,
91-
originalImages, imageRowStorage);
92-
93-
Composite slidersComposite = new Composite(shell, SWT.NONE);
94-
slidersComposite.setLayout(new GridLayout(3, false));
95-
96-
Scale lowScale = addScale(slidersComposite, "Low Threshold:", 0, MAX_BRIGHTNESS, WIN32_COCOA_THRESHOLD);
97-
Scale highScale = addScale(slidersComposite, "High Threshold:", 0, MAX_BRIGHTNESS,
98-
ADJUSTABLE_THRESHOLD_HIGH_DEFAULT);
99-
Scale thirdGray = addScale(slidersComposite, "3rd Gray:", GRAY_LOW.red, GRAY_HIGH.red,
100-
ADDITIONAL_GRAY_MID_DEFAULT.red);
101-
Scale hsbDisablingBrightness = addScale(slidersComposite, "HSB Brightness Percentage:", 40, 140,
102-
HSB_DISABLING_BRIGHTNESS_DEFAULT);
103-
104-
lowScale.addListener(SWT.Selection, e -> {
105-
int lowThreshold = lowScale.getSelection();
106-
adjThreshAdditGray.setThresholdLow(lowThreshold);
107-
updateImageRow(adjThreshAdditGray, imageRowStorage, originalImages);
108-
});
109-
110-
highScale.addListener(SWT.Selection, e -> {
111-
int highThreshold = highScale.getSelection();
112-
adjThreshAdditGray.setThresholdHigh(highThreshold);
113-
updateImageRow(adjThreshAdditGray, imageRowStorage, originalImages);
114-
});
115-
116-
thirdGray.addListener(SWT.Selection, e -> {
117-
int grayMid = thirdGray.getSelection();
118-
adjThreshAdditGray.setGrayMid(new RGB(grayMid, grayMid, grayMid));
119-
updateImageRow(adjThreshAdditGray, imageRowStorage, originalImages);
120-
});
121-
122-
hsbDisablingBrightness.addListener(SWT.Selection, e -> {
123-
int brightnessPercentage = hsbDisablingBrightness.getSelection();
124-
hsbTransformer.setBrightnessChange(brightnessPercentage / 100f);
125-
updateImageRow(hsbTransformer, imageRowStorage, originalImages);
126-
});
92+
addTransformationRow(imagesComposite, "(proposed) HSB Transformation:", hsbTransformer, originalImages, imageRowStorage);
93+
addTransformationRow(imagesComposite, "(proposed) Adjustable Threshold + 3rd Gray Tone:", adjThreshAdditGray, originalImages, imageRowStorage);
94+
95+
// add sliders
96+
Composite hsbSliderComposite = new Composite(shell, SWT.NONE);
97+
hsbSliderComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
98+
hsbSliderComposite.setLayout(new GridLayout(9, false));
99+
Scale hsbDisablingBrightness = addScale(hsbSliderComposite, "HSB Brightness %:", 40, 140, HSB_DISABLING_BRIGHTNESS_DEFAULT);
100+
Scale hsbDisablingSaturation = addScale(hsbSliderComposite, "HSB Saturation %:", 0, 100, HSB_DISABLING_SATURATION_DEFAULT);
101+
Scale hsbDisablingAlpha = addScale(hsbSliderComposite, "HSB Alpha %:", 0, 100, HSB_DISABLING_ALPHA_DEFAULT);
102+
103+
Composite eclipseMojoSliderComposite = new Composite(shell, SWT.NONE);
104+
eclipseMojoSliderComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
105+
eclipseMojoSliderComposite.setLayout(new GridLayout(9, false));
106+
Scale eclipsePreDisablingBrightness = addScale(eclipseMojoSliderComposite, "Eclipse Mojo Brightness %:", 40, 350, ECLIPSE_RENDER_MOJOE_BRIGHTNESS_DEFAULT);
107+
Scale eclipsePreDisablingAlpha = addScale(eclipseMojoSliderComposite, "Eclipse Mojo Alpha %:", 0, 100, ECLIPSE_RENDER_MOJO_ALPHA_DEFAULT);
108+
109+
Composite adjThreshAddGraySliderComposite = new Composite(shell, SWT.NONE);
110+
adjThreshAddGraySliderComposite.setLayout(new GridLayout(9, false));
111+
adjThreshAddGraySliderComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
112+
Scale lowScale = addScale(adjThreshAddGraySliderComposite, "Low Threshold:", 0, MAX_BRIGHTNESS, WIN32_COCOA_THRESHOLD);
113+
Scale highScale = addScale(adjThreshAddGraySliderComposite, "High Threshold:", 0, MAX_BRIGHTNESS, ADJUSTABLE_THRESHOLD_HIGH_DEFAULT);
114+
Scale thirdGray = addScale(adjThreshAddGraySliderComposite, "3rd Gray:", GRAY_LOW.red, GRAY_HIGH.red, ADDITIONAL_GRAY_MID_DEFAULT.red);
115+
116+
// update images on slider change
117+
addImageUpdateScaleListener(hsbDisablingBrightness, hsbTransformer, value -> hsbTransformer.setBrightnessChange(value / 100.f), imageRowStorage, originalImages);
118+
addImageUpdateScaleListener(hsbDisablingAlpha, hsbTransformer, value -> hsbTransformer.setAlphaChange(value / 100.f), imageRowStorage, originalImages);
119+
addImageUpdateScaleListener(hsbDisablingSaturation, hsbTransformer, value -> hsbTransformer.setSaturationChange(value / 100.f), imageRowStorage, originalImages);
120+
121+
addImageUpdateScaleListener(eclipsePreDisablingBrightness, eclipseMojoTransformer, value -> eclipseMojoTransformer.setBrightnessChange(value / 100.f), imageRowStorage, originalImages);
122+
addImageUpdateScaleListener(eclipsePreDisablingAlpha, eclipseMojoTransformer, value -> eclipseMojoTransformer.setAlphaChange(value / 100.f), imageRowStorage, originalImages);
123+
124+
addImageUpdateScaleListener(lowScale, adjThreshAdditGray, adjThreshAdditGray::setThresholdLow, imageRowStorage, originalImages);
125+
addImageUpdateScaleListener(highScale, adjThreshAdditGray, adjThreshAdditGray::setThresholdHigh, imageRowStorage, originalImages);
126+
addImageUpdateScaleListener(thirdGray, adjThreshAdditGray, value -> adjThreshAdditGray.setGrayMid(new RGB(value, value, value)), imageRowStorage, originalImages);
127+
128+
129+
// combo box for theme selection
130+
Combo themeCombo = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
131+
themeCombo.setItems("Light Theme", "Dark Theme", "Darker Theme");
132+
themeCombo.select(1);
133+
themeCombo.addListener(SWT.Selection, e -> {
134+
int selected = themeCombo.getSelectionIndex();
135+
switch (selected) {
136+
case 1:
137+
shell.setBackground(DARK_THEME_GRAY);
138+
imagesComposite.setBackground(DARK_THEME_GRAY_DARKER);
139+
break;
140+
case 2:
141+
shell.setBackground(DARK_THEME_GRAY_DARKER);
142+
imagesComposite.setBackground(DARK_THEME_GRAY);
143+
break;
144+
default:
145+
shell.setBackground(LIGHT_THEME_BACKGROUND);
146+
imagesComposite.setBackground(LIGHT_THEME_BACKGROUND);
147+
}
148+
});
127149

128150
shell.open();
129151
shell.layout();
@@ -158,6 +180,14 @@ private static void addTransformationRow(Composite parent, String labelText, Pix
158180
imageRowStorage.put(transformer, transformedImageLabels);
159181
}
160182

183+
private static void addImageUpdateScaleListener(Scale scale, PixelTransformer transformer, IntConsumer transformerUpdateMethod, Map<PixelTransformer, List<Label>> transformedImageStorage, List<Image> imagesToTransformOnUpdate ) {
184+
scale.addListener(SWT.Selection, e -> {
185+
int scaleValue = scale.getSelection();
186+
transformerUpdateMethod.accept(scaleValue);
187+
updateImageRow(transformer, transformedImageStorage, imagesToTransformOnUpdate);
188+
});
189+
}
190+
161191
private static Scale addScale(Composite parent, String labelText, int minimum, int maximum, int init) {
162192
Label label = new Label(parent, SWT.NONE);
163193
label.setText(labelText);
@@ -274,24 +304,85 @@ public RGBA transform(RGBA originalRgba) {
274304
*/
275305
public static class HSBConversionDisablingTransformer implements PixelTransformer {
276306
private float brightnessChange;
307+
private float alphaChange;
308+
private float saturationChange;
277309

278-
public HSBConversionDisablingTransformer(float brightnessChange) {
310+
public HSBConversionDisablingTransformer(float brightnessChange, float alphaChange, float saturationChange) {
311+
this.saturationChange = saturationChange;
279312
this.brightnessChange = brightnessChange;
313+
this.alphaChange = alphaChange;
280314
}
281315

282316
public void setBrightnessChange(float brightnessChange) {
283317
this.brightnessChange = brightnessChange;
284318
}
285319

320+
public void setAlphaChange(float alphaChange) {
321+
this.alphaChange = alphaChange;
322+
}
323+
324+
public void setSaturationChange(float saturationChange) {
325+
this.saturationChange = saturationChange;
326+
}
327+
286328
@Override
287329
public RGBA transform(RGBA originalRgba) {
288330
float[] hsba = originalRgba.getHSBA();
289-
hsba[1] *= 0.0f; // Lower saturation (0.0f = grayscale, 1.0f = full saturation)
290-
hsba[2] = Math.min(hsba[2] * brightnessChange, 1.0f); // Change brightness (cap at 1.0 for max brightness)
331+
332+
hsba[1] *= saturationChange;
333+
hsba[2] *= brightnessChange;
334+
hsba[3] *= alphaChange;
335+
336+
hsba[1] = Math.min(hsba[1], 1.f);
337+
hsba[2] = Math.min(hsba[2], 1.f);
338+
hsba[3] = Math.min(hsba[3], 255.f);
339+
291340
return new RGBA(hsba[0], hsba[1], hsba[2], hsba[3]);
292341
}
293342
}
294343

344+
/*
345+
* Class for Eclipse Mojo disabling (offline disabling for icons delivered with
346+
* eclipse)
347+
*/
348+
public static class EclipseRenderMojoDisablingTransformer implements PixelTransformer {
349+
private float brightnessChange;
350+
private float alphaChange;
351+
352+
public EclipseRenderMojoDisablingTransformer(float brightnessChange, float alphaChange) {
353+
this.brightnessChange = brightnessChange;
354+
this.alphaChange = alphaChange;
355+
}
356+
357+
public void setBrightnessChange(float brightnessChange) {
358+
this.brightnessChange = brightnessChange;
359+
}
360+
361+
public void setAlphaChange(float alphaChange) {
362+
this.alphaChange = alphaChange;
363+
}
364+
365+
@Override
366+
public RGBA transform(RGBA rgba) {
367+
float saturation = 0.3f;
368+
float contrast = 0.2f;
369+
// float brightness = 2.9f;
370+
371+
// Adjust saturation
372+
float[] hsba = rgba.getHSBA();
373+
hsba[1] *= saturation;
374+
rgba = new RGBA(hsba[0], hsba[1], hsba[2], hsba[3]);
375+
376+
// Adjust contrast and brightness
377+
rgba.rgb.blue = (int) Math.min(Math.max((contrast * (rgba.rgb.blue * brightnessChange - 128) + 128), 0), 255);
378+
rgba.rgb.red = (int) Math.min(Math.max((contrast * (rgba.rgb.red * brightnessChange - 128) + 128), 0), 255);
379+
rgba.rgb.green = (int) Math.min(Math.max((contrast * (rgba.rgb.green * brightnessChange - 128) + 128), 0), 255);
380+
rgba.alpha = (int) (alphaChange * rgba.alpha);
381+
382+
return rgba;
383+
}
384+
}
385+
295386
public static Image transformImage(Image originalImage, PixelTransformer transformer) {
296387
ImageData originalData = originalImage.getImageData();
297388
ImageData transformedData = new ImageData(originalData.width, originalData.height, 24,

0 commit comments

Comments
 (0)