changeset 24800:2dc04b6e1740

Don't create new figure when calling colormap functions (bug #53217). * autumn.m, bone.m, contrast.m, cool.m, copper.m, cubehelix.m, flag.m, gray.m, hot.m, hsv.m, jet.m, lines.m, ocean.m, pink.m, prism.m, rainbow.m, spring.m, summer.m, viridis.m, white.m, winter.m: Check root figure for a CurrentFigure and use the number of rows in its colormap if it is available. Otherwise, use default of 64.
author Rik <rik@octave.org>
date Tue, 27 Feb 2018 08:57:21 -0800
parents 74a596fd6bab
children daf61c7dfcae
files scripts/image/autumn.m scripts/image/bone.m scripts/image/contrast.m scripts/image/cool.m scripts/image/copper.m scripts/image/cubehelix.m scripts/image/flag.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/viridis.m scripts/image/white.m scripts/image/winter.m
diffstat 21 files changed, 271 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/autumn.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/autumn.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = autumn (n = rows (colormap ()))
+function map = autumn (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("autumn: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("autumn: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
--- a/scripts/image/bone.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/bone.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = bone (n = rows (colormap ()))
+function map = bone (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("bone: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("bone: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [1/8 1/8 1/8];
--- a/scripts/image/contrast.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/contrast.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,20 @@
 
 function cmap = contrast (x, n)
 
-  if (nargin == 1)
-    n = rows (colormap ());
-  elseif (nargin == 2)
+  if (nargin > 2)
+    print_usage ();
+  elseif (nargin == 1)
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
+  else
     if (! isscalar (n))
       error ("contrast: N must be a scalar");
     endif
-  else
-    print_usage ();
+    n = double (n);
   endif
 
   x = x(:);
--- a/scripts/image/cool.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/cool.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = cool (n = rows (colormap ()))
+function map = cool (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("cool: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("cool: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 1, 1];
--- a/scripts/image/copper.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/copper.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = copper (n = rows (colormap ()))
+function map = copper (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("copper: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("copper: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
--- a/scripts/image/cubehelix.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/cubehelix.m	Tue Feb 27 08:57:21 2018 -0800
@@ -43,16 +43,24 @@
 
 ## Author: Carnë Draug <carandraug@octave.org>
 
-function map = cubehelix (n = rows (colormap ()), start = 0.5,
-                          rots = -1.5, hue = 1, gamma = 1)
+function map = cubehelix (n, start = 0.5, rots = -1.5, hue = 1, gamma = 1)
 
   if (nargin > 5)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("cubehelix: N must be a scalar");
+  elseif (nargin > 0)
+    if (! isscalar (n))
+      error ("cubehelix: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
 
-  n = double (n);
   if (n > 1)
     coeff = [ -0.14861  -0.29227   1.97294
                1.78277  -0.90649   0.00000];
--- a/scripts/image/flag.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/flag.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = flag (n = rows (colormap ()))
+function map = flag (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("flag: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("flag: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = [1, 0, 0];
   elseif (n > 1)
--- a/scripts/image/gray.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/gray.m	Tue Feb 27 08:57:21 2018 -0800
@@ -31,14 +31,23 @@
 ## Created: July 1994
 ## Adapted-By: jwe
 
-function map = gray (n = rows (colormap ()))
+function map = gray (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("gray: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("gray: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
--- a/scripts/image/hot.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/hot.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = hot (n = rows (colormap ()))
+function map = hot (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("hot: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("hot: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = [1, 1, 1];
   elseif (n == 2)
--- a/scripts/image/hsv.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/hsv.m	Tue Feb 27 08:57:21 2018 -0800
@@ -33,14 +33,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = hsv (n = rows (colormap ()))
+function map = hsv (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("hsv: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("hsv: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
--- a/scripts/image/jet.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/jet.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = jet (n = rows (colormap ()))
+function map = jet (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("jet: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("jet: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = [0, 1, 1];
   elseif (n == 2)
--- a/scripts/image/lines.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/lines.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,23 @@
 ## @seealso{colormap}
 ## @end deftypefn
 
-function map = lines (n = rows (colormap ()))
+function map = lines (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("lines: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("lines: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = [0, 0, 1];
   elseif (n > 1)
--- a/scripts/image/ocean.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/ocean.m	Tue Feb 27 08:57:21 2018 -0800
@@ -31,14 +31,23 @@
 ## Created: July 1994
 ## Adapted-By: jwe
 
-function map = ocean (n = rows (colormap ()))
+function map = ocean (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("ocean: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("ocean: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 0, 0];
--- a/scripts/image/pink.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/pink.m	Tue Feb 27 08:57:21 2018 -0800
@@ -31,14 +31,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = pink (n = rows (colormap ()))
+function map = pink (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("pink: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("pink: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = sqrt ([1/3, 1/3, 1/3]);
   elseif (n == 2)
--- a/scripts/image/prism.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/prism.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = prism (n = rows (colormap ()))
+function map = prism (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("prism: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("prism: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   if (n == 1)
     map = [1 0 0];
   elseif (n > 1)
--- a/scripts/image/rainbow.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/rainbow.m	Tue Feb 27 08:57:21 2018 -0800
@@ -32,14 +32,23 @@
 ## this colormap is not part of matlab, it is like the prism
 ## colormap map but with a continuous map
 
-function map = rainbow (n = rows (colormap ()))
+function map = rainbow (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("rainbow: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("rainbow: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [1, 0, 0];
--- a/scripts/image/spring.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/spring.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = spring (n = rows (colormap ()))
+function map = spring (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("spring: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("spring: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [1, 0, 1];
--- a/scripts/image/summer.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/summer.m	Tue Feb 27 08:57:21 2018 -0800
@@ -29,14 +29,23 @@
 ## Author:  Kai Habel <kai.habel@gmx.de>
 ## Date:  06/03/2000
 
-function map = summer (n = rows (colormap ()))
+function map = summer (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("summer: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("summer: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 0.5, 0.4];
--- a/scripts/image/viridis.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/viridis.m	Tue Feb 27 08:57:21 2018 -0800
@@ -36,14 +36,23 @@
 ## by Eric Firing.  The original file is distributed under CC0:
 ## http://creativecommons.org/publicdomain/zero/1.0
 
-function map = viridis (n = rows (colormap ()))
+function map = viridis (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("viridis: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("viridis: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   ## FIXME: Is there no algorithmic definition of the viridis colormap?
   persistent viridi = [0.26700401  0.00487433  0.32941519
                        0.26851048  0.00960483  0.33542652
--- a/scripts/image/white.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/white.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = white (n = rows (colormap ()))
+function map = white (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("white: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("white: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-
   map = ones (n, 3);
 
 endfunction
--- a/scripts/image/winter.m	Mon Feb 26 17:35:21 2018 -0800
+++ b/scripts/image/winter.m	Tue Feb 27 08:57:21 2018 -0800
@@ -28,14 +28,23 @@
 
 ## Author:  Kai Habel <kai.habel@gmx.de>
 
-function map = winter (n = rows (colormap ()))
+function map = winter (n)
 
   if (nargin > 1)
     print_usage ();
-  elseif (! isscalar (n))
-    error ("winter: N must be a scalar");
+  elseif (nargin == 1)
+    if (! isscalar (n))
+      error ("winter: N must be a scalar");
+    endif
+    n = double (n);
+  else
+    hf = get (0, "currentfigure");
+    if (! isempty (hf))
+      n = rows (get (hf, "colormap"));
+    else
+      n = 64;
+    endif
   endif
-  n = double (n);
 
   if (n == 1)
     map = [0, 0, 1];