calc_colours

PURPOSE ^

[colours,scl_data]= calc_colours(img, set_value, do_colourbar)

SYNOPSIS ^

function [colours,scl_data]= calc_colours(img, set_value, do_colourbar)

DESCRIPTION ^

 [colours,scl_data]= calc_colours(img, set_value, do_colourbar)
 Calculate a colour for each image element 

 Conductive (positive) areas are shown in red
 Non-Conductive (negative) areas are shown in blue

 PARAMETERS: img
     - 1) an EIDORS image object, or a Ex1 vector
     - 2) a 2D image matrix

 Usage #1 (img is a Ex1 vector of element conductivities)

   Cs = calc_colours( img);
   patch(Xs,Ys,Zs,Cs);

 Cs is Ex1x1 colourmap entries (if mapped_colour>0)
       Ex1x3 colourmap entries (if mapped_colour==0)

 Usage #2 (rimg is a MxN image matrix of reconstructed pixels):
           img is an image structure with the image properties
  
   c_img = calc_colours( rimg, img);
   image( c_img );

 c_img is MxN colourmap entries (if mapped_colour>0)
          MxNx3 colourmap entries (if mapped_colour==0)

 Usage #3 (img is string parameter value)
  
   value = calc_colours( 'param' );
   calc_colours( 'param', value );
    eg. calc_colours('mapped_colour',127)
         Use this to allow printing vector eps files to get around
         a matlab bug with warning 'RGB color data not ...'

   calc_colours( 'component', 'real' ); (DEFAULT real)
     the other value is 'imag' to show the imaginary component

   The following parameters are accepted

   'greylev'    (DEFAULT -.01): the colour of the ref_level.
      Negative values indicate white background
      For almost white, greylev=-.01; Black=> greylev=.01
   'sat_adj'    (DEFAULT .9): max G,B when R=1
   'window_range' (DEFAULT .9); window colour range
      Colour slope outside range is 1/3 of centre slope
   'backgnd' ( DEFAULT [.5,.5,.15] ): image border colour 
   'ref_level' (DEFAULT 'auto') conductivity of centre of
      colour mapping. 'auto' tries to estimate a good level.
      For complex image data, ref_level should also be complex.
   'mapped_colour' (DEFAULT 127) number of colourmap entries
      using mapped_colour allows matlab to print vector graphics to eps
      setting mapped_colour=0 means to use RGB colours
   'npoints' (DEFAULT 64) number of points accross the image
   'transparency_thresh' fraction of maximum value at which colours
             are rendered transparent for 
   'clim'    (DEFAULT []) crop colour display of values above clim
           colour limit. values more different from ref_level are cropped.
           if not specified or clim==[] => no limit
   'cmap_type'  Specify special colours (Default 'blue_red')
           'blue_red':          default Blue/Red eidors colourmpa
           'jet':               matlab jet colourmap
           'jetair':            scaled jet colours
           'blue_yellow':       Blue/Yellow colours
           'greyscale':         Greyscale colours (Lungs white)
           'greyscale-inverse': Greyscale colours (Lungs black)
           'copper':            Copper colours
           'blue_white_red':    Blue/White/Red colours
           'black_red':         Black/Red Colours
           'blue_black_red':    Blue/Black/Red colours
           'polar_colours':     "Polar" blue/white/red colours
           'draeger', 'draeger-difference':
                                Draegerwerk colourmap (difference)
           'draeger-2009':      Draegerwerk colourmap (2009)
           'draeger-tidal':     Draegerwerk colourmap (tidal images)
           'swisstom'           Swisstom colourmap
           'timpel'             Timpel colourmap
           matrix [Nx3]         Use values ([0-1]) as REB colourmap
   'cb_shrink_move' shrink or move the colorbar. See eidors_colourbar
           help for details.
   'image_field', 'image_field_idx', 'image_field_val' 
           image elements which match _idx are set to _val.
   'defaults' set to default colours in list above

   'colourmap' Return the current EIDORS colormap. 
           Use as colormap(calc_colours('colourmap'))

 PARAMETERS CAN BE SPECIFIED IN TWO WAYS
   1. as an image parameter (ie clim in img.calc_colours.clim)
   2. a second parameter to ( calc_colours(data, param2 )
          where param2.calc_colours.clim= ... etc
   3. parameter to calc_colours('clim')

 Parameters specified as (1) will override (2)

 PARAMETERS: do_colourbar
    - show a Matlab colorbar with appropriate scaling

  usage: c_img= calc_colours( img, clim );
         image( c_img );
         calc_colours( img, clim, 1); %now do colorbar 

 PARAMETERS: ref_lev
     - if specified, override the global ref_level parameter

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [colours,scl_data]= calc_colours(img, set_value, do_colourbar)
0002 % [colours,scl_data]= calc_colours(img, set_value, do_colourbar)
0003 % Calculate a colour for each image element
0004 %
0005 % Conductive (positive) areas are shown in red
0006 % Non-Conductive (negative) areas are shown in blue
0007 %
0008 % PARAMETERS: img
0009 %     - 1) an EIDORS image object, or a Ex1 vector
0010 %     - 2) a 2D image matrix
0011 %
0012 % Usage #1 (img is a Ex1 vector of element conductivities)
0013 %
0014 %   Cs = calc_colours( img);
0015 %   patch(Xs,Ys,Zs,Cs);
0016 %
0017 % Cs is Ex1x1 colourmap entries (if mapped_colour>0)
0018 %       Ex1x3 colourmap entries (if mapped_colour==0)
0019 %
0020 % Usage #2 (rimg is a MxN image matrix of reconstructed pixels):
0021 %           img is an image structure with the image properties
0022 %
0023 %   c_img = calc_colours( rimg, img);
0024 %   image( c_img );
0025 %
0026 % c_img is MxN colourmap entries (if mapped_colour>0)
0027 %          MxNx3 colourmap entries (if mapped_colour==0)
0028 %
0029 % Usage #3 (img is string parameter value)
0030 %
0031 %   value = calc_colours( 'param' );
0032 %   calc_colours( 'param', value );
0033 %    eg. calc_colours('mapped_colour',127)
0034 %         Use this to allow printing vector eps files to get around
0035 %         a matlab bug with warning 'RGB color data not ...'
0036 %
0037 %   calc_colours( 'component', 'real' ); (DEFAULT real)
0038 %     the other value is 'imag' to show the imaginary component
0039 %
0040 %   The following parameters are accepted
0041 %
0042 %   'greylev'    (DEFAULT -.01): the colour of the ref_level.
0043 %      Negative values indicate white background
0044 %      For almost white, greylev=-.01; Black=> greylev=.01
0045 %   'sat_adj'    (DEFAULT .9): max G,B when R=1
0046 %   'window_range' (DEFAULT .9); window colour range
0047 %      Colour slope outside range is 1/3 of centre slope
0048 %   'backgnd' ( DEFAULT [.5,.5,.15] ): image border colour
0049 %   'ref_level' (DEFAULT 'auto') conductivity of centre of
0050 %      colour mapping. 'auto' tries to estimate a good level.
0051 %      For complex image data, ref_level should also be complex.
0052 %   'mapped_colour' (DEFAULT 127) number of colourmap entries
0053 %      using mapped_colour allows matlab to print vector graphics to eps
0054 %      setting mapped_colour=0 means to use RGB colours
0055 %   'npoints' (DEFAULT 64) number of points accross the image
0056 %   'transparency_thresh' fraction of maximum value at which colours
0057 %             are rendered transparent for
0058 %   'clim'    (DEFAULT []) crop colour display of values above clim
0059 %           colour limit. values more different from ref_level are cropped.
0060 %           if not specified or clim==[] => no limit
0061 %   'cmap_type'  Specify special colours (Default 'blue_red')
0062 %           'blue_red':          default Blue/Red eidors colourmpa
0063 %           'jet':               matlab jet colourmap
0064 %           'jetair':            scaled jet colours
0065 %           'blue_yellow':       Blue/Yellow colours
0066 %           'greyscale':         Greyscale colours (Lungs white)
0067 %           'greyscale-inverse': Greyscale colours (Lungs black)
0068 %           'copper':            Copper colours
0069 %           'blue_white_red':    Blue/White/Red colours
0070 %           'black_red':         Black/Red Colours
0071 %           'blue_black_red':    Blue/Black/Red colours
0072 %           'polar_colours':     "Polar" blue/white/red colours
0073 %           'draeger', 'draeger-difference':
0074 %                                Draegerwerk colourmap (difference)
0075 %           'draeger-2009':      Draegerwerk colourmap (2009)
0076 %           'draeger-tidal':     Draegerwerk colourmap (tidal images)
0077 %           'swisstom'           Swisstom colourmap
0078 %           'timpel'             Timpel colourmap
0079 %           matrix [Nx3]         Use values ([0-1]) as REB colourmap
0080 %   'cb_shrink_move' shrink or move the colorbar. See eidors_colourbar
0081 %           help for details.
0082 %   'image_field', 'image_field_idx', 'image_field_val'
0083 %           image elements which match _idx are set to _val.
0084 %   'defaults' set to default colours in list above
0085 %
0086 %   'colourmap' Return the current EIDORS colormap.
0087 %           Use as colormap(calc_colours('colourmap'))
0088 %
0089 % PARAMETERS CAN BE SPECIFIED IN TWO WAYS
0090 %   1. as an image parameter (ie clim in img.calc_colours.clim)
0091 %   2. a second parameter to ( calc_colours(data, param2 )
0092 %          where param2.calc_colours.clim= ... etc
0093 %   3. parameter to calc_colours('clim')
0094 %
0095 % Parameters specified as (1) will override (2)
0096 %
0097 % PARAMETERS: do_colourbar
0098 %    - show a Matlab colorbar with appropriate scaling
0099 %
0100 %  usage: c_img= calc_colours( img, clim );
0101 %         image( c_img );
0102 %         calc_colours( img, clim, 1); %now do colorbar
0103 %
0104 % PARAMETERS: ref_lev
0105 %     - if specified, override the global ref_level parameter
0106 %
0107 
0108 % (C) 2005-2008 Andy Adler. License: GPL version 2 or version 3
0109 % $Id: calc_colours.m 5585 2017-06-21 13:04:53Z aadler $
0110 
0111 % BUGS TO FIX:
0112 %  mapped_colour should set the colourmap length for all
0113 
0114 if nargin==0; error('calc_colours: expecting argument'); end
0115 if ischar(img) && strcmp(img,'UNIT_TEST'); do_unit_test; return; end
0116 
0117 if ischar(img)
0118     % called as calc_colours('parameter' ... )
0119     if nargin==1;
0120        if strcmp(img,'defaults') % set defaults and return
0121           set_colours_defaults; return
0122        end
0123        colours= get_field(img);
0124     else
0125        colours= set_field(img, set_value);
0126     end
0127     return;
0128 
0129 elseif isfield(img,'type')
0130    img_data= get_img_data( img );
0131    pp=get_colours(img(1)) ;
0132 else
0133    img_data= img;
0134 
0135    if nargin==1
0136       pp=get_colours( [] ); 
0137    else
0138       pp=get_colours(set_value); 
0139    end
0140 end
0141 
0142 % Set default parameters
0143 if nargin < 3; do_colourbar = 0;       end
0144 ref_lev = 'use_global';
0145 
0146 
0147 if isempty(img_data)
0148     colours = 'k'; %black
0149     return;
0150 end
0151 
0152 m= size(img_data,1); n=size(img_data,2);
0153 
0154 % We can only plot the real part of data
0155 % Vectorize img_data here, it gets reshaped later
0156 [scl_data, ref_lev, max_scale] = scale_for_display( img_data(:), pp);
0157 
0158 backgnd= isnan(scl_data);
0159 scl_data(backgnd)= mean( scl_data(~backgnd));
0160 
0161 if pp.mapped_colour
0162    colours=set_mapped_colour(pp, backgnd, scl_data);
0163    colours= reshape( colours, m,n,[]);
0164 else
0165    [red,grn,blu] = blu_red_axis( pp, scl_data, backgnd );
0166    colours= reshape( [red,grn,blu],m,n,3);
0167 end
0168 
0169 % print colorbar if do_colourbar is specified
0170 if do_colourbar
0171    if ~pp.mapped_colour
0172        warning('Colorbar not available without mapped_colour option');
0173    elseif do_colourbar < 0
0174         eidors_colourbar(-do_colourbar,max_scale,pp.cb_shrink_move,1)
0175    else
0176        eidors_colourbar(max_scale,ref_lev,pp.cb_shrink_move)
0177    end
0178 end
0179 
0180 
0181 function set_colours_defaults;
0182    clear -global eidors_colours;
0183 
0184    calc_colours('greylev',-.001);          % background colour = white
0185    calc_colours('sat_adj',.9);             % saturation of red and blue
0186    calc_colours('window_range', .7);       % windowing of colours
0187    calc_colours('backgnd',[.35,.50,.50]);    % background colour
0188    calc_colours('mapped_colour',127);      % use 127*2+1 colourmap entries
0189    calc_colours('ref_level','auto');       % auto set background colour
0190    calc_colours('npoints',64);             % 64 raster points
0191    calc_colours('clim',[]);                % no colour cropping
0192    calc_colours('cb_shrink_move',[1,1,0]); % Don't shrink or move colorbar
0193    calc_colours('transparency_thresh',0.25); % transparent at .25 of max
0194    eidors_msg('Setting Default Colours',2);
0195    calc_colours('cmap_type','blue_red');   % default eidors colours
0196 
0197 
0198 %scaled data must go from -1 to 1
0199 function [red,grn,blu] = blu_red_axis( pp, scale_data, backgnd )
0200    if ~isfield(pp,'cmap_type')
0201       pp.cmap_type = 'blue_red';
0202    end; 
0203 
0204    % window data such that slope above w is 1/3 of that below
0205    % thus w is mapped to k st k/w = 3(1-k)/(1-w) -> k=3w/(1+2w)
0206    W= pp.window_range; K= 3*W/(1+2*W);
0207    scale_data= sign(scale_data) .* ( ...
0208      (  K/W*   abs(scale_data)   ) .* (abs(scale_data)<=W) + ...
0209      (K+K/W/3*(abs(scale_data)-W)) .* (abs(scale_data)> W) );
0210 
0211    if isnumeric(pp.cmap_type)
0212       user_colourmap = pp.cmap_type;
0213       pp.cmap_type= 'user_colourmap';
0214    end
0215 
0216    switch pp.cmap_type
0217      case 'user_colourmap'
0218       red = user_colourmap(:,1);
0219       grn = user_colourmap(:,2);
0220       blu = user_colourmap(:,3);
0221      case 'blue_red'
0222       [red,grn,blu]= blue_red_colours(pp,scale_data);
0223      case {'draeger', 'draeger-difference'}
0224       [red,grn,blu]= draeger_colours(pp,scale_data, 'difference2014');
0225      case 'draeger-2009',
0226       [red,grn,blu]= draeger_colours(pp,scale_data, '2009');
0227      case 'draeger-tidal',
0228       [red,grn,blu]= draeger_colours(pp,scale_data, 'tidal2014');
0229      case 'swisstom'
0230       [red,grn,blu]= swisstom_colours(pp,scale_data);
0231      case 'timpel'
0232       [red,grn,blu]= timpel_colours(pp,scale_data);
0233      case 'jet'
0234       [red,grn,blu]= jet_colours(pp,scale_data);
0235      case 'jetair'
0236       % Make most of the range for air -ve
0237       scd = (scale_data + 0.5)/0.6;
0238       [red,grn,blu]= jet_colours(pp,scd);
0239      case 'blue_yellow'
0240          [red,grn,blu] = blue_yellow_colours(pp,scale_data);
0241      case 'greyscale'          % Lungs are white
0242          [red,grn,blu] = greyscale_colours(pp,scale_data);
0243      case 'greyscale-inverse'  % Lungs are black
0244          [red,grn,blu] = greyscale_colours(pp,-scale_data);
0245      case 'copper'
0246          [red,grn,blu] = copper_colours(pp,scale_data);
0247      case 'blue_white_red'
0248          [red,grn,blu]= blue_white_red_colours(pp,scale_data);
0249      case 'black_red'
0250          [red,grn,blu] = black_red_colours(pp,scale_data);
0251      case 'blue_black_red'
0252          [red,grn,blu] = blue_black_red_colours(pp,scale_data);
0253      case 'polar_colours'
0254          [red,grn,blu] = polar_blue_white_red_colours(pp,scale_data);
0255 
0256      otherwise
0257       error(['specified cmap_type not understood:',pp.cmap_type]);
0258    end
0259 % Sometimes this is just slightly > 1
0260    red(red>1) = 1;
0261    grn(grn>1) = 1;
0262    blu(blu>1) = 1;
0263 
0264 
0265    red(backgnd) = pp.backgnd(1);
0266    grn(backgnd) = pp.backgnd(2);
0267    blu(backgnd) = pp.backgnd(3);
0268 
0269 %FIXME - come up with a general way to handle blocks in image field
0270 if isfield(pp,'image_field')
0271     red(2)=pp.image_field(1);
0272     grn(2)=pp.image_field(2);
0273     blu(2)=pp.image_field(3);
0274 end
0275 
0276    
0277 function [red,grn,blu]= blue_red_colours(pp,scale_data);
0278 if 0
0279    D= sign(pp.greylev+eps); %force 0 to 1
0280    glev= abs(pp.greylev);
0281    F= 3*pp.sat_adj;
0282 
0283    red= D*F*abs(scale_data+D/F) - D + (D==-1);
0284    red= red.*(red>0).*(red<1) + (red>=1);
0285    red= red*(1-glev) + glev;
0286 end
0287    ofs= (pp.greylev >= 0);   % 1 if greylev>=0
0288    glev= abs(pp.greylev);
0289 
0290    D= (2*ofs - 1);
0291    ofs= ofs - 2*(ofs==0);
0292    F= 3*pp.sat_adj;
0293    DF= D*F; D_F= D/F;
0294 
0295    red= DF*abs(scale_data+D_F) - ofs;
0296    red= red.*(red>0).*(red<1) + (red>=1);
0297 
0298    grn= DF*abs(scale_data    ) - ofs;
0299    grn= grn.*(grn>0).*(grn<1) + (grn>=1);
0300 
0301    blu= DF*abs(scale_data-D_F) - ofs;
0302    blu= blu.*(blu>0).*(blu<1) + (blu>=1);
0303 
0304    if pp.greylev >=0 % Black background
0305       red= red*(1-glev) + glev;
0306       grn= grn*(1-glev) + glev;
0307       blu= blu*(1-glev) + glev;
0308    else
0309       red= red*(1-glev);
0310       grn= grn*(1-glev);
0311       blu= blu*(1-glev);
0312    end
0313 
0314 function [red,grn,blu]= swisstom_colours(pp,scale_data);
0315    backgnd =  hex2dec(['59';'59';'59']);
0316    alphapts = [-255,32,160,255]/255;
0317    alphaval = [0, 0, 1, 1];
0318    alp = interp1(alphapts, alphaval, -scale_data, 'linear');
0319 
0320    hexcolours = ['3E4A50';
0321                  '2F4D58';
0322                  '357892';
0323                  '2AB1DF';
0324                  '61D1FB';
0325                  'CFF0FE'];
0326    redpts = hex2dec(hexcolours(:,1:2));
0327    grnpts = hex2dec(hexcolours(:,3:4));
0328    blupts = hex2dec(hexcolours(:,5:6));
0329    interppts = linspace(0,1, size(hexcolours,1));
0330 % add for +ve changes
0331    redpts = redpts([1,1:end]);
0332    grnpts = grnpts([1,1:end]);
0333    blupts = blupts([1,1:end]);
0334    interppts = [-1,interppts];
0335 
0336    red = interp1(interppts, redpts,-scale_data, 'linear');
0337    grn = interp1(interppts, grnpts,-scale_data, 'linear');
0338    blu = interp1(interppts, blupts,-scale_data, 'linear');
0339 
0340    red = red.*alp + backgnd(1)*(1-alp);
0341    grn = grn.*alp + backgnd(2)*(1-alp);
0342    blu = blu.*alp + backgnd(3)*(1-alp);
0343           
0344    red = red/255;
0345    grn = grn/255;
0346    blu = blu/255;
0347 
0348 function [red,grn,blu]= timpel_colours(pp,scale_data)
0349    sd  = -scale_data*100;
0350    P00 = (sd<  9);
0351    p09 = (sd>= 9) .* (sd<10) .* (sd- 9) / (10- 9); P09 = p09 >0;
0352    p10 = (sd>=10) .* (sd<17) .* (sd-10) / (17-10); P10 = p10 >0;
0353    p17 = (sd>=17) .* (sd<30) .* (sd-17) / (30-17); P17 = p17 >0;
0354    p30 = (sd>=30) .* (sd<60) .* (sd-30) / (60-30); P30 = p30 >0;
0355    p60 = (sd>=60) .* (sd<85) .* (sd-60) / (85-60); P60 = p60 >0;
0356    p85 = (sd>=85)            .* (sd-85) /(100-85); P85 = p85 >0;
0357 
0358 
0359    red =  150*P00 + 198*p85;
0360    grn =  150*P00 +       88 *P09 + ...
0361            88*P10 +  (99- 88)*p10 + ...
0362            99*P17 + (121- 99)*p17 + ...
0363           121*P30 + (190-121)*p30 + ...
0364           190*P60 + (243-190)*p60 + ...
0365           243*P85 + (251-243)*p85;
0366    blu =  150*P00 +      159 *P09 + ...
0367           180*P10 + (180-159)*p10 + ...
0368           255*P17 + ...
0369           255*P30 + ...
0370           255*P60 + ...
0371           255*P85 ;
0372           
0373    red = red/255;
0374    grn = grn/255;
0375    blu = blu/255;
0376 
0377 % From Vinicius Torsani
0378 %These are the color codes from range of 0 to 1:
0379 %
0380 %         R       G       B
0381 % 0 to 0,09       150     150     150
0382 % to 0,1  0       88      159
0383 % to 0,17         0       99      180
0384 % to 0,3  0       121     255
0385 % to 0,6  0       190     255
0386 % to 0,85         0       243     255
0387 % to 1    198     251     255
0388 
0389 
0390 
0391 
0392 function [red,grn,blu] = draeger_colours(pp,scale_data, version);
0393    switch version
0394      case '2009'
0395        grn=      (-scale_data>0.2) .* (-scale_data - 0.2)/0.8;
0396        red=grn + ( scale_data>0.2) .* ( scale_data - 0.2)/0.8*2/3;
0397        
0398        blu=      (-scale_data>0.1) .* (-scale_data - 0.1)/0.1;
0399        blu(-scale_data>0.2) = 1;
0400        blu=blu + ( scale_data>0.2) .* ( scale_data - 0.2)/0.8*2/3;
0401      case 'tidal2014'
0402        sd  = -scale_data*100;
0403        p20 = (sd> 20) .* (sd<= 100) .* (sd - 20) / (100-20);
0404        P20 = (p20>0);
0405        p10 = (sd> 10) .* (sd<=  20) .* (sd - 10) / ( 20-10);
0406        n20 = (sd<-20) .* (sd>=-100) .* (sd + 20) /-(100-20);
0407        red = 255*p20 + 170*n20;
0408        grn = 255*p20;
0409        blu = 255*P20 + 255*p10 + 170*n20;
0410 
0411        red = red/255;
0412        grn = grn/255;
0413        blu = blu/255;
0414 
0415      case 'difference2014';
0416        sd  = -scale_data*100;
0417        p40 = (sd> 40) .* (sd<= 100) .* (sd - 40) / (100-40);
0418        P40 = (p40>0);
0419        p10 = (sd> 10) .* (sd<=  40) .* (sd - 10) / ( 40-10);
0420        n10 = (sd<-10) .* (sd>=- 40) .* (sd + 10) /-( 40-10);
0421        n40 = (sd<-40) .* (sd>=-100) .* (sd + 40) /-(100-40);
0422        N40 = (n40>0);
0423        red =  132*p40  + 255*n10  + 255*N40;
0424        grn =  153*p10  + 153*P40 + (240-153)*P40 + ...
0425               153*n10  + 153*N40 + (240-153)*n40;
0426        blu =  153*n40  + 255*p10 +  255*P40;
0427 
0428        red = red/255;
0429        grn = grn/255;
0430        blu = blu/255;
0431      otherwise; error('version "%s" not recognized', version);
0432    end
0433 
0434 
0435 function [red,grn,blu] = jet_colours(pp,scale_data);
0436    grn = 2*(3/4 - abs(scale_data));
0437    grn(grn>1) = 1; grn(grn<0) = 0;
0438 
0439    red = 1.5 - 2*abs(scale_data + 0.5);
0440    red(red>1) = 1; red(red<0) = 0;
0441 
0442    blu = 1.5 - 2*abs(scale_data - 0.5);
0443    blu(blu>1) = 1; blu(blu<0) = 0;
0444 
0445 % TODO: only works with mapped_colours, fix to use scale_data
0446 function [red,grn,blu] = blue_yellow_colours(pp,scale_data);
0447    % Inpired by
0448    % FIREICE LightCyan-Cyan-Blue-Black-Red-Yellow-LightYellow Colormap
0449    % by: Joseph Kirk %  jdkirk630@gmail.com
0450    clrs = flipud([0.75 1 1; 0 1 1; 0 0 1;...
0451        0 0 0; 1 0 0; 1 1 0; 1 1 0.75]);
0452 
0453    y = linspace(-1,1,size(clrs,1));
0454    cc = interp2(1:3,y,clrs,1:3,scale_data);
0455    red = cc(:,1);
0456    grn = cc(:,2);
0457    blu = cc(:,3);
0458 
0459 
0460 % TODO: only works with mapped_colours, fix to use scale_data
0461 function [red,grn,blu]= greyscale_colours(pp,scale_data);
0462    cc= 0.5 - scale_data/2;
0463    red = cc;
0464    grn = cc;
0465    blu = cc;
0466 
0467    
0468 
0469 % TODO: only works with mapped_colours, fix to use scale_data
0470 function [red,grn,blu]= copper_colours(pp,scale_data);
0471 % Constants from the copper.m function
0472    cc= 0.5 + scale_data/2;
0473    red = min(cc * 1.2500,1);
0474    grn = cc * 0.7812;
0475    blu = cc * 0.4975;
0476 
0477 function [red,grn,blu]= blue_white_red_colours(pp,scale_data);
0478    % bluewhitered Colormap
0479    % Based on FIREICE from Joseph Kirk jdkirk630@gmail.com
0480    clrs = flipud([0 0 1; 1 1 1; 1 0 0]);
0481 
0482    y = linspace(-1,1,size(clrs,1));
0483    cc = interp2(1:3,y,clrs,1:3,scale_data);
0484    red = cc(:,1);
0485    grn = cc(:,2);
0486    blu = cc(:,3);
0487 
0488 
0489 
0490 function [red,grn,blu] = black_red_colours(pp,scale_data);
0491    red = 0.5 + scale_data/2;
0492    blu = 0*scale_data;
0493    grn = blu;
0494 
0495 % TODO: only works with mapped_colours, fix to use scale_data
0496 function [red,grn,blu] = blue_black_red_colours(pp,scale_data);
0497    red =-min(0,scale_data);
0498    blu = max(0,scale_data);
0499    grn = 0*scale_data;
0500 
0501 % TODO: only works with mapped_colours, fix to use scale_data
0502 % Ideas for this colourmap, and a few lines of code are from
0503 % (C) 2011, Francois Beauducel, IPGP in polarmap.m
0504 function [red,grn,blu] = polar_blue_white_red_colours(pp,scale_data);
0505 %  0   0   1
0506 %  .5  .5  1
0507 %  1   1   1
0508 %  1   .5  .5
0509 %  1   0   0
0510    red = min(1, 1 + scale_data);
0511    grn = 1 - abs(scale_data);
0512    blu = min(1, 1 - scale_data);
0513 
0514 
0515 function pp=get_colours( img );
0516    global eidors_colours;
0517    pp= eidors_colours;
0518 
0519    pp.component = 'real'; % Don't get from globals
0520 
0521 % override global if calc.colours specified
0522    try
0523 % DAMN Matlab should have syntax for this loop
0524       fds= fieldnames(img(1).calc_colours);%assume all are the same!
0525       for fdn= fds(:)';
0526          fdn= fdn{1};
0527          pp.( fdn ) = img(1).calc_colours.(fdn);
0528       end
0529    end
0530    if isfield(img, 'eidors_colourbar')
0531       pp.cb_shrink_move = [1,1,0];
0532       try
0533          pp.cb_shrink_move = img.eidors_colourbar.cb_shrink_move;
0534       end
0535    end
0536 
0537 function BGI= backgndidx;
0538   BGI= 1;
0539 
0540 function colours=set_mapped_colour(pp, backgnd, img_data)
0541    % need to generate a colourmap with pp.mapped_colour+1 elements
0542    % background pixel will be at entry #1. Thus for
0543    % mapped_colour= 3. CMAP = [backgnd,[-1 -.5  0 .5 1]
0544    %
0545    % Note: ensure patch uses 'direct' CDataMapping
0546    ncol= pp.mapped_colour;
0547    [red,grn,blu] = blu_red_axis( pp, ...
0548           [-1,linspace(-1,1,2*ncol - 1)]', backgndidx );
0549    colormap([red,grn,blu]);
0550 
0551    %FIXME - come up with a general way to handle blocks in image field
0552    if isfield(pp,'image_field')
0553       warning(['calc_colours: image_field is an experimental feature ' ...
0554                               'and will be re-implemented']);
0555       infmask = isinf(img_data);
0556       mindata = min(img_data(~infmask));
0557       colours = fix( (img_data-mindata) * (ncol-1))' + 3;
0558       colours(backgnd)= backgndidx;
0559       colours(pp.image_field_idx)=2;
0560       return
0561    end
0562 
0563 % This is the line I wan't to write. However, matlab seems to waste
0564 %  lots of memory to do it. Instead we break it into pieces
0565 %  colours = fix( img_data * (ncol-1))' + ncol + 1;
0566    colours = img_data'; colours = colours * (ncol-1);
0567    colours = fix ( colours ); colours = colours + ncol + 1;
0568    colours(backgnd)= backgndidx;
0569 
0570 function value= get_field(param);
0571    global eidors_colours;
0572    if strcmp(param,'colourmap')
0573       ncol= eidors_colours.mapped_colour;
0574       [red,grn,blu] = blu_red_axis( eidors_colours, ...
0575              [-1,linspace(-1,1,2*ncol - 1)]', backgndidx );
0576       value= [red,grn,blu];
0577    else
0578       value = getfield(eidors_colours, param);
0579    end
0580 
0581 function value= set_field(param, value);
0582    global eidors_colours;
0583    eidors_colours = setfield(eidors_colours, param, value);
0584    value= eidors_colours;
0585 
0586 % TESTS:
0587 function do_unit_test
0588    calc_colours('defaults');
0589 
0590    img = eidors_obj('image','test'); 
0591 
0592    img.calc_colours.mapped_colour = 127;
0593    img.calc_colours.ref_level = 'auto';
0594    img.calc_colours.sat_adj = 0.9;
0595    img.calc_colours.window_range = 0.9;
0596    img.calc_colours.greylev = -0.01;
0597  
0598    img.elem_data = [-2;0;0;0;1;3];
0599    unit_test_cmp('cc01a', calc_colours(img), [44; 128; 128; 128; 170; 254]);
0600 
0601    imgk(1) = img; imgk(2) = img;
0602    unit_test_cmp('cc01b', calc_colours(imgk), [44; 128; 128; 128; 170; 254]*[1,1]);
0603    img.calc_colours.ref_level = 0;
0604    unit_test_cmp('cc01c', calc_colours(img), [44; 128; 128; 128; 170; 254]);
0605 
0606    img.calc_colours.ref_level = 1;
0607    unit_test_cmp('cc02', calc_colours(img), [ 2;  86;  86;  86; 128; 212]);
0608 
0609    img.calc_colours.greylev = -.1;
0610    img.calc_colours.mapped_colour = 0;
0611    img.elem_data = [-2;1;3];
0612    cm= reshape([ 0, 0.9, 0.9; 0, 0.9, 0.0643; 0.27, 0.9, 0]',[3,1,3]);
0613    unit_test_cmp('cc03', calc_colours(img), cm,1e-3)
0614 
0615    img.calc_colours.greylev =  .1;
0616    cm= reshape([ 0.73, 1.0, 1.0; 0.1, 0.1, 0.1; 1.0, 0.9357, 0.1],[3,1,3]);
0617    unit_test_cmp('cc04', calc_colours(img), cm,1e-3)
0618 
0619    calc_colours('greylev',0);
0620    calc_colours('sat_adj',1);
0621    calc_colours('mapped_colour',4);
0622    calc_colours('backgnd',[0.5,0.5,0.5]);
0623    cc= calc_colours('colourmap');
0624    unit_test_cmp('cc05',cc, ...
0625       [2,2,2;4,4,4;2,4,4;0,1,4;0,0,0;4,1,0;4,4,2;4,4,4]/4,1e-10);
0626 
0627    calc_colours('greylev',-.01);
0628    cc= calc_colours('colourmap');
0629    unit_test_cmp('cc06',cc, [200,200,200;0,0,0;0,0,198; ...
0630         0,297,396;396,396,396;396,297,0;198,0,0;0,0,0]/400,1e-10);
0631 
0632    calc_colours('greylev',0);
0633    calc_colours('sat_adj',.5);
0634    cc= calc_colours('colourmap');
0635    unit_test_cmp('sa01',cc(2,:), [0,5,10]/10,1e-10);
0636 
0637    calc_colours('sat_adj',.8);
0638    cc= calc_colours('colourmap');
0639    unit_test_cmp('sa02',cc(2,:), [4,10,10]/10,1e-10);
0640 
0641    calc_colours('sat_adj',1 );
0642    cc= calc_colours('colourmap');
0643    unit_test_cmp('sa03',cc(2,:), [10,10,10]/10,1e-10);
0644 
0645 %   'window_range' (DEFAULT .9)
0646    unit_test_cmp('bg01',cc(1,:), [50,50,50]/100, 1e-10);
0647    calc_colours('defaults');
0648    cc= calc_colours('colourmap');
0649 %  unit_test_cmp('bg01',cc(1,:), [50,50,15]/100, 1e-10); %EIDORS 3.6
0650 %  unit_test_cmp('bg02',cc(1,:), [20,40,40]/100, 1e-10); %EIDORS 3.7
0651    unit_test_cmp('bg02',cc(1,:), [35,45,45]/100, 1e-10); %EIDORS 3.8
0652 
0653    unit_test_cmp('mc01',size(cc), [127*2,3]);
0654    calc_colours('mapped_colour',4);
0655    cc= calc_colours('colourmap');
0656    unit_test_cmp('mc02',size(cc), [4*2,3]);
0657 %   'ref_level' (DEFAULT 'auto')
0658 %   'clim'    (DEFAULT [])
0659 %   'cmap_type'  (Default blue-red)
0660 
0661    test.blue_red = [0,0,2997; 9990,9990,9990; 2997,0,0]/1e4;
0662 %  test.draeger  = [10000,10000,10000;0,0,0;6667,0,6667]/1e4; % OLD 2009!
0663    test.draeger  = [5176,9412,10000;0,0,0;10000,9412,6000]/1e4; % 2014
0664    test.timpel   = [7765,9843,10000;5882,5882,5882;5882,5882,5882]/1e4;
0665    test.jet      = [5000,0,0;5000,10000,5000;0,0,5000]/1e4;
0666    test.jetair   = [8333,0,0; 0,0,8333;0,0,0]/1e4;
0667    test.blue_yellow = [10000,10000,7500; 0,0,0;7500,10000,10000]/1e4;
0668    test.greyscale = flipud([0,0,0;5000,5000,5000;10000,10000,10000])/1e4;
0669    test.copper = [0,0,0;6250,3906,2487;10000,7812,4975]/1e4;
0670    test.blue_white_red = [10000,0,0;10000,10000,10000;0,0,10000]/1e4;
0671    test.black_red = [0,0,0;5000,0,0;10000,0,0]/1e4;
0672    test.blue_black_red = [10000,0,0;0,0,0;0,0,10000]/1e4;
0673    test.polar_colours = [0,0,10000;10000,10000,10000;10000,0,0]/1e4;
0674 
0675    calc_colours('defaults');
0676    calc_colours('mapped_colour',4);
0677    for ct = fieldnames(test)'
0678       ct = ct{1};
0679       tname = sprintf('ctm4(%s)',ct);
0680       calc_colours('cmap_type', ct);
0681       cc= calc_colours('colourmap');
0682       unit_test_cmp(tname,cc([2,5,8],:), test.( ct ), 1e-4);
0683    end
0684 
0685    calc_colours('defaults');
0686    calc_colours('mapped_colour',0);
0687    for ct = fieldnames(test)'
0688       ct = ct{1};
0689       tname = sprintf('ctm0(%s)',ct);
0690       calc_colours('cmap_type', ct);
0691 
0692       cc= calc_colours(linspace(-1,1,7)); cc= squeeze(cc);
0693       unit_test_cmp(tname,cc([1,4,7],:), test.( ct ), 1e-4);
0694    end
0695 
0696    calc_colours('defaults');

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005