function a = rgb2gray(r,g,b)
%RGB2GRAY Convert an RGB image or colormap to gray.
%	I = RGB2GRAY(R,G,B) returns the gray scale image equivalent
%	to the RGB image in the matrices R, G, B.  The matrices
%	R, G, B, contain intensities in the range from 0 to 1 of
%	the red, green, and blue components of the image.  The
%	intensity 0 corresponds to black, while the intensity 1
%	corresponds to full intensity.  The matrix I returned contains
%	gray scale intensities so that the triple I,I,I is a 
%	gray RGB image.
%
%	NEWMAP = RGB2GRAY(MAP) converts the M-by-3 colormap MAP to
%	the equivalent gray colormap MAP.
%
%	See also IND2GRAY, RGB2IND, NTSC2RGB, RGB2NTSC.

%	Clay M. Thompson 9-16-92
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.8 $  $Date: 1993/09/07 19:51:01 $

if nargin==1,
  rgb = r;
  a = zeros(size(r,1),1);

elseif nargin==2,
  error('Wrong number of arguments.');

else
  if (any(size(r)~=size(g)) | any(size(r)~=size(b))),
    error('R, G, and B must all be the same size.')
  end
  rgb = [r(:) g(:) b(:)];
  a = zeros(size(r));
end

T = inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);

a(:) = rgb*T(1,:)';

if nargin==1,
  a = [a,a,a];
end
