A couple of weeks ago, a reader of this website requested an article about color-selection components. So Ed – this one’s for you
Matlab includes a fully-documented uisetcolor function to enable color selection. uisetcolor uses a modal dialog window for this. To integrate this color-selection dialog in our GUI, simply add a uicontrol button or a menu item that call uisetcolor in their callback function.
An example of such an integrated control can be found in the uisetlineprops utility on the File Exchange, which I introduced earlier this month in my article on using a borderless button for setting plot properties:
Unfortunately, we often need to integrate color-selection components as a sub-component of an existing GUI, rather than as a stand-alone modal dialog window. This is not supported by uisetcolor.
Luckily, Matlab contains several internal Java-based color-selection components that can be integrated in our GUI. Being Java-based means that they work as advertised on any platform that runs Matlab. Matlab uses these components for different situations. For example,
com.mathworks.beans.editors.ColorPicker is used by the property inspector as the editor for colors properties of Java objects, whereas Matlab color properties use another editor –
com.mathworks.mlwidgets.graphics.ColorDialog (see below).
>> cc = javax.swing.JColorChooser; >> [jColorChooser,container] = javacomponent(cc,[1,1,450,325],gcf); >> jColorChooser.getColor ans = java.awt.Color[r=102,g=153,b=255]
JColorChooser should have a minimum size of about 425×325 pixels to appear uncropped.
Matlab ColorPicker components
com.mathworks.beans.editors.ColorPicker is an alternative component provided by Matlab:
>> cp = com.mathworks.beans.editors.ColorPicker; >> [jColorPicker,container] = javacomponent(cp,[1,1,400,200],gcf);
com.mathworks.beans.editors.ColorPicker object should not be confused with another internal
ColorPicker object, namely
com.mathworks.mlwidgets.graphics.ColorPicker. The Beans editor
ColorPicker is a simple stand-alone Java component that can be embedded GUI figures, whereas the MLWidgets
ColorPicker is a Java button control that is used to present a popup-selection similar to a
ColorDialog (see below):
options = 0; icon = 0; cp = com.mathworks.mlwidgets.graphics.ColorPicker(options,icon,''); [jColorPicker,hContainer] = javacomponent(cp,[10,220,30,20],gcf);
com.jidesoft.combobox.ColorComboBox is very similar to
ColorPicker. Despite its name and appearance as a combo-box, it actually extends the basic
JComboBox. It includes three separately-customizable sub-components: a color label, the color values, and the drop-down arrow button. All are shown by default (the color values may be hidden if the control is set too narrow):
ColorComboBox has a very nice feature, enabling manual modification of the color values (RGB) – the label’s color automatically changes once a new value has been entered (the <enter> key is pressed).
…and a few others
There are a few other color-selection controls. I won’t go into details here, but here are a couple of pointers to get you started:
com.mathworks.mwswing.MJColorComboBoxis a simple extension of the standard Swing
JComboBoxthat presents a color-selection drop-down control. Unfortunately, this control only works on Matlab releases up to 7.10 (R2010a) – it was removed in 7.11 (R2010b):
com.mathworks.mlwidgets.graphics.ColorDialogis the control used by Matlab for color selection in Matlab’s property inspector for Matlab handle objects. It can be displayed as either a modal dialog window or an embedded panel component:
com.mathworks.hg.util.dColorChooseris another control, which presents a panel grid of selectable colors:
Some final notes
It is fortunate to have such a wide selection of available components, although it is a pity that they are not fully documented and supported. Of course, there are many other 3rd-party color-selection components available, and these can also be integrated in your GUI – today I have only briefly discussed a few relevant built-in components.
Color-selection controls can also be embedded as sub-components in other GUI controls. For example, color-selection cells within data tables:
A more detailed report
I have prepared a 20-page report about using color-selection components in Matlab, which greatly expands on the above. This report is available for a small fee here (please allow up to 48 hours for email delivery).