Change Color of each individual String in a Listbox ?

Hey Guys,
I want to give each Word in my List box an specific color. My mentor said: its not possible. Now I ask here: is it possible to chose the Color of Listbox text with the RGB system ?

 Akzeptierte Antwort

Adam
Adam am 2 Sep. 2014
Bearbeitet: Adam am 2 Sep. 2014
Data(1).name = 'red';
Data(1).Color = [255 0 0];
Data(2).name = 'green';
Data(2).Color = [0 255 0];
Data(3).name = 'blue';
Data(3).Color = [0 0 255];
pre = '<HTML><FONT color="';
post = '</FONT></HTML>';
listboxStr = cell(numel( Data ),1);
for i = 1:numel( Data )
str = [pre rgb2Hex( Data(i).Color ) '">' Data(i).name post];
listboxStr{i} = str;
end
figure; hListBox = uicontrol('Style','list', 'Position', [20 20 100 100], 'String', listboxStr );
That should give you an example of what you want with the following function defined based on Guillame's input above :
function hexStr = rgb2Hex( rgbColour )
hexStr = reshape( dec2hex( rgbColour, 2 )',1, 6);
Obviously if you have a pre-existing listbox from Guide you can just do the usual:
set( handles.listbox1, 'String', listboxStr )
instead of creating a new listbox.

9 Kommentare

lets try it
i use matlab v2006a.....cause of my supervisor told me so... i dont have the rgb2hex.m command....I try to get it now
Adam
Adam am 2 Sep. 2014
Bearbeitet: Adam am 2 Sep. 2014
That is a function that I created which is underneath the other code in the post above. Just paste that function into a file (the .m file of your GUI will do if that is where you are calling the code from or its own file if you want).
You can just put the code directly into the for loop, I just prefer to factor out into a function because it looks a mess with all that code in the 'str = ...' line
Note that you don't need the num2str) in rgb2Hex, the output of dec2hex (and thus reshape) is already a string.
Adam
Adam am 2 Sep. 2014
Thanks. Yes, that's true, I didn't even look at the output before throwing that in! Corrected now.
Max Müller
Max Müller am 3 Sep. 2014
Bearbeitet: Max Müller am 3 Sep. 2014
Guys,
i ve made a stupid mistake. My RGB system works with the values 0 till 1. So blue is [0 0 1]..... but i guess i can convert.....1 = 100% of 255 right ?
Adam
Adam am 3 Sep. 2014
Just multiply by 255
Thanks, so much guys :D
The colours and names are embedded in the strings, so you could parse them:
liststrings = get(hlistbox, 'string');
colnames = regexp(liststrings, 'color="(.*)">(.*)</FONT', 'tokens', 'once');
data = cell2struct(reshape([colnames{:}], 2, []), {'color', 'name'});

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Adam
Adam am 1 Sep. 2014
Slightly modified example of code Yair Altman posted many years ago:
figure; uicontrol('Style','list', 'String', ...
{'<HTML><FONT color="red">Red</FONT></HTML>', ...
'<HTML><FONT color="green">Green</FONT></HTML>', ...
'<HTML><FONT color="blue">Blue</FONT></HTML>'} );

7 Kommentare

so it seems somehow complicated. ....lets try it
Adam
Adam am 1 Sep. 2014
It's just using the fact that underlying Matlab UI controls are Java controls and you can do things like use HTML markup in those.
You can also do things like create a multi-line Pushbutton string which I have often found useful, using HTML.
Guillaume
Guillaume am 1 Sep. 2014
Bearbeitet: Guillaume am 1 Sep. 2014
But since the fact that underlying Matlab UI controls are Java controls is an implementation detail of Matlab, it's also possible it may break in a future version.
Yair Altman
Yair Altman am 4 Sep. 2014
Bearbeitet: Yair Altman am 4 Sep. 2014
And related code/images (note that it's case-insensitive and that you don't have to close the HTML tags):
uicontrol('Style','list', 'Position',[10,10,70,70], 'String', ...
{'<HTML><FONT color="red">Hello</Font></html>', 'world', ...
'<html><font style="font-family:impact;color:green"><i>What a', ...
'<Html><FONT color="blue" face="Comic Sans MS">nice day!</font>'});
Trouble is, when there are "<" in the string, the "<" are all missing in the listbox!
Any ideas to fix it? Thanks!

Melden Sie sich an, um zu kommentieren.

Max Müller
Max Müller am 1 Sep. 2014

0 Stimmen

but i very new to programing..... can u pls explain me, how I cant set the ListboxInput and color each Word with a special Color (RGB)

4 Kommentare

Adam
Adam am 1 Sep. 2014
I'm not really familiar with HTML myself, I just learn on a "need to know" basis. I know you can use hex colour codes (I did it myself for the first time last week) rather than named colours, but I'm not sure about RGB colours...
Well, hex colour code is just red, green, blue (in that order) encoded as 2 bytes hexadecimal on a scale of 0 to 255.
e.g: red = 0, green = 126 (7E), blue = 255 (FF) => hex = 007EFF
Adam
Adam am 1 Sep. 2014
dec2hex( [r g b] )
should help convert between the two, although you have to scoop up the 3 rows of the result into a single string to put into the html string.
That would be:
reshape(dec2hex([r g b], 2)',1, 6)

Melden Sie sich an, um zu kommentieren.

Max Müller
Max Müller am 2 Sep. 2014

0 Stimmen

it sucks

1 Kommentar

Max, this is not an official "Answer" to your original question. If anything, it should have been a Comment to some other response.

Melden Sie sich an, um zu kommentieren.

Max Müller
Max Müller am 2 Sep. 2014

0 Stimmen

I just need an interactive way to show some text with a unique color and give this thing an on click callback.

2 Kommentare

Adam
Adam am 2 Sep. 2014
Please don't keep adding new questions as answers, it is confusing.
You can define the colour of independent static text objects easily enough (or indeed any independent objects like that if they are predefined).
If you want to have different coloured backgrounds or text in a listbox or uitable or similar though then I'm not aware of any alternative to using html and/or the underlying java UI programming as discussed in previous answers.
If you can explain which element of the suggested solution you are having a problem with we can assist further on that.
Imagine i have a structure called Data.
Data.name
Data.Color
are its Input. Now I want to write the names into a Listbox and give the String the Color form Data.Color.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 1 Sep. 2014

Kommentiert:

am 1 Nov. 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by