Class ColorPicker

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class ColorPicker extends Container

Picks a colour: a saturation and brightness square over a hue strip, with a swatch and the hex value of the result, and optionally a row of preset swatches.

The color picker with its saturation area, hue strip and presets

Form form = new Form("Accent color", BoxLayout.y());
Label preview = new Label("Preview");
preview.getAllStyles().setBgTransparency(255);
ColorPicker picker = new ColorPicker(0x3b82f6);
picker.setPresets(0xef4444, 0xf59e0b, 0x10b981, 0x3b82f6, 0x8b5cf6);
picker.addActionListener(e -> {
    preview.getAllStyles().setBgColor(picker.getColor());
    preview.repaint();
});
form.addAll(picker, preview);
form.show();

// or ask in a dialog; null when the user cancels
Integer chosen = ColorPicker.showDialog("Highlight", 0xffcc00);

Colours are 0xRRGGBB. Action listeners fire as the user drags and when a preset is tapped.

UIIDs: ColorPicker for the container and ColorPickerValue for the hex text, falling back to Label.

  • Constructor Details

    • ColorPicker

      public ColorPicker()
      A picker starting at the accent colour of the theme.
    • ColorPicker

      public ColorPicker(int rgb)

      A picker starting at rgb.

      Parameters
      • rgb: the initial colour, 0xRRGGBB
  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • showDialog

      public static Integer showDialog(String title, int initial)

      Shows a picker in a dialog with OK and Cancel.

      Parameters
      • title: the dialog's title

      • initial: the starting colour, 0xRRGGBB

      Returns

      the chosen colour, or null when the user cancelled

    • getColor

      public int getColor()

      The colour picked.

      Returns

      0xRRGGBB

    • setColor

      public final void setColor(int rgb)

      Sets the colour.

      Parameters
      • rgb: 0xRRGGBB
    • getHue

      public float getHue()

      The hue, 0 to 360.

      Returns

      the hue in degrees

    • setPresets

      public void setPresets(int... rgb)

      Sets the colours offered as a row of presets under the picker.

      Parameters
      • rgb: the preset colours, 0xRRGGBB; none hides the row
    • getPresets

      public int[] getPresets()

      The preset colours.

      Returns

      a copy of the presets

    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called whenever the user changes the colour.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • toHex

      public static String toHex(int rgb)

      The colour as "#RRGGBB".

      Parameters
      • rgb: 0xRRGGBB
      Returns

      the hex string, upper case

    • hsvToRgb

      public static int hsvToRgb(float h, float s, float v)

      Converts hue, saturation and brightness to a colour.

      Parameters
      • h: hue, 0 to 360

      • s: saturation, 0 to 1

      • v: brightness, 0 to 1

      Returns

      0xRRGGBB

    • rgbToHsv

      public static float[] rgbToHsv(int rgb)

      Converts a colour to hue, saturation and brightness.

      Parameters
      • rgb: 0xRRGGBB
      Returns

      {hue 0 to 360, saturation 0 to 1, brightness 0 to 1}