classdef Extra_basics < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                matlab.ui.Figure
        ColorSchemeButtonGroup  matlab.ui.container.ButtonGroup
        GrayButton              matlab.ui.control.ToggleButton
        RGBButton               matlab.ui.control.ToggleButton
        ResetButton             matlab.ui.control.Button
        LoadButton              matlab.ui.control.Button
        GammaSlider             matlab.ui.control.Slider
        GammaSliderLabel        matlab.ui.control.Label
        ContrastSlider          matlab.ui.control.Slider
        ContrastSliderLabel     matlab.ui.control.Label
        BrightnessSlider        matlab.ui.control.Slider
        BrightnessSliderLabel   matlab.ui.control.Label
        TransitionAxes          matlab.ui.control.UIAxes
        ImageAxes               matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        OriginalImage;
        WorkImage;
        ProcessedImage;
        XAxis;
        YAxis;
        Gamma;
        Contrast;
        Brightness;
    end
    
    methods (Access = private)
        
        function results = DrawImage(app)
            imshow(app.ProcessedImage,Parent=app.ImageAxes);
            plot(app.TransitionAxes ,app.XAxis,app.YAxis);
            app.TransitionAxes.YLim = [0,1];
        end
        
        function results = ApplyTransition(app)
            app.ProcessedImage = clip(((app.WorkImage.^(1/app.Gamma))-.5)*app.Contrast + .5 + app.Brightness,0,1);
            app.YAxis = clip(((app.XAxis.^(1/app.Gamma))-.5)*app.Contrast + .5 + app.Brightness,0,1);
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Value changing function: BrightnessSlider
        function BrightnessSliderValueChanging(app, event)
            changingValue = event.Value;
            app.Brightness = changingValue;
            app.ApplyTransition;
            app.DrawImage;
        end

        % Value changed function: BrightnessSlider
        function BrightnessSliderValueChanged(app, event)
            value = app.BrightnessSlider.Value;
            app.Brightness = value;
            app.ApplyTransition;
            app.DrawImage;
        end

        % Value changed function: ContrastSlider
        function ContrastSliderValueChanged(app, event)
            value = app.ContrastSlider.Value;
            app.Contrast = exp(value);
            app.ApplyTransition;
            app.DrawImage;
        end

        % Value changing function: ContrastSlider
        function ContrastSliderValueChanging(app, event)
            changingValue = event.Value;
            app.Contrast = exp(changingValue);
            app.ApplyTransition;
            app.DrawImage;
        end

        % Value changed function: GammaSlider
        function GammaSliderValueChanged(app, event)
            value = app.GammaSlider.Value;
            app.Gamma = exp(value);
            app.ApplyTransition;
            app.DrawImage;
        end

        % Value changing function: GammaSlider
        function GammaSliderValueChanging(app, event)
            changingValue = event.Value;
            app.Gamma = exp(changingValue);
            app.ApplyTransition;
            app.DrawImage;
        end

        % Button pushed function: ResetButton
        function ResetButtonPushed(app, event)
            app.Gamma = 1;
            app.Contrast =1;
            app.Brightness = 0;
            app.BrightnessSlider.Value = 0;
            app.ContrastSlider.Value = 0;
            app.GammaSlider.Value = 0;
            app.ApplyTransition;
            app.DrawImage;
        end

        % Button pushed function: LoadButton
        function LoadButtonPushed(app, event)
            try
            [file, path] = uigetimagefile;
            if ~isempty(file)
                app.OriginalImage = double(imread(file))/255;
                app.WorkImage = app.OriginalImage;
                app.ColorSchemeButtonGroup.Visible = "on";
                app.ResetButton.Visible = "on";
                app.GammaSlider.Visible = "on";
                app.ContrastSlider.Visible= "on";
                app.BrightnessSlider.Visible= "on";
                app.TransitionAxes.Visible = "on";
                app.ImageAxes.Visible="on";
                app.Brightness = 0;
                app.Gamma = 1;
                app.Contrast= 1;
                app.XAxis = 0:1/255:1;
                app.YAxis = app.XAxis;
                app.ApplyTransition;
                app.DrawImage;
            end
            catch e
                uialert(app.UIFigure, e.message, "Error while loading image",Interpreter="html");
            end
        end

        % Selection changed function: ColorSchemeButtonGroup
        function ColorSchemeButtonGroupSelectionChanged(app, event)
            selectedButton = app.ColorSchemeButtonGroup.SelectedObject;
            if selectedButton == app.GrayButton
                app.WorkImage = rgb2gray(app.OriginalImage);
            else
                app.WorkImage = app.OriginalImage;
            end
            app.ApplyTransition;
            app.DrawImage;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1469 727];
            app.UIFigure.Name = 'MATLAB App';

            % Create ImageAxes
            app.ImageAxes = uiaxes(app.UIFigure);
            app.ImageAxes.Toolbar.Visible = 'off';
            app.ImageAxes.XTick = [];
            app.ImageAxes.YTick = [];
            app.ImageAxes.Visible = 'off';
            app.ImageAxes.Position = [31 65 954 621];

            % Create TransitionAxes
            app.TransitionAxes = uiaxes(app.UIFigure);
            title(app.TransitionAxes, 'Transition graph')
            xlabel(app.TransitionAxes, 'original')
            ylabel(app.TransitionAxes, 'processed')
            zlabel(app.TransitionAxes, 'Z')
            app.TransitionAxes.PlotBoxAspectRatio = [1 1 1];
            app.TransitionAxes.Visible = 'off';
            app.TransitionAxes.Position = [990 121 451 329];

            % Create BrightnessSliderLabel
            app.BrightnessSliderLabel = uilabel(app.UIFigure);
            app.BrightnessSliderLabel.HorizontalAlignment = 'right';
            app.BrightnessSliderLabel.Visible = 'off';
            app.BrightnessSliderLabel.Position = [1033 562 62 22];
            app.BrightnessSliderLabel.Text = 'Brightness';

            % Create BrightnessSlider
            app.BrightnessSlider = uislider(app.UIFigure);
            app.BrightnessSlider.Limits = [-1 1];
            app.BrightnessSlider.MajorTicks = [-1 -0.6 -0.2 0.2 0.6 1];
            app.BrightnessSlider.MajorTickLabels = {''};
            app.BrightnessSlider.ValueChangedFcn = createCallbackFcn(app, @BrightnessSliderValueChanged, true);
            app.BrightnessSlider.ValueChangingFcn = createCallbackFcn(app, @BrightnessSliderValueChanging, true);
            app.BrightnessSlider.MinorTicks = [-1 -0.92 -0.84 -0.76 -0.68 -0.6 -0.52 -0.44 -0.36 -0.28 -0.2 -0.12 -0.04 0.04 0.12 0.2 0.28 0.36 0.44 0.52 0.6 0.68 0.76 0.84 0.92 1];
            app.BrightnessSlider.Visible = 'off';
            app.BrightnessSlider.Position = [1116 571 150 3];

            % Create ContrastSliderLabel
            app.ContrastSliderLabel = uilabel(app.UIFigure);
            app.ContrastSliderLabel.HorizontalAlignment = 'right';
            app.ContrastSliderLabel.Visible = 'off';
            app.ContrastSliderLabel.Position = [1050 515 50 22];
            app.ContrastSliderLabel.Text = 'Contrast';

            % Create ContrastSlider
            app.ContrastSlider = uislider(app.UIFigure);
            app.ContrastSlider.Limits = [-5 5];
            app.ContrastSlider.MajorTicks = [-5 -3 -1 1 3 5];
            app.ContrastSlider.MajorTickLabels = {''};
            app.ContrastSlider.ValueChangedFcn = createCallbackFcn(app, @ContrastSliderValueChanged, true);
            app.ContrastSlider.ValueChangingFcn = createCallbackFcn(app, @ContrastSliderValueChanging, true);
            app.ContrastSlider.MinorTicks = [-5 -4.6 -4.2 -3.8 -3.4 -3 -2.6 -2.2 -1.8 -1.4 -1 -0.6 -0.2 0.2 0.6 1 1.4 1.8 2.2 2.6 3 3.4 3.8 4.2 4.6 5];
            app.ContrastSlider.Visible = 'off';
            app.ContrastSlider.Position = [1116 525 150 3];

            % Create GammaSliderLabel
            app.GammaSliderLabel = uilabel(app.UIFigure);
            app.GammaSliderLabel.HorizontalAlignment = 'right';
            app.GammaSliderLabel.Visible = 'off';
            app.GammaSliderLabel.Position = [1052 473 48 22];
            app.GammaSliderLabel.Text = 'Gamma';

            % Create GammaSlider
            app.GammaSlider = uislider(app.UIFigure);
            app.GammaSlider.Limits = [-5 5];
            app.GammaSlider.MajorTicks = [-5 -3 -1 1 3 5];
            app.GammaSlider.MajorTickLabels = {''};
            app.GammaSlider.ValueChangedFcn = createCallbackFcn(app, @GammaSliderValueChanged, true);
            app.GammaSlider.ValueChangingFcn = createCallbackFcn(app, @GammaSliderValueChanging, true);
            app.GammaSlider.Visible = 'off';
            app.GammaSlider.Position = [1115 483 150 3];

            % Create LoadButton
            app.LoadButton = uibutton(app.UIFigure, 'push');
            app.LoadButton.ButtonPushedFcn = createCallbackFcn(app, @LoadButtonPushed, true);
            app.LoadButton.Position = [1051 663 100 23];
            app.LoadButton.Text = 'Load';

            % Create ResetButton
            app.ResetButton = uibutton(app.UIFigure, 'push');
            app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
            app.ResetButton.Visible = 'off';
            app.ResetButton.Position = [1151 662 100 23];
            app.ResetButton.Text = 'Reset';

            % Create ColorSchemeButtonGroup
            app.ColorSchemeButtonGroup = uibuttongroup(app.UIFigure);
            app.ColorSchemeButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ColorSchemeButtonGroupSelectionChanged, true);
            app.ColorSchemeButtonGroup.Title = 'Color Scheme';
            app.ColorSchemeButtonGroup.Visible = 'off';
            app.ColorSchemeButtonGroup.Position = [1053 583 122 78];

            % Create RGBButton
            app.RGBButton = uitogglebutton(app.ColorSchemeButtonGroup);
            app.RGBButton.Text = 'RGB';
            app.RGBButton.Position = [12 28 100 23];
            app.RGBButton.Value = true;

            % Create GrayButton
            app.GrayButton = uitogglebutton(app.ColorSchemeButtonGroup);
            app.GrayButton.Text = 'Gray';
            app.GrayButton.Position = [12 7 100 23];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Extra_basics

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end