% staticColorMap.m This function plot a static colormap superimposed to a black/white
% background image

function [ Image ] = ambisonicsStaticColorMap( Image, imageWidth, imageHeight, contourSpan, lowerLimit, upperLimit, alpha, ...
                                               contourEnable, useFixedSplPlotBounds, fixedLowerLimit, fixedUpperLimit, ...
                                               valueTickEnable, cmap, P_pwd, aziRes, polarRes, tickSize, tickBold, ...
                                               correctionIndex, colorbarEnable )

    % background image is grey-scale with three channels so it will not be affected by the colormap later on
    imshow( Image, 'XData', [-imageWidth/2, imageWidth/2], 'YData', [-imageHeight/2, +imageHeight/2] ); 
    set( gcf, 'PaperUnits', 'points', 'PaperPosition', [ 0 0 imageWidth/2 imageHeight/2 ] )
    set( gca, 'YDir', 'normal' );
    xticks([-imageWidth/2 -imageWidth/(8/3) -imageWidth/4 -imageWidth/8 0 imageWidth/8 imageWidth/4 imageWidth/(8/3) imageWidth/2])
    xticklabels( { '-180', '-135', '-90', '-45', '0', '45', '90', '135', '180' } )
    yticks( [ -imageHeight/2 -imageHeight/4 0 imageHeight/4 imageHeight/2 ] )
    yticklabels( { '-90', '-45', '0', '45', '90' } )
    axis on
    hold on

    Fgrid           = Fdirs2grid( P_pwd, aziRes, polarRes, 1 );
    surfpoint       = 360 / polarRes + 1;
    if correctionIndex > 0  
        Fgrid       = Fgrid( :, [ correctionIndex+1 : surfpoint, 1:correctionIndex ] );
    elseif correctionIndex < 0
        Fgrid       = Fgrid( :, [ surfpoint+correctionIndex+1 : surfpoint, 1:surfpoint+correctionIndex ] );
    end
    
    elev            = -imageHeight/2 : polarRes/90*imageHeight/2 : imageHeight/2;
    azi             = -imageWidth/2 : aziRes/180*imageWidth/2 : imageWidth/2;
    aziMat          = repmat( azi, size( elev, 2 ), 1 );
    elevMat         = repmat( elev, size( azi, 2 ), 1 )';

    colormap( cmap )                                                            % define color set
    % warning: axis are flipped so XI has to change sign
    pcolor( azi, elev, Fgrid )                                                 % compute color map
    shading interp                                                              % better quality setting for colormap

    if useFixedSplPlotBounds == 1
        caxis( [ fixedLowerLimit fixedUpperLimit ] );                           % set absolute limits to Z-axis [dB]
        v           = fixedLowerLimit : contourSpan : fixedUpperLimit;          % iso-line levels
    else                         
        caxis( [ lowerLimit upperLimit ] )                                      % set relative limits to Z-axis [dB]
        v           = lowerLimit : contourSpan : upperLimit;                    % iso-line levels
    end

    hand_axes       = get( gcf, 'children' );

    if contourEnable && valueTickEnable
        [ C, ~ ]    = contour( aziMat, elevMat, Fgrid, v, 'k', 'LineWidth', 1 ); % compute iso-lines
        hand_surf   = get( hand_axes, 'children' );
        set( hand_surf(2), 'facealpha', alpha );                                % set transparency
        set( hand_surf(2), 'linestyle', 'none' );
        set( hand_surf(1), 'linecolor', 'black' );                              % set color lines to black
        if tickBold == 1
            clabel( C, 'FontSize', tickSize, 'FontWeight', 'bold' );
        else
            clabel( C, 'FontSize', tickSize );
        end
    elseif contourEnable
        contour( aziMat, elevMat, Fgrid, v, 'k' );                              % compute iso-lines
        hand_surf   = get( hand_axes, 'children' );
        set( hand_surf(2), 'facealpha', alpha );                                % set transparency
        set( hand_surf(2), 'linestyle', 'none' );
        set( hand_surf(1), 'linecolor', 'black' );                              % set color lines to black
    else
        hand_surf   = get( hand_axes, 'children' );
        set( hand_surf(1), 'facealpha', alpha );                                % set transparency
    end
    
    if colorbarEnable
        c           = colorbar;                                                 % show colormap legend on right side
        c.FontSize  = tickSize;
        if tickBold == 1
            c.FontWeight = 'bold';
        end
    end
    
    hold off
    
end
