matlab.ui.componentcontainer.ComponentContainer.getTheme - Get figure theme of component container subclass - MATLAB (original) (raw)

Main Content

Class: matlab.ui.componentcontainer.ComponentContainer
Namespace: matlab.ui.componentcontainer

Get figure theme of component container subclass

Since R2025a

Syntax

Description

[gt](#mw%5F955452e2-4ce6-4bd6-87c8-34dff5a373c3) = getTheme([c](#mw%5F5acfa862-864d-4d2f-ad8f-8e9f69db42ae)) returns theGraphicsTheme object for the parent figure of the specifiedComponentContainer object. To determine the theme of the figure, query the BaseColorStyle property of gt.

The update method of the component container executes when the figure theme changes, so you can call this method from within the update method of your subclass to respond to changes in the figure theme.

example

Input Arguments

expand all

Component container, specified as an object of a class that inherits from thematlab.ui.componentcontainer.ComponentContainer base class.

Output Arguments

expand all

Graphics theme information, returns as a GraphicsTheme object that has the properties in this table.

Property Description
Name Short description of the theme, returned as 'Light Theme' or 'Dark Theme'.
BaseColorStyle Basic color style of the theme, returned as'light' or 'dark'. This property indicates the overall lightness or darkness of the theme. Query this property when deciding on colors for your component.

Examples

expand all

Create a custom UI component by defining a class namedSpinnerValidator that updates the background color of a spinner if its value is invalid. Program the component to update the color based on the theme of the figure that the component is in.

To define the class, create a file named SpinnerValidator.m that contains the following class definition with these features:

classdef SpinnerValidator < matlab.ui.componentcontainer.ComponentContainer % SpinnerValidator A spinner that shows whether the value is valid.

properties (Access=private,Transient,NonCopyable)
    Spinner matlab.ui.control.Spinner
    GridLayout matlab.ui.container.GridLayout
end

methods (Access=protected)
    function setup(obj)
        % Set the initial position of this component
        obj.Position = [100 100 100 42];
        % Layout
        obj.GridLayout = uigridlayout(obj,[1,1]);
        % UI components
        obj.Spinner = uispinner(obj.GridLayout, ...
            ValueChangedFcn=@(src,event)updateColor(obj,event.Value));
    end
    
    function update(obj)
        updateColor(obj,obj.Spinner.Value)
    end
end
   
methods (Access=private)
    function updateColor(obj,val)
        lightValidColor = "#90EE90"; % light green
        lightInvalidColor = "#FFADA0"; % light red
        gt = getTheme(obj);
        spinnerColor = lightValidColor;
        if val >= 10
            spinnerColor = lightInvalidColor;
        end
        if strcmp(gt.BaseColorStyle,"dark")
            spinnerColor = fliplightness(spinnerColor);
        end
        obj.Spinner.BackgroundColor = spinnerColor;
    end
end

end

Create a SpinnerValidator component in a light-themed figure.

fig = uifigure; sp = SpinnerValidator(fig);

Spinner in a light-themed figure with a value of 11 and a light red background

Change the figure to the dark theme. The component colors update in response.

Spinner in a dark-themed figure with a value of 11 and a dark red background

Version History

Introduced in R2025a