% Create a color for visualizing voltage value inside a finite
% difference grid.
%
% Arguments:
% u        value of the voltage
% vmin     minimum of the voltage scale
% vmax     maximum of the voltage scale
%
% Returns:
% color     RGB color as a three-vector with entries in range [0,1]
%
% Samuli Siltanen February 2025

function color = colorscale_volt(u,vmin,vmax)

% Check validity of the request
if (u<vmin)|(u>vmax)
    error('Argument voltage u must be between vmin and vmax')
end
if vmax<vmin
    error('Maximal voltage vmax must be greater than vmin')
end

% Pick colormap and record its size
colormap_volt = colormap('cool');
N = size(colormap_volt,1);

% Determine where the desired voltage lies in the colormap scale.
% Here t belongs to the interval [0,1]
t = (u-vmin)/(vmax-vmin);
    
% Calculate appropriate color
R = interp1([0:N-1],colormap_volt(:,1).',t*(N-1));
G = interp1([0:N-1],colormap_volt(:,2).',t*(N-1));
B = interp1([0:N-1],colormap_volt(:,3).',t*(N-1));
color = [R,G,B];


