Main Content

getTheme

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

Get figure theme of component container subclass

Since R2025a

Description

gt = getTheme(c) returns the GraphicsTheme object for the parent figure of the specified ComponentContainer 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 the matlab.ui.componentcontainer.ComponentContainer base class.

Output Arguments

expand all

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

PropertyDescription

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.

Attributes

Protectedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Create a custom UI component by defining a class named SpinnerValidator 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:

  • Spinner and GridLayout private properties that place the spinner in the component container.

  • A setup method that creates the UI components.

  • An update method that calls a function to update the background color of the component when a property changes. MATLAB® automatically calls the update when the theme of the figure that contains the component changes.

  • An updateColor method that sets the background color of the spinner based on its value and the theme of the figure that contains the component. A valid spinner value is less than 10.

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.

fig.Theme = "dark";

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

Version History

Introduced in R2025a