changeset 23524:7c278572db66

Implement colormap property for axes objects (bug #48667). * graphics.cc (convert_cdata): Get colormap map data from axes ancestor to current object rather than figure ancestor. * graphics.cc (axes::properties::get_colormap): get colormap from hidden axes property __colormap__. If it is empty, get colormap from figure ancestor. * graphics.in.h (axes): Add colormap property which can be queried or set. Add hidden __colormap__ property to hold colormap data. * colormap.m: Update to allow setting colormap on axes, not just figure object. Update documentation. * imshow.m: Apply any map for indexed images to the current axes rather than current figure.
author Markus Muetzel <markus.muetzel@gmx.de>
date Tue, 23 May 2017 13:37:17 -0700
parents d2748870db4e
children 645d1f5f2cb2
files libinterp/corefcn/graphics.cc libinterp/corefcn/graphics.in.h scripts/image/colormap.m scripts/image/imshow.m
diffstat 4 files changed, 37 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/graphics.cc	Tue May 23 14:27:44 2017 -0400
+++ b/libinterp/corefcn/graphics.cc	Tue May 23 13:37:17 2017 -0700
@@ -915,20 +915,15 @@
   Matrix clim (1, 2, 0.0);
 
   graphics_object go = gh_manager::get_object (props.get___myhandle__ ());
-  graphics_object fig = go.get_ancestor ("figure");
-
-  if (fig.valid_object ())
-    {
-      Matrix _cmap = fig.get (caseless_str ("colormap")).matrix_value ();
+  graphics_object ax = go.get_ancestor ("axes");
+
+  if (ax.valid_object ())
+    {
+      Matrix _cmap = ax.get (caseless_str ("colormap")).matrix_value ();
 
       cmap = _cmap;
-    }
-
-  if (is_scaled)
-    {
-      graphics_object ax = go.get_ancestor ("axes");
-
-      if (ax.valid_object ())
+
+      if (is_scaled)
         {
           Matrix _clim = ax.get (caseless_str ("clim")).matrix_value ();
 
@@ -5195,6 +5190,21 @@
   override_defaults (bgo);
 }
 
+octave_value
+axes::properties::get_colormap (void) const
+{
+  if (__colormap__.get ().is_empty ())
+    {
+      graphics_object go (gh_manager::get_object (get___myhandle__ ()));
+      graphics_object go_f (go.get_ancestor ("figure"));
+      figure::properties& figure_props
+        = reinterpret_cast<figure::properties&> (go_f.get_properties ());
+      return figure_props.get_colormap ();
+    }
+
+  return get___colormap__ ();
+}
+
 void
 axes::properties::delete_text_child (handle_property& hp)
 {
--- a/libinterp/corefcn/graphics.in.h	Tue May 23 14:27:44 2017 -0400
+++ b/libinterp/corefcn/graphics.in.h	Tue May 23 13:37:17 2017 -0700
@@ -3971,6 +3971,7 @@
       radio_property climmode al , "{auto}|manual"
       radio_property clippingstyle , "{3dbox}|rectangle"
       color_property color , color_property (color_values (1, 1, 1), radio_values ("none"))
+      array_property colormap sg , Matrix ()
       array_property colororder , default_colororder ()
       double_property colororderindex , 1.0
       array_property currentpoint , Matrix (2, 3, 0.0)
@@ -4070,6 +4071,7 @@
       double_property zticklabelrotation , 0.0
       radio_property ztickmode u , "{auto}|manual"
       // Octave-specific properties
+      array_property __colormap__ h , Matrix ()
       double_property mousewheelzoom , 0.5
       // hidden properties for alignment of subplots
       radio_property __autopos_tag__ h , "{none}|subplot"
@@ -4438,6 +4440,13 @@
 
     Matrix calc_tightbox (const Matrix& init_pos);
 
+    void set_colormap (const octave_value& val)
+    {
+      set___colormap__ (val);
+    }
+
+    octave_value get_colormap (void) const;
+
   public:
     Matrix get_axis_limits (double xmin, double xmax,
                             double min_pos, double max_neg,
--- a/scripts/image/colormap.m	Tue May 23 14:27:44 2017 -0400
+++ b/scripts/image/colormap.m	Tue May 23 13:37:17 2017 -0700
@@ -40,7 +40,7 @@
 ## is the name of a function that returns a colormap.
 ##
 ## If the first argument @var{hax} is an axes handle, then the colormap for
-## the parent figure of @var{hax} is queried or set.
+## those axes is queried or set.
 ##
 ## For convenience, it is also possible to use this function with the
 ## command form, @code{colormap @var{map_name}}.
@@ -93,7 +93,7 @@
   endif
 
   if (! isempty (hax))
-    cf = ancestor (hax, "figure");
+    cf = hax;
   else
     cf = get (0, "currentfigure");
   endif
--- a/scripts/image/imshow.m	Tue May 23 14:27:44 2017 -0400
+++ b/scripts/image/imshow.m	Tue May 23 13:37:17 2017 -0700
@@ -85,7 +85,7 @@
   if (ischar (im))
     [im, map] = imread (im);
     indexed = true;
-    colormap (map);
+    colormap (gca, map);
   endif
 
   nd = ndims (im);
@@ -96,7 +96,7 @@
 
   if (nd == 2)
     if (! indexed)
-      colormap (gray ());
+      colormap (gca, gray ());
     endif
   elseif (size (im, 3) == 3)
     if (ismember (class (im), {"uint8", "uint16", "double", "single"}))
@@ -117,7 +117,7 @@
       elseif (columns (arg) == 3)
         indexed = true;
         if (iscolormap (arg) && min (arg) >= 0 || max (arg) <= 1)
-          colormap (arg);
+          colormap (gca,  arg);
         else
           error ("imshow: invalid colormap MAP");
         endif
@@ -132,7 +132,7 @@
         case "colormap"
           map = varargin{narg++};
           if (iscolormap (map) && min (map) >= 0 || max (map) <= 1)
-            colormap (map);
+            colormap (gca, map);
           else
             error ("imshow: invalid colormap");
           endif