I created a function where the inputs d
and t
and then it will make the figure where the background color of the text box changes based on the velocity (d/t). To convert the velocity to a color, I set up a color_matrix
variable whose first column is the velocity and the next 3 columns are the red, green, and blue values needed to specify the color. You can add more rows to have more colors involved, such as going from red to yellow to green.
function ShowSpeed(d, t)
figure(1)
clf
ah = uicontrol('style', 'text', 'units', 'normalized', 'Position', [0.5, 0.5, 0.3, 0.1], 'FontSize', 12);
velocity = d / t;
a = [num2str(velocity) 'km/h'];
% first column is the velocity, 2-4 columns are the red, green, blue values respectively
color_matrix = [50, 0, 0.5, 0; ... % dark breen for 50
60, 1, 1, .07]; % yellow for 60
background_color = interp1(color_matrix(:, 1), color_matrix(:, 2:4), velocity, 'linear', 'extrap');
% make sure color values are not lower than 0
background_color = max([background_color; 0, 0, 0]);
% make sure color values are not higher than 1
background_color = min([background_color; 1, 1, 1]);
set(ah,'String', a, 'BackgroundColor', background_color);
Here is the result with a velocity of 50:
ShowSpeed(500,10)
Here is the result with a velocity of 60
ShowSpeed(600,10)
You can also interpolate and extrapolate, although you can't go too far.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…