-
Notifications
You must be signed in to change notification settings - Fork 4
ColorButton
greipadmin edited this page Jan 4, 2019
·
27 revisions

The ColorButton widget allow the user to select a color. You can use different color choosers for the color models RGB and HSB, a color wheel and a color list.
-
ColorButton(Composite parent)-
parentthe parent Composite
-
-
void setRGB(RGB)set the initial color value. -
RGB getRGB()gets the selected color or the initial color, when no color selected. -
boolean chooseRGB()opens the color chooser and allows the user to select a color (for use inside a selection listener). -
void setColorChooserFactory(IColorChooserFactory)sets a factory for creating the color chooser. -
void setResultConsumer(Consumer<RGB>)sets a consumer for the selected color. -
void setDropDownArrowVisible(boolean)switch visibility of drop down arrow. -
boolean isDropDownArrowVisible()returns the visibility of drop down arrow. - You can also use all SWT Button methods.
-
ColorChooserHSB, selects a color by hue, brightness and saturation. -
ColorChooserRGB, selects a color by red, green, blue. -
ColorWheelChooser, selects a color from a color wheel.
A color chooser has the following features:
- Define a color resolution (reduces the maximum number of displayed colors).
- Show/hide RGB info.
- Preview with old and new color.
...
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Click me!");
// initialize with current background rgb
colorButton.setRGB(shell.getBackground().getRGB());
colorButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
if (colorButton.chooseRGB()) {
RGB rgb = colorButton.getRGB();
[ enter your own code here ]
}
}
});
......
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Click me!");
// initialize with current background rgb
colorButton.setRGB(shell.getBackground().getRGB());
// define the color chooser factory
colorButton.setColorChooserFactory(new ColorWheelChooser.Factory(ColorResolution.Minimal, false, false));
// the color consumer is called when a color was selected by user
colorButton.setColorConsumer(rgb -> {
[ enter your own code here ]
});
...-
ColorPicker, selects a color from a predefined or user defined color list. When you callsetRGB(RGB), the nearest color in the list is selected.
...
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Background");
// define the color list
final RGB[] rgbs = new RGB[] {
display.getSystemColor(SWT.COLOR_WHITE).getRGB(),
display.getSystemColor(SWT.COLOR_YELLOW).getRGB(),
display.getSystemColor(SWT.COLOR_GREEN).getRGB(),
display.getSystemColor(SWT.COLOR_RED).getRGB(),
display.getSystemColor(SWT.COLOR_CYAN).getRGB(),
display.getSystemColor(SWT.COLOR_MAGENTA).getRGB(),
display.getSystemColor(SWT.COLOR_BLUE).getRGB(),
display.getSystemColor(SWT.COLOR_GRAY).getRGB(),
display.getSystemColor(SWT.COLOR_BLACK).getRGB()};
colorButton.setColorChooserFactory(new ColorPicker.Factory(rgbs));
colorButton.setColorConsumer(rgb -> {
[ enter your own code here ]
});
...