exporting patch:
# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1470031285 18000
#      Mon Aug 01 01:01:25 2016 -0500
# Node ID 79927be853fae6c9f897785fb0ebcb24af71f3f4
# Parent  e35866e6a2e08a8fe14fc97c575ecff0c088b4cb
Rework color scaling for gnuplot toolkit (bug #48664)

* __gnuplot_draw_axes__.m: Remove cdatadirect variable and operate directly
on individual object 'cdatamapping' property.  If axes cautoscale is set, do
a first pass to get the 'clim' min and max of 'cdata' for all objects having
'direct' for 'cdatamapping' property.  If "image", "patch" or "surface"
object has 'cdatamapping' property set to scale, use a linearly scaling into
the 'clim' range.  Simplify the logic for setting gnuplot's cbrange to either
account for extended color map or simply use 'clim' form the cbrange.

diff --git a/scripts/plot/util/private/__gnuplot_draw_axes__.m b/scripts/plot/util/private/__gnuplot_draw_axes__.m
--- a/scripts/plot/util/private/__gnuplot_draw_axes__.m
+++ b/scripts/plot/util/private/__gnuplot_draw_axes__.m
@@ -430,7 +430,6 @@ function __gnuplot_draw_axes__ (h, plot_
   yautoscale = strcmp (axis_obj.ylimmode, "auto");
   zautoscale = strcmp (axis_obj.zlimmode, "auto");
   cautoscale = strcmp (axis_obj.climmode, "auto");
-  cdatadirect = false;
   truecolor = false;
 
   fputs (plot_stream, "set clip two;\n");
@@ -455,7 +454,39 @@ function __gnuplot_draw_axes__ (h, plot_
   hidden_removal = NaN;
   view_map = false;
 
-  if (! cautoscale && clim(1) == clim(2))
+  if (cautoscale)
+    ## First pass to get cdata limits, maybe general graphics should do this
+    kids1 = kids;
+    clim = [Inf -Inf];
+
+    while (! isempty (kids1))
+      obj = get (kids1(end));
+      kids1 = kids1(1:(end-1));
+
+      switch (obj.type)
+        case {"image", "patch", "surface"}
+          if (isfield (obj, "cdatamapping")
+              && strcmp (obj.cdatamapping, "direct")
+              && isfield (obj, "cdata"))
+            clim(1) = min (clim(1), min (obj.cdata(:)));
+            clim(2) = max (clim(2), max (obj.cdata(:)));
+          endif
+
+        case "hggroup"
+          ## Push group children into the kid list.
+          if (isempty (kids1))
+            kids1 = obj.children;
+          elseif (! isempty (obj.children))
+            kids1 = [kids1; obj.children];
+          endif
+      endswitch
+    endwhile
+
+    if (clim(1) == Inf)
+        clim = axis_obj.clim;
+    endif
+
+  elseif (clim(1) == clim(2))
     clim(2)++;
   endif
   addedcmap = [];
@@ -517,8 +548,11 @@ function __gnuplot_draw_axes__ (h, plot_
 
         if (ndims (img_data) == 3)
           truecolor = true;
-        elseif (strcmp (obj.cdatamapping, "direct"))
-          cdatadirect = true;
+        elseif (strcmp (obj.cdatamapping, "scaled"))
+          cmax = max (img_data(:));
+          cmin = min (img_data(:));
+          img_data = clim(1) + (img_data - cmin)*(clim(2) - clim(1))...
+                               /(cmax - cmin);
         endif
         data_idx += 1;
         is_image_data(data_idx) = true;
@@ -672,9 +706,8 @@ function __gnuplot_draw_axes__ (h, plot_
 
         if (! isempty (obj.cdata))
           cdat = obj.cdata;
-          if (strcmp (obj.cdatamapping, "direct"))
-            cdatadirect = true;
-          endif
+          cmax = max (cdat(:));
+          cmin = min (cdat(:));
         else
           cdat = [];
         endif
@@ -754,9 +787,14 @@ function __gnuplot_draw_axes__ (h, plot_
                       ## RGB Triplet
                       color = ccol;
                     elseif (nd == 3 && numel (xcol) == 3)
-                      ccdat = ccol;
+                      if (strcmp (obj.cdatamapping, "scaled"))
+                        ccdat = clim(1) + (ccol - cmin)*(clim(2) - clim(1))...
+                                          /(cmax - cmin);
+                      else
+                        ccdat = ccol;
+                      endif
                     else
-                      if (cdatadirect)
+                      if (strcmp (obj.cdatamapping, "direct"))
                         r = round (ccol);
                       else
                         r = 1 + round ((rows (cmap) - 1)
@@ -773,6 +811,9 @@ function __gnuplot_draw_axes__ (h, plot_
                              [1 : rows(ccdat)];
                         addedcmap = [addedcmap; ccdat];
                         ccdat = tmp(:);
+                      elseif (strcmp (obj.cdatamapping, "scaled"))
+                        ccdat = clim(1) + (ccdat(:) - cmin)*(clim(2) - clim(1))...
+                                          /(cmax - cmin);
                       else
                         ccdat = ccdat(:);
                       endif
@@ -780,7 +821,7 @@ function __gnuplot_draw_axes__ (h, plot_
                       if (sum (diff (ccol)))
                         warning ("\"interp\" not supported, using 1st entry of cdata");
                       endif
-                      if (cdatadirect)
+                      if (strcmp (obj.cdatamapping, "direct"))
                         r = round (ccol);
                       else
                         r = 1 + round ((rows (cmap) - 1)
@@ -1147,6 +1188,12 @@ function __gnuplot_draw_axes__ (h, plot_
         ydat = obj.ydata;
         zdat = obj.zdata;
         cdat = obj.cdata;
+        if (strcmp (obj.cdatamapping, "scaled"))
+          cmax = max (cdat(:));
+          cmin = min (cdat(:));
+          cdat = clim(1) + (cdat - cmin)*(clim(2) - clim(1))...
+                           /(cmax - cmin);
+        endif
         err = false;
         if (! size_equal (zdat, cdat))
           err = true;
@@ -1405,21 +1452,16 @@ function __gnuplot_draw_axes__ (h, plot_
   cmap = parent_figure_obj.colormap;
   cmap_sz = rows (cmap);
   if (! any (isinf (clim)))
-    if (truecolor || ! cdatadirect)
-      if (rows (addedcmap) > 0)
-        for i = 1:data_idx
-          if (have_3d_patch(i))
-            data{i}(end,:) = clim(2) * (data{i}(end, :) - 0.5) / cmap_sz;
-           endif
-        endfor
-        fprintf (plot_stream, "set cbrange [%.15e:%.15e];\n",
-                 clim(1), clim(2) * (cmap_sz + rows (addedcmap)) / cmap_sz);
-      else
-        fprintf (plot_stream, "set cbrange [%.15e:%.15e];\n", clim);
-      endif
+    if (truecolor && (rows (addedcmap) > 0))
+      for i = 1:data_idx
+        if (have_3d_patch(i))
+          data{i}(end,:) = clim(2) * (data{i}(end, :) - 0.5) / cmap_sz;
+         endif
+      endfor
+      fprintf (plot_stream, "set cbrange [%.15e:%.15e];\n",
+               clim(1), clim(2) * (cmap_sz + rows (addedcmap)) / cmap_sz);
     else
-      fprintf (plot_stream, "set cbrange [1:%d];\n", cmap_sz +
-               rows (addedcmap));
+      fprintf (plot_stream, "set cbrange [%.15e:%.15e];\n", clim);
     endif
   endif
 
