forked from MarcelBeining/masterplotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorme.m
139 lines (136 loc) · 4.9 KB
/
colorme.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function [col,colgroupdef] = colorme(varargin)
% returns colors for plotting
% input can be number of colors needed or specific color declaration in a
% cell
% -gr : graded + basic color (r g b or black)
if iscell(varargin{1})
ugroupdef = varargin{1};
if numel(varargin) > 1
options = varargin{2};
else
options = '';
end
elseif isvector(varargin{1}) && numel(varargin{1}) == 3 && ~ischar(varargin{1})
if numel(varargin) < 2
varargin{2} = 'brighter';
end
switch varargin{2}
case 'brighter'
fac = 0.5;
case 'darker'
fac = 2;
end
col = [1 1 1]-([1 1 1] - varargin{1})*fac;
col(col<0) = 0;
col(col>1) = 1;
return
else
ugroupdef = varargin;
if ischar(ugroupdef{end}) && ~isempty(strfind(ugroupdef{end},'-'))
options = ugroupdef{end};
ugroupdef = ugroupdef(1:end-1);
else
options = '';
end
end
if ~isempty(strfind(options,'-2'))
if numel(varargin) > 2
numsubgroup = varargin{3};
else
numsubgroup = 2;
end
end
if ischar(ugroupdef)
ugroupdef = {ugroupdef};
end
if ~isempty(strfind(options,'-k'))
stdcolor = {[1 1 1],[1 0 0],[0 0 0.9],[0 0.6 0],[1 0.7 0],[0.5 0 1],[0 1 1],[0.5 0.5 0.5],[1 0 1],[0.2 0.6 1],[0.6 0 0],[0 1 0],[0 0.2 0],[0 0.5 0.5],[0.4 0 0],[1 1 0],[0.5 1 0.7],[1 0.5 0.5],[0.9 0.4 0],[0 1 1],[0 0 0.6],[0 0 0],[0.05 0.05 0.05]};
stdcoln = {'white','red','blue','dim green','yellow','violett','turquois','gray','pink','light blue','dim red','green','dark green','dark turqoius','dark red','bright yellow','bright green','bright red','orange','cyan','dim blue','black','bblack'};
else
stdcolor = {[0 0 0],[1 0 0],[0 0 0.9],[0 0.6 0],[1 0.7 0],[0.5 0 1],[0 1 1],[0.5 0.5 0.5],[1 0 1],[0.2 0.6 1],[0.6 0 0],[0 1 0],[0 0.2 0],[0 0.5 0.5],[0.4 0 0],[1 1 0],[0.5 1 0.7],[1 0.5 0.5],[0.9 0.4 0],[0 1 1],[0 0 0.6],[1 1 1],[0.05 0.05 0.05]};
stdcoln = {'black','red','blue','dim green','yellow','violett','turquois','gray','pink','light blue','dim red','green','dark green','dark turqoius','dark red','bright yellow','bright green','bright red','orange','cyan','dim blue','white','bblack'};
end
if numel(ugroupdef) == 1 && isnumeric(ugroupdef{1})
if ~isempty(strfind(options,'-gr'))
col = zeros(ugroupdef{1},3);
vec = 1:3;
if isempty(strfind(options,'-w'))
grader = (0.3:1.4/(ugroupdef{1}-1):1.7)';
switch options(strfind(options,'-gr')+3)
case 'r'
ind = 1;
case 'g'
ind = 2;
case 'b'
ind = 3;
otherwise
ind = [1 2 3];
grader = (0:0.8/(ugroupdef{1}-1):0.8)';
end
col(:,ind) = repmat(grader,1,numel(ind));
col(col(:,ind) > 1,setdiff(vec,ind)) = repmat(col(col(:,ind) > 1,ind) -1,1,2);
col(col(:,ind) > 1,ind) = 1;
else
grader = (0.3:1.4/(ugroupdef{1}-1):1.7)';
switch options(strfind(options,'-gr')+3)
case 'r'
ind = 1;
case 'g'
ind = 2;
case 'b'
ind = 3;
otherwise
ind = [];
end
grader = flipud((0:0.8/(ugroupdef{1}-1):0.8)');
col(:,ind) = 1;
col(:,setdiff(vec,ind)) = repmat(grader,1,numel(setdiff(vec,ind)));
end
tcol = cell(size(col,1),1);
for c = 1:size(col,1)
tcol{c} = col(c,:);
end
col = tcol;
else
reps = repmat(1:min(numel(stdcolor)-2,ugroupdef{1}),1,ceil(ugroupdef{1}/(numel(stdcolor)-2)));
reps = reps(1:min(numel(reps),ugroupdef{1}));
col = stdcolor(reps);
colgroupdef = stdcoln(reps);
end
elseif ~isempty(ugroupdef)
col = cell(numel(ugroupdef),1);
for c = 1:numel(ugroupdef)
ind = strcmpi(ugroupdef{c},stdcoln); % take only first appearance of colordef...
if sum(ind) == 0
fprintf('Warning, color %s not found, color vec is black at that entry\n',ugroupdef{c});
col{c} = [0 0 0];
else
col(c) = stdcolor(ind);
end
end
colgroupdef = ugroupdef;
else
col = [];
colgroupdef = [];
end
% prevent two colors being exactly the same (for applyhatch)
[ucol,~,ib] = unique(cell2mat(col),'rows');
if size(ucol,1)~= numel(col)
for u = 1:size(ucol,1)
if sum(ib == u) > 1
ind = find(ib == u,1,'last');
col{ind} = col{ind} - [0 0 sign((col{ind}(3)==1)-0.5)*0.000];
ib(ind) = NaN;
end
end
end
if ~isempty(strfind(options,'-2'))
newcol = cell(numel(col)*numsubgroup,1);
for c = 1:numel(col)
for n = 1:numsubgroup
newcol{numsubgroup*(c-1)+n} = col{c} * n/numsubgroup;
% newcol{numsubgroup*c} = col{c} * 0.5;
end
end
col = newcol;
end