Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Color selection components

May 30, 2011 12 Comments

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:

uisetlineprops
uisetlineprops

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

Swing’s JColorChooser

We start with Swing’s standard JColorChooser. This can easily be used in Matlab, just as any other Swing component. It can be added to our GUI using the built-in javacomponent function:

>> 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]

>> 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]

Swing's standard JColorChooser component
Swing's standard JColorChooser component

Note that 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);

>> cp = com.mathworks.beans.editors.ColorPicker; >> [jColorPicker,container] = javacomponent(cp,[1,1,400,200],gcf);

Matlab's internal ColorPicker component
Matlab's internal ColorPicker component

The 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);

options = 0; icon = 0; cp = com.mathworks.mlwidgets.graphics.ColorPicker(options,icon,''); [jColorPicker,hContainer] = javacomponent(cp,[10,220,30,20],gcf);

a different Matlab ColorPicker a different Matlab ColorPicker a different Matlab ColorPicker  
a different Matlab ColorPicker
(note several possible button icons & popup options)

JIDE’s ColorComboBox

JIDE’s com.jidesoft.combobox.ColorComboBox is very similar to ColorPicker. Despite its name and appearance as a combo-box, it actually extends the basic JComponent, not 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):

JIDE's ColorComboBox
JIDE's ColorComboBox

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.MJColorComboBox is a simple extension of the standard Swing JComboBox that 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):
    Matlab's MJColorComboBox
    Matlab's MJColorComboBox
  • com.mathworks.mlwidgets.graphics.ColorDialog is 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:
    Matlab's ColorDialog
    Matlab's ColorDialog
  • com.mathworks.hg.util.dColorChooser is another control, which presents a panel grid of selectable colors:
    Matlab's dColorChooser
    Matlab's dColorChooser

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:

Color selection table cell editor and renderer
Color selection table cell editor and renderer

A more detailed report

I have prepared a 20-page PDF report about using color-selection components in Matlab, which greatly expands on the above. This report is available for $19 here (please allow up to 48 hours for email delivery).

Related posts:

  1. Date selection components – The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...
  2. Font selection components – Several built-in components enable programmatic font selection in Matlab GUI - this article explains how. ...
  3. Plot-type selection components – Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...
  4. Javacomponent background color – This article explains how to align Java component background color with a Matlab color....
  5. Setting status-bar components – Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...
  6. Figure toolbar components – Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....
GUI Internal component Java JIDE
Print Print
« Previous
Next »
12 Responses
  1. Ed Ross June 3, 2011 at 11:46 Reply

    This info is awesome. The Beans editor ColorPicker is exactly what I was looking for but it’s sure nice to have all those other options as well. Thanks for responding so quickly to my request!

  2. dhruv jain June 16, 2011 at 07:34 Reply

    thanks…..will be looking to paste question if i am not able to make a child components in parent gui………..really good one bydave…….

  3. Jesse dziedzic October 20, 2011 at 08:47 Reply

    Wonderful post!!

  4. Red Kim May 13, 2012 at 23:29 Reply

    Hi.
    I’m always find information in your Blog.
    ans also read this page. but I don’t know how to get color info
    in “com.mathworks.mlwidgets.graphics.ColorPicker”.
    I’d like get color info same to get “uigetcolor”.
    How to get it?
    and I’d like set default color…
    It like System Test Tool box “Plot – General”.

    • Yair Altman May 13, 2012 at 23:46 Reply

      @Kim –

      import com.mathworks.mlwidgets.graphics.ColorPicker
      cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, '');
      [jcp,hcontainer] = javacomponent(cp,[],gcf);
      color = cp.getValue;  % a java.awt.Color object
      color = cp.getValue.getColorComponents([])' * 255;   % an [R,G,B] array of values

      import com.mathworks.mlwidgets.graphics.ColorPicker cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, ''); [jcp,hcontainer] = javacomponent(cp,[],gcf); color = cp.getValue; % a java.awt.Color object color = cp.getValue.getColorComponents([])' * 255; % an [R,G,B] array of values

      More information about ColorPicker, including its options and icons styles, can be found in section 5.4.1 of my Matlab-Java book.

    • Red Kim May 14, 2012 at 03:50 Reply

      Thank you so much!!
      It’s working now!
      Your amazing guy!!

  5. Ronin July 1, 2014 at 02:14 Reply

    Is it possible with some script or ui to edit the predefined colors that all the above mentioned color-selection tools seem to have in common? I personally dont ever use any other colors than given in the first 4 lines (white to black, red1… , red2…, and the one with green in it ). So the last 5 lines are just crap. Id really love to change them into more useful colors.

    Thanks in advance

    • Yair Altman July 2, 2014 at 09:51 Reply

      @ronin – I am not aware of a way to do this.

  6. Paul October 13, 2015 at 04:48 Reply

    Hi, I find your advice absolutely wonderful.
    However I can’t find a way to retrieve colour information from the com.mathworks.beans.editors.ColorPicker object (used as in example), even with your Red Kim answer.
    Would you have any idea why ?

    • Yair Altman October 13, 2015 at 12:58 Reply

      @Paul – you can use the following object methods to retrieve the com.mathworks.beans.editors.ColorPicker‘s selected color:

      * getBlue() % returns int
      * getGreen() % returns int
      * getRed() % returns int
      * getSelectedColor() % returns java.awt.Color

      Red Kim referred to a different ColorPicker class (that happens to have the same class name), but it’s a different class in a different package so of course it has different behavior.

  7. Meade February 22, 2017 at 18:14 Reply

    Yair,

    Another super useful post I’m just now finding!

    I have embedded this color picker within a context menu of a GUI I’ve built.
    My question is this:
    Is it possible to trigger a callback when the user clicks ‘OK’ in the widget?
    Using the code snippet you provided for @Red Kim, hopefully my intention is clear.

    Thanks so much for all the great insights.
    Best,
    meade

    import com.mathworks.mlwidgets.graphics.ColorPicker
    cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, '');
    [jcp,hcontainer] = javacomponent(cp,[],gcf);   % Need to trigger callback on selection here!
     
    % function MyCallback(hObj,evt)
    color = cp.getValue;  % a java.awt.Color object % This should be within the callback
    color = cp.getValue.getColorComponents([])' * 255;   % an [R,G,B] array of values
    % Do something with the color
    % end %MyCallback

    import com.mathworks.mlwidgets.graphics.ColorPicker cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, ''); [jcp,hcontainer] = javacomponent(cp,[],gcf); % Need to trigger callback on selection here! % function MyCallback(hObj,evt) color = cp.getValue; % a java.awt.Color object % This should be within the callback color = cp.getValue.getColorComponents([])' * 255; % an [R,G,B] array of values % Do something with the color % end %MyCallback

    • meade March 3, 2017 at 21:28 Reply

      Yair,

      I figured out my request. Posting it here for anyone else who may be curious.

      Thanks again!
      Meade

       import com.mathworks.mlwidgets.graphics.ColorPicker
       cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, '');
       [jcp,hcontainer] = javacomponent(cp,[],gcf);
       
       % !! Here's the key bit... This will trigger when user picks a color &amp; selects "OK" in widget
       set(jcp, 'ItemStateChangedCallback', {@MyCallback,gcf})
       
       
       function MyCallback(hObj,evt,hFigure)
            color = cp.getValue;  % a java.awt.Color object % This should be within the callback
            color = cp.getValue.getColorComponents([])' * 255;   % an [R,G,B] array of values
            % Do something with the color
       end %MyCallback

      import com.mathworks.mlwidgets.graphics.ColorPicker cp = ColorPicker(ColorPicker.AUTO, ColorPicker.LINE_ICON, ''); [jcp,hcontainer] = javacomponent(cp,[],gcf); % !! Here's the key bit... This will trigger when user picks a color &amp; selects "OK" in widget set(jcp, 'ItemStateChangedCallback', {@MyCallback,gcf}) function MyCallback(hObj,evt,hFigure) color = cp.getValue; % a java.awt.Color object % This should be within the callback color = cp.getValue.getColorComponents([])' * 255; % an [R,G,B] array of values % Do something with the color end %MyCallback

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Nicholas (6 days 13 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Nicholas (6 days 13 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Yair Altman (6 days 20 hours ago): Nicholas – yes, I used it in a compiled Windows app using R2022b (no update). You didn’t specify the Matlab code location that threw the error so I can’t help...
  • Nicholas (7 days 16 hours ago): Hi Yair, Have you attempted your displayWebPage utility (or the LightweightHelpPanel in general) within a compiled application? It appears to fail in apps derived from both R2022b...
  • João Neves (10 days 21 hours ago): I am on matlab 2021a, this still works: url = struct(struct(struct(struct(hF ig).Controller).PlatformHost). CEF).URL; but the html document is empty. Is there still a way to do...
  • Yair Altman (13 days 20 hours ago): Perhaps the class() function could assist you. Or maybe just wrap different access methods in a try-catch so that if one method fails you could access the data using another...
  • Jeroen Boschma (13 days 22 hours ago): Never mind, the new UI components have an HTML panel available. Works for me…
  • Alexandre (13 days 23 hours ago): Hi, Is there a way to test if data dictionnatry entry are signal, simulink parameters, variables … I need to access their value, but the access method depends on the data...
  • Nicholas (14 days 13 hours ago): In case anyone is looking for more info on the toolbar: I ran into some problems creating a toolbar with the lightweight panel. Previously, the Browser Panel had an addToolbar...
  • Jeroen Boschma (17 days 20 hours ago): I do not seem to get the scrollbars (horizontal…) working in Matlab 2020b. Snippets of init-code (all based on Yair’s snippets on this site) handles.text_explorer...
  • Yair Altman (45 days 23 hours ago): m_map is a mapping tool, not even created by MathWorks and not part of the basic Matlab system. I have no idea why you think that the customizations to the builtin bar function...
  • chengji chen (46 days 5 hours ago): Hi, I have tried the method, but it didn’t work. I plot figure by m_map toolbox, the xticklabel will add to the yticklabel at the left-down corner, so I want to move down...
  • Yair Altman (53 days 22 hours ago): @Alexander – this is correct. Matlab stopped including sqlite4java in R2021b (it was still included in 21a). You can download the open-source sqlite4java project from...
  • Alexander Eder (59 days 18 hours ago): Unfortunately Matlab stopped shipping sqlite4java starting with R2021(b?)
  • K (66 days 4 hours ago): Is there a way to programmatically manage which figure gets placed where? Let’s say I have 5 figures docked, and I split it into 2 x 1, I want to place 3 specific figures on the...
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top