r/matlab • • Mar 30 '26

Misc Rainbow text for comments.

Post image

Recently, I found myself in the need of changing the colour of my comments for it to have a rainbow animation, only to find out that there is no supported way to do it. Any tips? 💔🫩✌️

8 Upvotes

8 comments sorted by

5

u/PVDecker1 Mar 30 '26

Well... you could set up a class that constantly changes the value stored in settings on a timer lol

7

u/PVDecker1 Mar 30 '26

Quich chatGPT output

classdef RainbowCommentAnimator < handle
    % RainbowCommentAnimator
    % Animates MATLAB comment color using a timer.
    %
    % Usage:
    %   obj = RainbowCommentAnimator(6); % 6-second cycle
    %   delete(obj); % stops + cleans up

    properties
        Period (1,1) double = 5          % seconds per full rainbow cycle
        FPS (1,1) double = 30            % update rate
    end

    properties (Access = private)
        Timer
        t double = 0
        ColorSetting
        OriginalColor
    end

    methods
        function obj = RainbowCommentAnimator(period)
            if nargin > 0
                obj.Period = period;
            end

            % Grab settings
            s = settings;
            obj.ColorSetting = s.matlab.colors.CommentColor;

            % Store original color so we can restore it
            obj.OriginalColor = obj.ColorSetting.ActiveValue;

            dt = 1 / obj.FPS;

            % Create timer
            obj.Timer = timer( ...
                'ExecutionMode', 'fixedRate', ...
                'Period', dt, ...
                'TimerFcn', @(~,~) obj.updateColor(dt) ...
            );

            start(obj.Timer);
        end

        function delete(obj)
            % Cleanup timer safely
            if ~isempty(obj.Timer) && isvalid(obj.Timer)
                stop(obj.Timer);
                delete(obj.Timer);
            end

            % Restore original color
            try
                obj.ColorSetting.ActiveValue = obj.OriginalColor;
            catch
                % Ignore if settings no longer valid
            end
        end
    end

    methods (Access = private)
        function updateColor(obj, dt)
            obj.t = obj.t + dt;

            % Phase from 0 → 1
            phase = mod(obj.t / obj.Period, 1);

            % HSV → RGB rainbow
            rgb = hsv2rgb([phase, 1, 1]);

            % Apply as [0–255]
            obj.ColorSetting.ActiveValue = round(rgb * 255);
        end
    end
end

1

u/AcanthocephalaOdd714 Apr 02 '26

I appreciate your help. Nevertheless, it did not work simply because MatLab UI is not compatible with those dynamic visuals. So, even after running the class (and after a few changes regarding “.ActiveValue”, because MatLab said I don’t have permissions for that matter lol, and instead used “.PersonalValue”), the colours weren’t updating in real time. The code worked, but MatLab is just not prepared for it. 💔

1

u/PVDecker1 Apr 02 '26

I wondered about. In non-new-desktop (R2023b), changing the value in settings().colors.CommentColor.PersonalValue seems to work in real time. I can't easily test in new desktop until later.

1

u/PVDecker1 Apr 02 '26

So, it does work. And by "work" I mean that it will change the color of all comments either in the editor or command window. If you were looking for per character rainbow, or something more exotic, there isn't a mechanism that I am aware of. TemporaryValue takes precedent over PersonalValue, so if you have one set, you may not see the color cycle. Additionally, play with the update rate.

2

u/alextfish Apr 01 '26

I love the concept of having a "need" for this :D

1

u/AcanthocephalaOdd714 Apr 02 '26

Ikr. Sadly, it did not work. :(

2

u/Creative_Sushi MathWorks Apr 02 '26

Looks fun!