changeset 19647:e8e3a89fa370

Fix colormap functions when N is not of class double (bug #44070) * autumn.m, bone.m, cool.m, copper.m, cubehelix.m, gray.m, hsv.m, ocean.m, rainbow.m, spring.m, summer.m, winter.m: cast N to double since in this colormap functions, N is used directly used in the operations and ends up casting colormap to something else. Note that a colormap MUST be of class double. Also, if N is an integer class, all values in colormap end up 0 or 1. Also simplify code for input checking by declaring default on function signature line. * flag.m, gmap40.m, hot.m, jet.m, lines.m, pink.m, prism.m, white.m: simplify input check like the other colormap functions. * test/colormaps.tst: add new test file to test all colormaps at once. * test/Makefile.am: add new test file to build system. used to create the colormap and if less
author Carnë Draug <carandraug@octave.org>
date Fri, 23 Jan 2015 15:37:56 +0000
parents 68cc16969f78
children 2f4406e9dad6
files scripts/image/autumn.m scripts/image/bone.m scripts/image/cool.m scripts/image/copper.m scripts/image/cubehelix.m scripts/image/flag.m scripts/image/gmap40.m scripts/image/gray.m scripts/image/hot.m scripts/image/hsv.m scripts/image/jet.m scripts/image/lines.m scripts/image/ocean.m scripts/image/pink.m scripts/image/prism.m scripts/image/rainbow.m scripts/image/spring.m scripts/image/summer.m scripts/image/white.m scripts/image/winter.m test/Makefile.am test/colormaps.tst
diffstat 22 files changed, 126 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/autumn.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/autumn.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,17 +31,14 @@
 ## PKG_ADD: colormap ("register", "autumn");
 ## PKG_DEL: colormap ("unregister", "autumn");
 
-function map = autumn (n)
+function map = autumn (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("autumn: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("autumn: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
@@ -56,7 +53,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'autumn' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/bone.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/bone.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,17 +31,14 @@
 ## PKG_ADD: colormap ("register", "bone");
 ## PKG_DEL: colormap ("unregister", "bone");
 
-function map = bone (n)
+function map = bone (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("bone: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("bone: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [1/8 1/8 1/8];
@@ -83,7 +80,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'bone' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/cool.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/cool.m	Fri Jan 23 15:37:56 2015 +0000
@@ -30,17 +30,14 @@
 ## PKG_ADD: colormap ("register", "cool");
 ## PKG_DEL: colormap ("unregister", "cool");
 
-function map = cool (n)
+function map = cool (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("cool: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("cool: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 1, 1];
@@ -55,7 +52,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'cool' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/copper.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/copper.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,17 +31,14 @@
 ## PKG_ADD: colormap ("register", "copper");
 ## PKG_DEL: colormap ("unregister", "copper");
 
-function map = copper (n)
+function map = copper (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("copper: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("copper: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
@@ -58,7 +55,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'copper' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/cubehelix.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/cubehelix.m	Fri Jan 23 15:37:56 2015 +0000
@@ -54,6 +54,7 @@
   elseif (! isscalar (n))
     error ("cubehelix: N must be a scalar");
   endif
+  n = double (n);
 
   if (n > 1)
     coeff = [ -0.14861  -0.29227   1.97294
--- a/scripts/image/flag.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/flag.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "flag");
 ## PKG_DEL: colormap ("unregister", "flag");
 
-function map = flag (n)
+function map = flag (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("flag: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("flag: N must be a scalar");
   endif
 
   if (n == 1)
@@ -54,7 +50,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'flag' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/gmap40.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/gmap40.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "gmap40");
 ## PKG_DEL: colormap ("unregister", "gmap40");
 
-function map = gmap40 (n)
+function map = gmap40 (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = 6;
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("gmap40: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("gmap40: N must be a scalar");
   endif
 
   if (n > 0)
@@ -52,7 +48,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'gmap40' colormap as an image
 %! image (1:6, linspace (0, 1, 6), repmat ((1:6)', 1, 6));
--- a/scripts/image/gray.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/gray.m	Fri Jan 23 15:37:56 2015 +0000
@@ -33,17 +33,14 @@
 ## PKG_ADD: colormap ("register", "gray");
 ## PKG_DEL: colormap ("unregister", "gray");
 
-function map = gray (n)
+function map = gray (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("gray: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("gray: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
@@ -56,7 +53,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'gray' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/hot.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/hot.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "hot");
 ## PKG_DEL: colormap ("unregister", "hot");
 
-function map = hot (n)
+function map = hot (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("hot: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("hot: N must be a scalar");
   endif
 
   if (n == 1)
@@ -72,7 +68,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'hot' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/hsv.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/hsv.m	Fri Jan 23 15:37:56 2015 +0000
@@ -35,17 +35,14 @@
 ## PKG_ADD: colormap ("register", "hsv");
 ## PKG_DEL: colormap ("unregister", "hsv");
 
-function map = hsv (n)
+function map = hsv (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("hsv: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("hsv: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
@@ -58,7 +55,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'hsv' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/jet.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/jet.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "jet");
 ## PKG_DEL: colormap ("unregister", "jet");
 
-function map = jet (n)
+function map = jet (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("jet: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("jet: N must be a scalar");
   endif
 
   if (n == 1)
@@ -91,7 +87,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'jet' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/lines.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/lines.m	Fri Jan 23 15:37:56 2015 +0000
@@ -30,16 +30,12 @@
 ## PKG_ADD: colormap ("register", "lines");
 ## PKG_DEL: colormap ("unregister", "lines");
 
-function map = lines (n)
+function map = lines (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("lines: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("lines: N must be a scalar");
   endif
 
   if (n == 1)
@@ -54,7 +50,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'lines' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/ocean.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/ocean.m	Fri Jan 23 15:37:56 2015 +0000
@@ -33,17 +33,14 @@
 ## PKG_ADD: colormap ("register", "ocean");
 ## PKG_DEL: colormap ("unregister", "ocean");
 
-function map = ocean (n)
+function map = ocean (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("ocean: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("ocean: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
@@ -65,7 +62,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'ocean' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/pink.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/pink.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "pink");
 ## PKG_DEL: colormap ("unregister", "pink");
 
-function map = pink (n)
+function map = pink (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("pink: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("pink: N must be a scalar");
   endif
 
   if (n == 1)
@@ -72,7 +68,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'pink' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/prism.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/prism.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,16 +31,12 @@
 ## PKG_ADD: colormap ("register", "prism");
 ## PKG_DEL: colormap ("unregister", "prism");
 
-function map = prism (n)
+function map = prism (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("prism: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("prism: N must be a scalar");
   endif
 
   if (n == 1)
@@ -54,7 +50,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'prism' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/rainbow.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/rainbow.m	Fri Jan 23 15:37:56 2015 +0000
@@ -34,17 +34,14 @@
 ## PKG_ADD: colormap ("register", "rainbow");
 ## PKG_DEL: colormap ("unregister", "rainbow");
 
-function map = rainbow (n)
+function map = rainbow (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("rainbow: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("rainbow: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
@@ -69,7 +66,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'rainbow' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/spring.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/spring.m	Fri Jan 23 15:37:56 2015 +0000
@@ -30,17 +30,14 @@
 ## PKG_ADD: colormap ("register", "spring");
 ## PKG_DEL: colormap ("unregister", "spring");
 
-function map = spring (n)
+function map = spring (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("spring: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("spring: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [1, 0, 1];
@@ -55,7 +52,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'spring' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/summer.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/summer.m	Fri Jan 23 15:37:56 2015 +0000
@@ -31,17 +31,14 @@
 ## PKG_ADD: colormap ("register", "summer");
 ## PKG_DEL: colormap ("unregister", "summer");
 
-function map = summer (n)
+function map = summer (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("summer: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("summer: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 0.5, 0.4];
@@ -56,7 +53,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'summer' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/white.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/white.m	Fri Jan 23 15:37:56 2015 +0000
@@ -30,23 +30,18 @@
 ## PKG_ADD: colormap ("register", "white");
 ## PKG_DEL: colormap ("unregister", "white");
 
-function map = white (n)
+function map = white (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("white: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("white: N must be a scalar");
   endif
 
   map = ones (n, 3);
 
 endfunction
 
-
 %!demo
 %! ## Show the 'white' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/scripts/image/winter.m	Fri Jan 23 16:33:02 2015 +0000
+++ b/scripts/image/winter.m	Fri Jan 23 15:37:56 2015 +0000
@@ -30,17 +30,14 @@
 ## PKG_ADD: colormap ("register", "winter");
 ## PKG_DEL: colormap ("unregister", "winter");
 
-function map = winter (n)
+function map = winter (n = rows (colormap ()))
 
-  if (nargin == 0)
-    n = rows (colormap);
-  elseif (nargin == 1)
-    if (! isscalar (n))
-      error ("winter: N must be a scalar");
-    endif
-  else
+  if (nargin > 1)
     print_usage ();
+  elseif (! isscalar (n))
+    error ("winter: N must be a scalar");
   endif
+  n = double (n);
 
   if (n == 1)
     map = [0, 0, 1];
@@ -55,7 +52,6 @@
 
 endfunction
 
-
 %!demo
 %! ## Show the 'winter' colormap as an image
 %! image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
--- a/test/Makefile.am	Fri Jan 23 16:33:02 2015 +0000
+++ b/test/Makefile.am	Fri Jan 23 15:37:56 2015 +0000
@@ -25,6 +25,7 @@
   args.tst \
   bug-31371.tst \
   bug-38576.tst \
+ colormaps.tst \
   diag-perm.tst \
   error.tst \
   eval-catch.tst \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/colormaps.tst	Fri Jan 23 15:37:56 2015 +0000
@@ -0,0 +1,37 @@
+## Copyright (C) 2015 Carnë Draug
+##
+## This file is part of Octave.
+##
+## Octave 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 Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+%!test
+%! all_colormaps = colormap ("list");
+%!
+%! assert (numel (all_colormaps) > 0)
+%!
+%! for i = 1:numel (all_colormaps)
+%!   f = str2func (all_colormaps{i});
+%!
+%!   assert (iscolormap (f (1)))
+%!   assert (iscolormap (f (12)))
+%!   assert (iscolormap (f (200)))
+%!
+%!   ## bug #44070
+%!   assert (class (f (uint8 (12))), "double")
+%!   assert (iscolormap (f (uint8 (12))))
+%!
+%!   assert (f (0), zeros (0, 3))
+%! endfor
+