home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / GRAPHICS.DI$ / BLT.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  4.2 KB  |  171 lines

  1. function output_colors = blt( object, contrastColor, colors )
  2. %BLT Set line and text objects to Black or White.
  3. %    BLT is an M-file that will recursively change the color
  4. %    properties of the given object, or the current Figure window by 
  5. %    default, and its children to give proper black on white or white 
  6. %    on blackhardcopy output. The color of the line/text object depends
  7. %    on the contrastColor RGB triplet supplied, or the Figure's color
  8. %    attribute if none is supplied.
  9. %
  10. %    Returned are the colors of all line and text objects found while
  11. %    traversing the tree of objects. This can be used to restore the
  12. %    previous colors to all line and text objects. 
  13. %
  14. %    See also PRINT.
  15.  
  16. %       Copyright (c) 1984-93 by The MathWorks, Inc.
  17.  
  18. %
  19. % input is a) none  b) just colors  c) object and contrastColor  d) everything
  20. %
  21.  
  22. %
  23. % if only 1 input, it should be nx3 matrix of RGB values
  24. %
  25. if nargin == 1
  26.     if isempty(object)
  27.         return
  28.     end
  29.     colors = object;
  30.     if size(colors,2) ~= 3
  31.         error('Argument must be a 3-column matrix of RGB triplets.');
  32.     end
  33. end
  34.  
  35. %
  36. % assume current figure if given no input or only colors
  37. %
  38. if nargin < 2
  39.     object = gcf;
  40.     contrastColor = get(gcf,'color');
  41. end
  42.  
  43. %
  44. % get descendents in family tree of objects
  45. %
  46. kids = get(object, 'Children')';
  47.  
  48. if (nargin == 2) | (nargin == 0)
  49.  
  50.     %
  51.     % all current colors will be stored for return
  52.     %
  53.     colors = [];
  54.     old_colors = [];
  55.  
  56.     if  strcmp(get(object,'type'), 'axes')
  57.         for whichAxis = 'xyz'
  58.             c = get(object, [ whichAxis 'color' ]);
  59.             old_colors = [ old_colors; c];
  60.     
  61.             if (3 ~= sum(c == [1 1 1])) & (3 ~= sum(c == [0 0 0]))
  62.                 %
  63.                 % if contrastColor color converted to NTSC gray scale is close 
  64.                 % to white, set line/text to black, else white
  65.                 %
  66.                 if (contrastColor * [.3 ; .59 ; .11]) > .75
  67.                     set(object, [ whichAxis 'color' ], 'black');
  68.                 else
  69.                     set(object, [ whichAxis 'color' ], 'white');
  70.                 end
  71.             end
  72.         end
  73.  
  74.  
  75.     %
  76.     % need to replace current colors of line/text objects
  77.     %
  78.     elseif strcmp(get(object,'type'), 'line') | strcmp(get(object,'type'), 'text')
  79.         c = get(object, 'color');
  80.         old_colors = [colors; c];
  81.  
  82.         %
  83.         % is line/text object not black or white
  84.         %
  85.         if (3 ~= sum(c == [1 1 1])) & (3 ~= sum(c == [0 0 0]))
  86.             %
  87.             % if contrastColor color converted to NTSC gray scale is close 
  88.             % to white, set line/text to black, else white
  89.             %
  90.             if (contrastColor * [.3 ; .59 ; .11]) > .75
  91.                 set(object, 'color', 'black');
  92.             else
  93.                 set(object, 'color', 'white');
  94.             end
  95.         end
  96.  
  97.     elseif strcmp(get(object,'type'), 'surface') 
  98.         %
  99.         % see if it has the attributes of a mesh plot
  100.         %
  101.         c = get(object, 'Facecolor');
  102.         if ( (1 == size(c,1)) & (3 == size(c,2)) ...
  103.           & strcmp('flat', get(object, 'EdgeColor')) )
  104.             %
  105.             % give meshes a grid color that contrasts
  106.             %
  107.             if (contrastColor * [.3 ; .59 ; .11]) > .75
  108.                 set(object, 'EdgeColor', 'black');
  109.             else
  110.                 set(object, 'EdgeColor', 'white');
  111.             end
  112.             
  113.             %
  114.             % hold onto handle of this particular mesh object
  115.             % so we can replace flat shading 
  116.             %
  117.             old_colors = [colors; [object NaN NaN] ];
  118.         else
  119.             % space holder
  120.             old_colors = [colors; [NaN NaN NaN] ];          
  121.         end
  122.  
  123.     end
  124.  
  125.     for obj = kids
  126.         old_colors = [ old_colors ; blt(obj, contrastColor) ];
  127.     end
  128.  
  129. else
  130.     %
  131.     % need to set the object's color back to what it was, the old color
  132.     % was passed in 
  133.     %
  134.     old_colors = colors;
  135.  
  136.     if  strcmp(get(object,'type'), 'axes')
  137.         for whichAxis = 'xyz'
  138.             set(object, [ whichAxis 'color' ], old_colors(1,:) );
  139.             old_colors(1,:) = [];
  140.         end
  141.  
  142.     elseif strcmp(get(object,'type'), 'line') | strcmp(get(object,'type'), 'text')
  143.         set(object, 'color', old_colors(1,:) );
  144.         old_colors(1,:) = [];
  145.  
  146.     elseif strcmp(get(object,'type'), 'surface') 
  147.         %
  148.         % see if it is one of the mesh objects we turned from flat to colored
  149.         %
  150.         if object == old_colors(1,1)
  151.             set(object, 'Edgecolor', 'flat')
  152.             old_colors(1,:) = [];
  153.         end
  154.  
  155.     end
  156.  
  157.     for obj = kids
  158.         old_colors = blt(obj, contrastColor, old_colors);
  159.     end
  160.  
  161. end
  162.  
  163. %
  164. % echo the colors we changed, so a future call to blt can use 
  165. % swap the colors back, only if user asked for the output or set
  166. % ans if using all default input
  167. %
  168. if (nargout == 1) | (nargin == 0)
  169.     output_colors = old_colors;
  170. end
  171.