Skip to content

ColorButton

greipadmin edited this page Feb 7, 2017 · 27 revisions

Introduction

Contructor

  • ColorButton(Composite parent)
  • parent the parent Composite

Methods

  • setRGB(RGB) set the initial color value.
  • RGB getRGB() gets the selected color or the initial color, when no color selected.
  • chooseRGB(IColorChooserFactory) for use inside a SelectionListener.
  • setColorChooserFactory(IColorChooserFactory) sets a factory for creating color choosers.
  • setResultConsumer(Consumer) sets a consumer for the selected color.
  • You can also use all SWT Button methods.

Color choosers

  • 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.

Example: Use a SelectionListener

...
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) {
		final RGB rgb = colorButton.chooseRGB(p -> new ColorChooserHSB(p, ColorResolution.Medium, true));
		// check if new color selected
		if (rgb != null) {
			[ enter your own code here ]
		}
	}
});
...

Example: ColorButton without Listeners

...
final ColorButton colorButton = new ColorButton(parent);
colorButton.setText("Click me!");

// initialize with current background rgb
colorButton.setRGB(shell.getBackground().getRGB());

colorButton.setColorChooserFactory(p -> new ColorWheelChooser(p, ColorResolution.Minimal, false));
colorButton.setColorConsumer(rgb -> {
	[ enter your own code here ]
});
...

Color picker

  • ColorPicker, selects a color from a predefined or user defined color list. When you call setRGB(RGB), the neares color in the list is selected.

Color picker example

...
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(p -> new ColorPicker(p, rgbs));
colorButton.setColorConsumer(rgb -> {
	[ enter your own code here ]
});
...
Clone this wiki locally