Skip to content

Commit b54a3e3

Browse files
committed
1 parent 98b4e91 commit b54a3e3

File tree

5 files changed

+194
-15
lines changed

5 files changed

+194
-15
lines changed

ApplyPrfFile.matlab

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function ApplyPrfFile(prfFullpath)
2+
%% Input checking
3+
if ~nargin
4+
disp('No .prf file provided - aborting...'); return
5+
end
6+
%% Open and parse the prf file
7+
fid = fopen(prfFullpath,'rt');
8+
C = textscan(fid, '%s%s', 'Delimiter','=', 'CollectOutput',true);
9+
C = C{1};
10+
fclose(fid);
11+
%% Make sure that system defaults are off:
12+
com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem', false);
13+
%% Interate over all settings and apply everything:
14+
for ind1=1:size(C,1)
15+
prefname = C{ind1,1};
16+
preftype = C{ind1,2}(1);
17+
prefval = C{ind1,2}(2:end);
18+
switch preftype
19+
case 'B' % boolean
20+
val = strcmp(prefval,'true');
21+
com.mathworks.services.Prefs.setBooleanPref(prefname, val);
22+
case 'C' % RGB color
23+
val = int32(str2double(prefval));
24+
com.mathworks.services.Prefs.setRGBColorPref(prefname, val);
25+
com.mathworks.services.ColorPrefs.notifyColorListeners(prefname);
26+
case 'I' % int
27+
val = int32(str2double(prefval));
28+
com.mathworks.services.Prefs.setIntegerPref(prefname,val);
29+
case 'J' % double
30+
val = str2double(prefval);
31+
com.mathworks.services.Prefs.setDoublePref(prefname,val);
32+
case 'S' % string
33+
com.mathworks.services.Prefs.setStringPref(prefname,prefval);
34+
case 'F' % font
35+
if strcmp(prefname,'Desktop.Font.Text')
36+
com.mathworks.services.Prefs.setBooleanPref('GeneralTextUseSystemFont', false);
37+
end
38+
val = strsplit(prefval,' ');
39+
newFont = java.awt.Font(val{3}, str2double(val{1}), str2double(val{2}));
40+
com.mathworks.services.Prefs.setFontPref(prefname, newFont);
41+
disp(['[' 8 '- Notice:]' 8 ' you have changed a font preference (' prefname ...
42+
') - a restart of MATLAB is required to see changes.'])
43+
otherwise
44+
% Unhandled property - ignored.
45+
end
46+
end

Explanation.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
This is a proof-of-concept MATLAB color scheme changer, that works when MATLAB is open.
2-
To use put colorSetting and MatlabTheme (as .m files...) into your MATLAB path or active directory then run `MatlabTheme.ApplyTheme()`.
1+
This is a proof-of-concept MATLAB color scheme changer, that works when MATLAB is open.
2+
There are currently 2 options to use this code:
3+
### Option 1 ###
4+
Put colorSetting and MatlabTheme (as .m files...) into your MATLAB path or active directory then run `MatlabTheme.ApplyTheme()`.
5+
Currently new themes should be subclasses of MatlabTheme with different `cs` properties.
36

4-
Currently new themes should be subclasses of MatlabTheme with different `cs` properties, which are constructed using
5-
name/setting pairs as appear in colorSettingsEnum.m (the 3rd file), and correspond to the color setting found in the
7+
### Option 2 ###
8+
Put ApplyPrfFile.m(atlab) in your path/cd and supply it with a standard .prf file.
9+
10+
### Option 3 (coming soon) ###
11+
Run MatlabThemeChooser. Should be self-explanatory...
12+
13+
Notes:
14+
- The enumeration class, unfortunately, cannot be used as-is, and it is just provided for reference.
15+
- prefname/prefvalue pairs as appear in colorSettingsEnum.m (the 3rd file), and correspond to the color setting found in the
616
*Preferences > MATLAB > Colors* and *Preferences > MATLAB > Colors > Programming Tools* menus.
717

8-
The enumeration class, unfortunately, cannot be used as-is, and it is just provided for clarity.
18+
29-July-2015:
19+
- Added Amro's `.prf` parsing script with the TODO parts done :)
20+
- Added Amro's GUI v2 as `MatlabThemeChooser` with slight modifications to include the prefNames as in the uicontrols' `Tag` field.

MatlabTheme.matlab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ classdef MatlabTheme < handle
1010
end
1111
end
1212

13-
methods (Access = private, Static = true)
13+
methods (Access = protected, Static = true)
1414
function cs = makeCsVec()
1515
nCs = size(MatlabTheme.cs,1);
1616
cs = repmat(colorSetting(MatlabTheme.cs{1,1},MatlabTheme.cs{1,2}),[nCs,1]);

MatlabThemeChooser.matlab

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
function MatlabThemeChooser
2+
f = figure('Menubar','none', 'NumberTitle','off', ...
3+
'Name','MATLAB Theme Editor', 'Position',[810 352 275 578]);
4+
padding = {'Padding',5, 'Spacing',5};
5+
panelprops = {'BorderType','line', 'HighlightColor',[.8 .8 .8]};
6+
txtprops = {'Horizontal','left'};
7+
butprops = {'Callback','set(gcbo,''BackgroundColor'',uisetcolor(get(gcbo,''BackgroundColor'')))'};
8+
9+
v0 = uix.VBox('Parent',f, padding{:});
10+
11+
%%
12+
p1 = uipanel('Parent',v0, 'Title','Desktop tool colors', panelprops{:});
13+
h1 = uix.HBox('Parent',p1, padding{:});
14+
v1 = [uix.VBox('Parent',h1), uix.VBox('Parent',h1)];
15+
16+
uicontrol('Parent',v1(1), 'Style','text', txtprops{:}, 'String','Text');
17+
uicontrol('Parent',v1(1), 'Style','text', txtprops{:}, 'String','Background');
18+
uicontrol('Parent',v1(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','ColorsText')
19+
uicontrol('Parent',v1(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','ColorsBackground')
20+
21+
%%
22+
p2 = uipanel('Parent',v0, 'Title','MATLAB syntax highlighting colors', panelprops{:});
23+
h2 = uix.HBox('Parent',p2, padding{:});
24+
v2 = [uix.VBox('Parent',h2), uix.VBox('Parent',h2)];
25+
26+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','Keywords');
27+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','Comments');
28+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','Strings');
29+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','Unterminated Strings');
30+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','System Commands');
31+
uicontrol('Parent',v2(1), 'Style','text', txtprops{:}, 'String','Syntax Errors');
32+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_Keywords');
33+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_Comments');
34+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_Strings');
35+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_UnterminatedStrings');
36+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_SystemCommands');
37+
uicontrol('Parent',v2(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_Errors');
38+
39+
%%
40+
p3 = uipanel('Parent',v0, 'Title','MATLAB Command Window colors', panelprops{:});
41+
h3 = uix.HBox('Parent',p3, padding{:});
42+
v3 = [uix.VBox('Parent',h3), uix.VBox('Parent',h3)];
43+
44+
uicontrol('Parent',v3(1), 'Style','text', txtprops{:}, 'String','Error Text');
45+
uicontrol('Parent',v3(1), 'Style','text', txtprops{:}, 'String','Warning Text');
46+
uicontrol('Parent',v3(1), 'Style','text', txtprops{:}, 'String','Hyperlinks');
47+
uicontrol('Parent',v3(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Color_CmdWinErrors');
48+
uicontrol('Parent',v3(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Color_CmdWinWarnings');
49+
uicontrol('Parent',v3(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_HTML_HTMLLinks');
50+
51+
%%
52+
p4 = uipanel('Parent',v0, 'Title','Code analyzer colors', panelprops{:});
53+
h4 = uix.HBox('Parent',p4, padding{:});
54+
v4 = [uix.VBox('Parent',h4), uix.VBox('Parent',h4)];
55+
56+
uicontrol('Parent',v4(1), 'Style','text', txtprops{:}, 'String','Warnings');
57+
uicontrol('Parent',v4(1), 'Style','text', txtprops{:}, 'String','Autofix highlight');
58+
uicontrol('Parent',v4(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Colors_M_Warnings');
59+
uicontrol('Parent',v4(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','ColorsMLintAutoFixBackground');
60+
61+
%%
62+
p5 = uipanel('Parent',v0, 'Title','Variable and function colors', panelprops{:});
63+
h5 = uix.HBox('Parent',p5, padding{:});
64+
v5 = [uix.VBox('Parent',h5), uix.VBox('Parent',h5)];
65+
66+
uicontrol('Parent',v5(1), 'Style','text', txtprops{:}, 'String','Automatically highlight');
67+
uicontrol('Parent',v5(1), 'Style','text', txtprops{:}, 'String','Variables with shared scope');
68+
uicontrol('Parent',v5(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Editor.VariableHighlighting.Color');
69+
uicontrol('Parent',v5(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Editor.NonlocalVariableHighlighting.TextColor');
70+
71+
%%
72+
p6 = uipanel('Parent',v0, 'Title','Section display colors', panelprops{:});
73+
h6 = uix.HBox('Parent',p6, padding{:});
74+
v6 = [uix.VBox('Parent',h6), uix.VBox('Parent',h6)];
75+
76+
uicontrol('Parent',v6(1), 'Style','text', txtprops{:}, 'String','Highlight sections');
77+
uicontrol('Parent',v6(2), 'Style','pushbutton', butprops{:}, 'BackgroundColor','k','Tag','Editorhighlight-lines');
78+
%%
79+
p8 = uipanel('Parent',v0, 'Title','Section display colors', panelprops{:});
80+
h8 = uix.HBox('Parent',p8, padding{:});
81+
v8 = [uix.VBox('Parent',h8), uix.VBox('Parent',h8)];
82+
83+
uicontrol('Parent',v8(1), 'Style','text', txtprops{:}, 'String','Font');
84+
uicontrol('Parent',v8(2), 'Style','pushbutton', 'String','F');
85+
86+
%%
87+
h7 = uix.HButtonBox('Parent',v0);
88+
uicontrol('Parent',h7, 'Style','pushbutton', butprops{:}, 'String','Load...',...
89+
'Callback',@loadCallback);
90+
uicontrol('Parent',h7, 'Style','pushbutton', butprops{:}, 'String','Save...',...
91+
'Callback',@saveCallback);
92+
uicontrol('Parent',h7, 'Style','pushbutton', butprops{:}, 'String','Restore',...
93+
'Callback',@restoreCallback);
94+
uicontrol('Parent',h7, 'Style','pushbutton', butprops{:}, 'String','Apply',...
95+
'Callback',@applyCallback);
96+
97+
%%
98+
set([h1,h2,h3,h4,h5,h6,h8], 'Widths', [-1 30]);
99+
set(v0, 'Heights', ([2 6 3 2 2 1 1 0]+1.2)*20)
100+
end
101+
102+
% TODO: move or transfer the following callbacks to separate files:
103+
function loadCallback(source,callbackdata) %#ok
104+
disp('"Load" has been clicked!');
105+
end
106+
107+
function saveCallback(source,callbackdata) %#ok
108+
disp('"Save" has been clicked!');
109+
end
110+
111+
function restoreCallback(source,callbackdata) %#ok
112+
disp('"Restore" has been clicked!');
113+
end
114+
115+
function applyCallback(source,callbackdata) %#ok
116+
disp('"Apply" has been clicked!');
117+
end

colorSettingsEnum.matlab

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ classdef colorSettingsEnum
22
enumeration
33
TEXT_COLOR ('ColorsText'),
44
BACKGROUND_COLOR ('ColorsBackground'),
5-
USE_SYSTEM_COLORS ('ColorsUseSystem'),
6-
M_COLORS ('Colors_M_'),
7-
HTML_COLORS ('Colors_HTML_'),
8-
HYPERLINK_COLOR ('Colors_HTML_HTMLLinks'),
9-
SEARCH_RESULT_HIGHLIGHT ('ColorsSearchResult'),
10-
CMD_WIN_WARNINGS_COLOR ('Color_CmdWinWarnings'),
11-
CMD_WIN_ERRORS_COLOR ('Color_CmdWinErrors'),
5+
126
M_KEYWORDS ('Colors_M_Keywords'),
137
M_COMMENTS ('Colors_M_Comments'),
148
M_STRINGS ('Colors_M_Strings'),
159
M_UNTERMINATED_STRINGS ('Colors_M_UnterminatedStrings'),
1610
M_SYSTEM_COMMANDS ('Colors_M_SystemCommands'),
17-
M_WARNINGS ('Colors_M_Warnings'),
1811
M_ERRORS ('Colors_M_Errors'),
12+
13+
CMD_WIN_ERRORS_COLOR ('Color_CmdWinErrors'),
14+
CMD_WIN_WARNINGS_COLOR ('Color_CmdWinWarnings'),
15+
HYPERLINK_COLOR ('Colors_HTML_HTMLLinks'),
16+
17+
M_WARNINGS ('Colors_M_Warnings'),
1918
USE_MLINT_AUTOFIX_BACKGROUND ('ColorsUseMLintAutoFixBackground'),
2019
MLINT_AUTOFIX_BACKGROUND_COLOR ('ColorsMLintAutoFixBackground'),
2120
EDITOR_VAR_HLIGHT('Editor.VariableHighlighting.Color'),
21+
EDITOR_NONLOCAL_VAR_HLIGHT('Editor.NonlocalVariableHighlighting.TextColor'),
2222
EDITOR_CELL_HLIGHT('Editorhighlight-lines'),
23-
EDITOR_NONLOCAL_VAR_HLIGHT('Editor.NonlocalVariableHighlighting.TextColor')
23+
24+
SEARCH_RESULT_HIGHLIGHT ('ColorsSearchResult'),
25+
USE_SYSTEM_COLORS ('ColorsUseSystem'),
26+
M_COLORS ('Colors_M_'),
27+
HTML_COLORS ('Colors_HTML_')
2428
end
2529
end

0 commit comments

Comments
 (0)