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

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005