%% Dark Theme for Plots and Apps
% by Abby Skofield
% MATLAB Graphics and App Building Blog
% Do you use dark theme or dark mode in your OS or any applications? If you do, you have plenty of company. In the Spring of 2023, a UX researcher here at MathWorks, Nicole Pangborn, posed this question as part of a short web survey you may have seen pop up on MathWorks's documentation and Community sites.
%
% More than 75% of the thousands of respondents said yes, they use dark theme. We can use a donutchart to plot the responses:

figure 
dc = donutchart([2738, 854],["Yes","No"],LabelStyle='namedata',FaceAlpha=1); 

% use custom colors to represent dark mode vs. light mode 
bluePurple = "#655BC1"; 
paleYellow = "#FAFDBB"; 
customColors = hex2rgb([bluePurple,paleYellow]); 
dc.ColorOrder = customColors; 

title('Do you use Dark Theme or Dark Mode in your OS or any applications?') 

% Of course, Nicole was quick to warn us about self-selection bias. Dark mode lovers were likely more motivated to jump in and share their opinion on a survey about it. Luckily we were able to corroborate this data with qualitative feedback from our amazing Community, which has certainly been vocal about the topic! For many of you, dark mode is a personal, aesthetic preference, while others find dark mode easier on the eyes, or your laptop's battery life. We want to thank you all for your feedback and patience along the way - as my colleague Michelle announced in <<OTHER BLOG>>, the R2025a release includes dark mode for the MATLAB Desktop!
% Personally, I only started using dark mode a couple years ago, and it was because, well, I needed to understand where all you battery-optimizers were coming from! As the lead developer working to enable dark theme for MATLAB graphics and apps, I realized it was time to see how things were over on the dark side. It definitely took a little while to get used to reading and typing in dark mode... but let me tell you I'm not going back. I'm writing today to tell you about how you can have dark theme in your MATLAB desktop and your graphics too! Or not, as you prefer. (And you're right - the blog doesn't support dark theme yet - sorry!)
%
%% Applying a Theme
% Back in the spring of 2023, Nicole and I were preparing a presentation to share the results of the survey with our stakeholders. We wanted to share plots like the one above, but they looked terrible in our dark-themed PowerPoint template. We needed dark themed graphics!
%
% Good news, we were developing just the feature to solve this problem. The new theme command allows you to update the current figure to use 'light' or 'dark' theme. Now we have a plot that worked nicely in our dark-themed presentation!

theme dark

%
%% What is a theme?
% When I used the command theme dark above, I doubt you were surprised to see the background of the plot go dark or to see the text and borders switch to a light color. This is what we've all come to expect in dark mode. The MATLAB graphics system now has automatic colors for both light and dark theme. In this example, I never explicitly specified colors for the background, the title, the labels and the donutchart border - MATLAB chose them for me, and they changed between light and dark theme.
%
%% Custom colors
% Now what about the bluePurple and paleYellow colors I had chosen above - did those change? No, they remained the same. (Technically speaking, the color values in the DonutChart's ColorOrder property have not changed.) When the theme changes, the graphics system will not change colors that you have specified explicitly - so that whatever special meaning you're conveying will persist - like sunny yellow for light theme and dusky purple for dark theme.
% We've designed themes to change the automatic colors of graphics objects and UI components, but to leave untouched any custom colors that have been specified.
%% More ways to change the theme
% Although the theme command is probably the easiest way to change the theme of a Figure, the current theme is stored in the Figure's Theme property. The theme command as well as the "Theme" dropdown menu on the Figure Toolstrip both update the value of the Figure Theme property
%
% And yes, you can set the Figure property directly. Here, I'll create a new figure to plot some more data from the web survey, and then set the Theme property of the figure directly to use dark theme.

f = figure; 
dc = donutchart([1887, 494],["Match dark desktop","Remain light"],LabelStyle='namedata',FaceAlpha=1); 
dc.ColorOrder = customColors; 

title("How would you expect your graphics or apps"+newline+... 
    "to look when the MATLAB desktop uses a dark theme?") 

f.Theme = "dark"; 

% The value of the Figure's Theme property can be queried, as you see below. The BaseColorStyle property of the GraphicsTheme object can be used to determine if the current theme is 'light' or 'dark'.

f.Theme 

%
%% Automatic Themes
% Did you have a chance to see the data I plotted just there? One of the survey questions was specifically asking about whether users expected their graphics or apps to match the desktop when it was in dark mode. We weren't surprised to learn that folks expected their graphics to match the desktop, since we've read all the feedback about how "glaring" a single light themed application window feels when running in an otherwise dark mode display.
% Figures in MATLAB now automatically adopt the same theme as the desktop - so you'll have a nice, dark experience across the desktop, figures and apps.
%
% However, what about the hundreds of people represented by that yellow section of the data, the folks who expected graphics and apps to remain light when desktop uses dark theme?

dc.ExplodedWedges = 2;

%
%% Bring back the light!
% In addition to the web survey data, MathWorkers heard directly from dozens of customers working with the Beta version of the New Desktop and themed graphics explaining that even though they like to edit their code files in dark mode, they wanted their graphics to remain unchanged. Many of these customers explained that they needed "light themed"
% graphics because they looked better in journal papers and presentations, while others just found the change to their plots too jarring - we've all gotten pretty used to those grey figure windows and white axes :)
% For users in this group, one option is to manually set the theme to light - using the same techniques described above. This is a good idea of you are designing a plot or app that should always appear in light theme, even for a colleague of yours who is running in a dark mode version of MATLAB.

theme light

% But if you're just debugging an algorithm and calling plot here and there to see what is going on, you probably don't want to call the theme command every time a new figure pops up. In that case, you can configure the "Figure Theme" preference in the Settings dialog to specify that Figures should always be light, rather than matching the MATLAB Desktop theme.
%
% With this setting configured, when you run your plot or app code the figures will automatically use the light theme, as in this example below.
%
% The Figure's automatic theme behavior is controlled by the ThemeMode property. When you specify a theme, this property's value will be "manual". When no theme has been specified, its "auto". MATLAB will choose a theme automatically in the "auto" case according to the Figure Theme preference shown above.

f = gcf;
f.ThemeMode

%% Conclusion
% OK, there's my 1500 words or so summing up what many of my colleagues and I have been working on for the last couple years - I hope we make it look easy! In the next few weeks, you can look forward to some more articles digging into more specific topics about graphics themes, as well as other new features in the R2025a release. To whet your appetite, here is a little graphics themes cheat sheet with some reminders, tips and tricks for working with plots and apps in light and dark theme. <LINK OR EMBED? TBD>
%