import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
interface ColorFunction {
public org.eclipse.swt.graphics.Color transformColors(org.eclipse.swt.graphics.Color color);
}
public class ColorFunctionDisplay extends Composite {
static HashMap<ColorFunction, String> functionsMap = new HashMap<>();
static List<ColorFunction> functionsList = new ArrayList<>();
static final int WIDTH_OF_LABEL_OF_SELECTED_COLOR_COMPONENTS = 70;
static final int SIDE_WIDTH_OF_COLOR_PREVIEW_SQUARES = 75;
static final int WIDTH_OF_LABEL_OF_HEX_COLOR_CODES_OF_THE_COLOR_PREVIEW_SQUARES = 65;
public static void main(String[] args) {
functionsMap.put((color) -> {
int r = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getRed() + 0.5 * color.getBlue())));
int g = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getGreen() + 0.5 * color.getBlue())));
int b = 0;
if ((g - r) > 0) {
r += (g - r) / 2;
}
return new Color(r, g, b);
}, "SSJ123");
functionsMap.put((color) -> {
int b = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getBlue() + 0.5 * color.getRed())));
int g = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getGreen() + 0.5 * color.getRed())));
int r = 0;
return new Color(r, g, b);
}, "SSJBlue");
functionsMap.put((color) -> {
int b = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getBlue() + 0.5 * color.getGreen())));
int r = 0xC0 + (int) (0.25 * (Math.min(0xFF, (int) color.getRed() + 0.5 * color.getGreen())));
int g = 0;
return new Color(r, g, b);
}, "SSJRose");
functionsList = functionsMap.keySet().stream().collect(Collectors.toList());
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(420, 500);
shell.setLayout(new RowLayout());
shell.setText("Composite Example");
ColorFunctionDisplay cfd = new ColorFunctionDisplay(shell, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
cfd.setLayout(gridLayout);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public ColorFunctionDisplay(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(3, false));
Label lblColorToTransform = new Label(this, SWT.NONE);
lblColorToTransform.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
lblColorToTransform.setText("Color to transform");
Label lblEnterR = new Label(this, SWT.NONE);
lblEnterR.setText("R");
Scale scaleR = new Scale(this, SWT.NONE);
scaleR.setMaximum(255);
Label lblValueR = new Label(this, SWT.NONE);
GridData gd_lblValueR = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblValueR.widthHint = WIDTH_OF_LABEL_OF_SELECTED_COLOR_COMPONENTS;
lblValueR.setLayoutData(gd_lblValueR);
lblValueR.setText("0");
Label lblEnterG = new Label(this, SWT.NONE);
lblEnterG.setText("G");
Scale scaleG = new Scale(this, SWT.NONE);
scaleG.setMaximum(255);
Label lblValueG = new Label(this, SWT.NONE);
GridData gd_lblValueG = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblValueG.widthHint = WIDTH_OF_LABEL_OF_SELECTED_COLOR_COMPONENTS;
lblValueG.setLayoutData(gd_lblValueG);
lblValueG.setText("0");
Label lblEnterB = new Label(this, SWT.NONE);
lblEnterB.setText("B");
Scale scaleB = new Scale(this, SWT.NONE);
scaleB.setMaximum(255);
Label lblValueB = new Label(this, SWT.NONE);
GridData gd_lblValueB = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblValueB.widthHint = WIDTH_OF_LABEL_OF_SELECTED_COLOR_COMPONENTS;
lblValueB.setLayoutData(gd_lblValueB);
lblValueB.setText("0");
Label lblOldColorHex = new Label(this, SWT.NONE);
lblOldColorHex.setText("#000000");
new Label(this, SWT.NONE);
Label lblNewColorHex = new Label(this, SWT.NONE);
GridData gd_lblNewColorHex = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblNewColorHex.widthHint = WIDTH_OF_LABEL_OF_HEX_COLOR_CODES_OF_THE_COLOR_PREVIEW_SQUARES;
lblNewColorHex.setLayoutData(gd_lblNewColorHex);
Button btnOldColorDisplay = new Button(this, SWT.NONE);
btnOldColorDisplay.setBackground(new Color(new RGB(0, 0, 0)));
btnOldColorDisplay.setText("");
btnOldColorDisplay.setEnabled(false);
GridData gd_btnOldColorDisplay = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_btnOldColorDisplay.widthHint = SIDE_WIDTH_OF_COLOR_PREVIEW_SQUARES;
gd_btnOldColorDisplay.heightHint = SIDE_WIDTH_OF_COLOR_PREVIEW_SQUARES;
btnOldColorDisplay.setLayoutData(gd_btnOldColorDisplay);
lblNewColorHex
.setText(colorToHex(functionsList.get(0).transformColors(btnOldColorDisplay.getBackground()), true));
Label lblNewColor = new Label(this, SWT.NONE);
GridData gd_lblNewColor = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblNewColor.widthHint = 207;
lblNewColor.setLayoutData(gd_lblNewColor);
lblNewColor.setText("New Color: ");
Button btnNewColorDisplay = new Button(this, SWT.NONE);
btnNewColorDisplay.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
btnNewColorDisplay.setText("");
GridData gd_btnNewColorDisplay = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_btnNewColorDisplay.widthHint = SIDE_WIDTH_OF_COLOR_PREVIEW_SQUARES;
gd_btnNewColorDisplay.heightHint = SIDE_WIDTH_OF_COLOR_PREVIEW_SQUARES;
btnNewColorDisplay.setLayoutData(gd_btnNewColorDisplay);
btnNewColorDisplay.setBackground(functionsList.get(0).transformColors(btnOldColorDisplay.getBackground()));
btnNewColorDisplay.setEnabled(false);
new Label(this, SWT.NONE);
Scale scaleFunctionNumber = new Scale(this, SWT.NONE);
new Label(this, SWT.NONE);
scaleFunctionNumber.setMaximum(functionsList.size() - 1);
scaleFunctionNumber.setMinimum(0);
scaleFunctionNumber.setIncrement(1);
Label lblFunctionNumber = new Label(this, SWT.NONE);
lblFunctionNumber.setText("0");
Label lblFunctionName = new Label(this, SWT.NONE);
GridData gd_lblFunctionName = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblFunctionName.widthHint = 207;
lblFunctionName.setLayoutData(gd_lblFunctionName);
lblFunctionName.setText(functionsMap.get(functionsList.get(0)));
new Label(this, SWT.NONE);
scaleR.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectedFunctionNumber = scaleFunctionNumber.getSelection();
int rValue = scaleR.getSelection();
lblValueR.setText(Integer.toString(rValue));
Color oldColor = btnOldColorDisplay.getBackground();
btnOldColorDisplay.setBackground(new Color(rValue, oldColor.getGreen(), oldColor.getBlue()));
Color newColor = btnOldColorDisplay.getBackground();
lblOldColorHex.setText(colorToHex(newColor, true));
Color outputForColorTransformation = functionsList.get(selectedFunctionNumber)
.transformColors(btnOldColorDisplay.getBackground());
btnNewColorDisplay.setBackground(outputForColorTransformation);
lblNewColorHex.setText(colorToHex(outputForColorTransformation, true));
}
});
scaleG.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectedFunctionNumber = scaleFunctionNumber.getSelection();
int gValue = scaleG.getSelection();
lblValueG.setText(Integer.toString(gValue));
Color oldColor = btnOldColorDisplay.getBackground();
btnOldColorDisplay.setBackground(new Color(oldColor.getRed(), gValue, oldColor.getBlue()));
Color newColor = btnOldColorDisplay.getBackground();
lblOldColorHex.setText(colorToHex(newColor, true));
Color outputForColorTransformation = functionsList.get(selectedFunctionNumber)
.transformColors(btnOldColorDisplay.getBackground());
btnNewColorDisplay.setBackground(outputForColorTransformation);
lblNewColorHex.setText(colorToHex(outputForColorTransformation, true));
}
});
scaleB.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectedFunctionNumber = scaleFunctionNumber.getSelection();
int bValue = scaleB.getSelection();
lblValueB.setText(Integer.toString(bValue));
Color oldColor = btnOldColorDisplay.getBackground();
btnOldColorDisplay.setBackground(new Color(oldColor.getRed(), oldColor.getGreen(), bValue));
Color newColor = btnOldColorDisplay.getBackground();
lblOldColorHex.setText(colorToHex(newColor, true));
Color outputForColorTransformation = functionsList.get(selectedFunctionNumber)
.transformColors(btnOldColorDisplay.getBackground());
btnNewColorDisplay.setBackground(outputForColorTransformation);
lblNewColorHex.setText(colorToHex(outputForColorTransformation, true));
}
});
scaleFunctionNumber.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectedFunctionNumber = scaleFunctionNumber.getSelection();
lblFunctionNumber.setText(Integer.toString(selectedFunctionNumber));
lblFunctionName.setText(functionsMap.get(functionsList.get(selectedFunctionNumber)));
btnNewColorDisplay.setBackground(functionsMap.keySet().stream().collect(Collectors.toList())
.get(selectedFunctionNumber).transformColors(btnOldColorDisplay.getBackground()));
lblNewColorHex.setText(colorToHex(btnNewColorDisplay.getBackground(), true));
}
});
}
public static String colorToHex(Color color, boolean addHash) {
String out = addHash ? "#" : "";
out += (color.getRed() < 16) ? "0" : "";
out += Integer.toString(color.getRed(), 16);
out += (color.getGreen() < 16) ? "0" : "";
out += Integer.toString(color.getGreen(), 16);
out += (color.getBlue() < 16) ? "0" : "";
out += Integer.toString(color.getBlue(), 16);
return out;
}
}