-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNegativeEnhancingColormap.m
95 lines (83 loc) · 3.01 KB
/
NegativeEnhancingColormap.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function MAP = NegativeEnhancingColormap(N, SCALE, ...
NEGATIVE_COLOR, POSITIVE_COLOR, ZERO_COLOR, POWER)
% MAP = NegativeEnhancingColormap(N, SCALE, NEG_COLOR, POS_COLOR, ZERO[, POWER=1])
%
% Calculates a colormap that goes from pure red to black for positive
% values (black being zero) and from black to pure blue for negative
% values.
%
% INPUT VARIABLES
% ---------------
% N: Number of samples of the map.
% SCALE: Two-element vector with [MINV MAXV], being MINV the minimum value
% of the color scale and MAXV the maximum. Note that MINV must be
% negative and MAXV positive.
% NEG_COLOR:
% (Optional) Color to use for negative values represented as a three
% RGB component vector. Each component goes from 0 to 1. Default
% value is [0 0 1] which corresponds to blue color.%
% POS_COLOR:
% (Optional) Color to use for positive values represented as a three
% RGB component vector. Each component goes from 0 to 1. Default
% value is [1 0 0] which corresponds to red color.
% ZERO: (Optional) Color to use for zero value represented as a three RGB
% component vector. Each component goes from 0 to 1. Default value
% is [1 1 1] which corresponds to white color.
% POWER: (Optional) Power of the interpolation curve for the color. By
% default is 1 which corresponds to linear interpolation.
%
% OUTPUT VARIABLES
% ----------------
% MAP: Generated colormap.
%
% Copyright (c) 2014 GICO-UCM
% default negative color
if(nargin < 3 || isempty(NEGATIVE_COLOR))
NEGATIVE_COLOR = [0 0 1];
end
% default positive color
if(nargin < 4 || isempty(POSITIVE_COLOR))
POSITIVE_COLOR = [1 0 0];
end
% default zero color
if(nargin < 5 || isempty(ZERO_COLOR))
ZERO_COLOR = [1 1 1];
end
% default power
if(nargin < 6)
POWER = 1;
end
if(~isvector(SCALE) || numel(SCALE) ~= 2)
error('SCALE must be a two-element vector.');
end
MINV = SCALE(1);
MAXV = SCALE(2);
if(MINV >= 0 || MAXV <= 0)
error('MINV must be negative and MAXV positive. None can be zero.');
end
% initialize map
MAP = zeros(N, 3);
% calculate zero position
ZERO_N = round(N*abs(MINV)/(MAXV-MINV));
% calculate number of colors for positive or negative
NUM_POSITIVE = N - ZERO_N;
NUM_NEGATIVE = ZERO_N - 1;
% place red, blue, and black
MAP(1, :) = NEGATIVE_COLOR;
MAP(end, :) = POSITIVE_COLOR;
MAP(ZERO_N, :) = ZERO_COLOR;
for(K=2:N-1)
if(K < ZERO_N)
INTERPV = 1 - K/(NUM_NEGATIVE+1);
INTERPV = INTERPV^POWER;
MAP(K, :) = (1-INTERPV)*ZERO_COLOR + INTERPV*NEGATIVE_COLOR;
elseif(K > ZERO_N)
INTERPV = (K-ZERO_N)/(NUM_POSITIVE+1);
INTERPV = INTERPV^POWER;
MAP(K, :) = (1-INTERPV)*ZERO_COLOR + INTERPV*POSITIVE_COLOR;
end
end
% make it unique
[~, INDEX] = unique(MAP, 'rows', 'first');
MAP = MAP(sort(INDEX), :);
end