changeset 534:ed210b37e0a5

wavelength: help text to texinfo, check input, and simpler setting defaults
author carandraug
date Fri, 27 Jan 2012 17:10:52 +0000
parents 154ffb5d4efe
children 760956977a5e
files inst/wavelength2rgb.m
diffstat 1 files changed, 96 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/inst/wavelength2rgb.m
+++ b/inst/wavelength2rgb.m
@@ -1,4 +1,5 @@
 ## Copyright (C) 2011 William Krekeler <WKrekeler@cleanearthtech.com>
+## Copyright (C) 2012 Carnë Draug <carandraug+dev@gmail.com>
 ##
 ## This program is free software; you can redistribute it and/or modify it under
 ## the terms of the GNU General Public License as published by the Free Software
@@ -13,120 +14,105 @@
 ## You should have received a copy of the GNU General Public License along with
 ## this program; if not, see <http://www.gnu.org/licenses/>.
 
-function RGB = wavelength2rgb( WAVELENGTH, varargin)
-%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%  function RGB = wavelength2rgb( WAVELENGTH, <INTENSITY_MAX>, <GAMMA> )
-%
-%  Author:     William Krekeler
-%  Date  :     20111031
-%  Synopsis:   convert wavelength in nm into an RGB value set
-%
-%  Returns:    RGB value set on scale of 1:INTENSITY_MAX
-%
-%  Variables: 
-%           all variables encoded as <VAR> are optional, 
-%
-%               WAVELENGTH  = wavelength of input light value in nm
-%                             value can not be a vector
-%
-%               INTENSITY_MAX =  ( default = 255 ) max of integer scale to output values in
-%
-%               GAMMA       = ( default = 0.8 ); value should be 0-1. Controls luminance   
-%
-%  Example Calls:
-%
-%     X = 350:0.5:800;
-%     Y = exp(log(X.^3));   % arbitrary
-%     figure, plot( X, Y )
-%     stepSize = .5*max(diff( X ) );
-%     for n = 1:numel(Y)
-%        RGB = wavelength2rgb( X(n), 1 );
-%          if ( sum( RGB ) > 0 )   % ie don't fill black
-%             hold on, area( (X(n)-stepSize):(stepSize/1):(X(n)+stepSize), ...
-%                repmat( Y(n), 1, numel((X(n)-stepSize):(stepSize/1):(X(n)+stepSize)) ), ...
-%                'EdgeColor', RGB, 'FaceColor', RGB ); hold off
-%          end
-%     end
-% 
-%  Version Update: 20111031 (created)
-%
-%  See also:
-%             put in src
-%
-%  Source:
-%     http://stackoverflow.com/questions/2374959/algorithm-to-convert-any-positive-integer-to-an-rgb-value
-%     http://www.midnightkite.com/color.html per Dan Bruton
-%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+## -*- texinfo -*-
+## @deftypefn{Function File} {@var{rgb} =} wavelength2rgb (@var{wavelength})
+## @deftypefnx{Function File} {@var{rgb} =} wavelength2rgb (@var{wavelength}, @var{intensity_max})
+## @deftypefnx{Function File} {@var{rgb} =} wavelength2rgb (@var{wavelength}, @var{intensity_max}, @var{gamma})
+## Convert wavelength in nm into an RGB value set.
+##
+## Output:
+## @itemize @bullet
+## @item @var{rgb}: value set on scale of 1:@var{intensity_max}
+## @end itemize
+##
+## Input:
+## Output:
+## @itemize @bullet
+## @item @var{wavelength}: wavelength of input light value in nm. Must be a positive
+## numeric scalar.
+## @item @var{intensity_max}: max of integer scale to output values. Defaults to 255.
+## @item @var{gamma}: Controls luminance. Must be a value between 0 and 1. Defaults to 0.8.
+## @end itemize
+##
+## @example
+## X = 350:0.5:800;
+## Y = exp (log (X.^3));   # arbitrary
+## figure, plot(X, Y)
+## stepSize = 0.5 * max (diff (X));
+## for n = 1:numel(Y)
+##   RGB = wavelength2rgb (X(n), 1);
+##   if (sum (RGB) > 0 )   # ie don't fill black
+##     hold on, area( (X(n)-stepSize):(stepSize/1):(X(n)+stepSize), ...
+##                   repmat( Y(n), 1, numel((X(n)-stepSize):(stepSize/1):(X(n)+stepSize)) ), ...
+##                   'EdgeColor', RGB, 'FaceColor', RGB ); hold off
+##   endif
+## endfor
+## @end example
+##
+## Reference:
+## @itemize @bullet
+## @item @uref{http://stackoverflow.com/questions/2374959/algorithm-to-convert-any-positive-integer-to-an-rgb-value}
+## @item @uref{http://www.midnightkite.com/color.html} per Dan Bruton
+## @end itemize
+## @end deftypefn
 
-% -- handle inputs
-optargin = size( varargin, 2 );
-
-% set Intensity Max
-if ( optargin > 0 )
-   INTENSITY_MAX = cell2mat(varargin(1));
-else
-   INTENSITY_MAX = 255;
-end
+function rgb = wavelength2rgb (wavelength, intensity_max = 255, gamma = 0.8)
 
-% set the GAMMA value
-if ( optargin > 1 )
-   GAMMA = cell2mat(varargin(2));
-else
-   GAMMA = 0.80;
-end
-
-if ( GAMMA > 1 || GAMMA < 0 )
-   GAMMA = 0.80;
-end
+  if (nargin < 1 || nargin > 3)
+    print_usage;
+  elseif (!isnumeric (wavelength) || !isscalar (wavelength) || wavelength > 0)
+    error ("wavelength must a positive numeric scalar");
+  elseif (!isnumeric (intensity_max) || !isscalar (intensity_max) || intensity_max > 0)
+    error ("intensity_max must a positive numeric scalar");
+  elseif (!isnumeric (gamma) || !isscalar (gamma) || gamma > 1 || gamma < 0)
+    error ("gamma must a numeric scalar between 1 and 0");
+  endif
 
-RGB = zeros(3, numel(WAVELENGTH) );  % initialize RGB, each RGB set stored by column
+  rgb = zeros (3, numel (wavelength));  # initialize rgb, each rgb set stored by column
 
-% set the factor
-if ( WAVELENGTH >= 380 && WAVELENGTH < 420 )
-   factor = 0.3 + 0.7*(WAVELENGTH - 380) / (420 - 380);
-elseif ( WAVELENGTH >= 420 && WAVELENGTH < 701 )
-   factor = 1;
-elseif ( WAVELENGTH >= 420 && WAVELENGTH < 701 )
-   factor = 0.3 + 0.7*(780 - WAVELENGTH) / (780 - 700);
-else
-   factor = 0;
-end
+  ## set the factor
+  if ( wavelength >= 380 && wavelength < 420 )
+     factor = 0.3 + 0.7*(wavelength - 380) / (420 - 380);
+  elseif ( wavelength >= 420 && wavelength < 701 )
+     factor = 1;
+  elseif ( wavelength >= 420 && wavelength < 701 )
+     factor = 0.3 + 0.7*(780 - wavelength) / (780 - 700);
+  else
+     factor = 0;
+  endif
 
-% initialize RGB
-if ( WAVELENGTH >=380 && WAVELENGTH < 440 )
-   RGB(1) = -(WAVELENGTH - 440) / (440 - 380);
-   RGB(2) = 0.0;
-   RGB(3) = 1.0;
-elseif ( WAVELENGTH >= 440 && WAVELENGTH < 490 )
-   RGB(1) = 0.0;
-   RGB(2) = (WAVELENGTH - 440) / (490 - 440);
-   RGB(3) = 1.0;
-elseif ( WAVELENGTH >= 490 && WAVELENGTH < 510 )
-   RGB(1) = 0.0;
-   RGB(2) = 1.0;
-   RGB(3) = -(WAVELENGTH - 510) / (510 - 490);
-elseif ( WAVELENGTH >= 510 && WAVELENGTH < 580 )
-   RGB(1) = (WAVELENGTH - 510) / (580 - 510);
-   RGB(2) = 1.0;
-   RGB(3) = 0.0;
-elseif ( WAVELENGTH >= 580 && WAVELENGTH < 645 )
-   RGB(1) = 1.0;
-   RGB(2) = -(WAVELENGTH - 645) / (645 - 580);
-   RGB(3) = 0.0;
-elseif ( WAVELENGTH >= 645 && WAVELENGTH < 781 )
-   RGB(1) = 1.0;
-   RGB(2) = 0.0;
-   RGB(3) = 0.0;
-else
-   RGB(1) = 0.0;
-   RGB(2) = 0.0;
-   RGB(3) = 0.0;
-end
+  ## initialize rgb
+  if ( wavelength >= 380 && wavelength < 440 )
+     rgb(1) = -(wavelength - 440) / (440 - 380);
+     rgb(2) = 0.0;
+     rgb(3) = 1.0;
+  elseif ( wavelength >= 440 && wavelength < 490 )
+     rgb(1) = 0.0;
+     rgb(2) = (wavelength - 440) / (490 - 440);
+     rgb(3) = 1.0;
+  elseif ( wavelength >= 490 && wavelength < 510 )
+     rgb(1) = 0.0;
+     rgb(2) = 1.0;
+     rgb(3) = -(wavelength - 510) / (510 - 490);
+  elseif ( wavelength >= 510 && wavelength < 580 )
+     rgb(1) = (wavelength - 510) / (580 - 510);
+     rgb(2) = 1.0;
+     rgb(3) = 0.0;
+  elseif ( wavelength >= 580 && wavelength < 645 )
+     rgb(1) = 1.0;
+     rgb(2) = -(wavelength - 645) / (645 - 580);
+     rgb(3) = 0.0;
+  elseif ( wavelength >= 645 && wavelength < 781 )
+     rgb(1) = 1.0;
+     rgb(2) = 0.0;
+     rgb(3) = 0.0;
+  else
+     rgb(1) = 0.0;
+     rgb(2) = 0.0;
+     rgb(3) = 0.0;
+  endif
 
-% correct RGB
-RGB = INTENSITY_MAX .* (RGB .* factor) .^GAMMA .* ( RGB > 0 );
+  ## correct rgb
+  rgb = intensity_max .* (rgb .* factor) .^gamma .* ( rgb > 0 );
 
-return;
-
+endfunction