/
colorGray.m
executable file
·113 lines (103 loc) · 4.41 KB
/
colorGray.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
function RGBmap = colorGray(numLevels,debugplot)
% BRIEF: creates a rainbor color colormap which also looks good in greyscale
%
% SYNTAX:
% RGBmap = colorGray(numLevels)
%
%OUTPUTS
%
%INTPUTS
% numlevels - number of levels in the output colormap
% plot - boolean, if true outputs a plot showing the range of colors
% produced and lineplot of the range of grayscale produced verifying
% grayscale linearity.
%
%****************************************************
%Author: Alexandre R. Tumlinson, October 23, 2006
%****************************************************
%REVISIONS
%16, Nov. 2006 Peder Axensten - sent great suggestions and some nicely rewriten code.
% make the numLevels input optional. Also optimized by vectorizing
% code. Added the debug plotting options.
%****************************************************
if(nargin < 1), numLevels= size(colormap, 1); end % Same number of colors as the present color map.
if(nargin < 2), debugplot=0; end % turn off debug plotting by default
%first make a Jet map and then scale it so that it will look good in gray
lightest=0.05;
% Some constants to trim the greymap so lines arent too light.
% Also limit dark end because color sarted looking weird.
offsetbrightlimit= floor(numLevels*lightest);
offsetdarklimit= ceil(numLevels*0.25);
grayInt= gray(numLevels+offsetbrightlimit+offsetdarklimit);
grayInt= grayInt(offsetdarklimit+1:end-offsetbrightlimit,:);
grayInt= sum(grayInt,2)/3.4;
% Do something similar for the color map
offsetbluelimit= floor(numLevels*0.1);
offsetredlimit= ceil(numLevels*0.0);
jetmap= jet(numLevels+offsetbluelimit+offsetredlimit);
jetmap= jetmap(offsetbluelimit+1:end-offsetredlimit,:);
jetmapInt= jetmap*[0.2989; 0.5870; 0.1140]; %the gray level of the jetmap
scalefactor= grayInt./jetmapInt;
RGBmap= jetmap.*repmat(scalefactor, 1, 3);
%find elements greater than 1 and distribute to other colors
%attempts to maintain grey level on redistribution
%redistribution percentages are a litte arbitrary, but it seems to work
% Find elements greater than 1 and distribute to other colors,
% attempts to maintain grey level on redistribution.
% Redistribution percentages are a litte arbitrary, but it seems to work
for trials = 1:5
isdone= true;
bigs= (RGBmap(:,1) > 1);
if (any(bigs))
isdone= false;
surplus= RGBmap(bigs,1) - 1;
RGBmap(bigs,1)= 1;
RGBmap(bigs,2)= RGBmap(bigs,2) + surplus*0.85*(0.2989/0.5870); % Goes to salmon
RGBmap(bigs,3)= RGBmap(bigs,3) + surplus*0.15*(0.2989/0.1140); % Goes to salmon
% RGBmap(bigs,2)= RGBmap(bigs,2) + surplus*0.00*(0.2989/0.5870); % Goes to pink
% RGBmap(bigs,3)= RGBmap(bigs,3) + surplus*1.00*(0.2989/0.1140); % Goes to pink
end
bigs= (RGBmap(:,2) > 1);
if (any(bigs))
isdone= false;
surplus= RGBmap(bigs,2) - 1;
RGBmap(bigs,1)= RGBmap(bigs,1) + surplus*0.50*(0.5870/0.2989);
RGBmap(bigs,2)= 1;
RGBmap(bigs,3)= RGBmap(bigs,3) + surplus*0.50*(0.5870/0.1140);
end
bigs= (RGBmap(:,3) > 1);
if (any(bigs))
isdone= false;
surplus= RGBmap(bigs,3) - 1;
RGBmap(bigs,1)= RGBmap(bigs,1) + surplus*0.00*(0.1140/0.2989);
RGBmap(bigs,2)= RGBmap(bigs,2) + surplus*1.00*(0.1140/0.5870);
RGBmap(bigs,3)= 1;
end
if(isdone), break; end
end
%debugging plot option
if(debugplot)
testarray=ones(2,numLevels);
for count=1:numLevels
testarray(:,count)=testarray(:,count)*count;
end
figure
%create a line plot demonstrating the color range of the colormap
subplot(1,2,1)
set(gca,'ColorOrder',RGBmap)
set(gca,'NextPlot','replaceChildren') %because default operation plot automatically sets default color order
plot(testarray,'lineWidth',5 )
title('Colors in colormap');
ylabel('colormap index');
set(gca,'Ylim',[0.5 numLevels+0.5]);
set(gca,'Xtick',[]);
% Make a plot of grayscale linearity for created colormap
subplot(1,2,2)
greyval= RGBmap*[0.2989; 0.5870; 0.1140]; %the gray level of the output map
plot(greyval);
title('Range of grey used');
xlabel('colormap index');
ylabel('equivalent greylevel') %would be nice to put this on the right side of plot, any suggestions?
set(gca,'Ylim',[0 1]);
set(gca,'Xlim',[0.5 numLevels+0.5]);
end