* Bug 13984 fixed: plot(..,'color',colors) unvectorized & limited colors names 26/19726/3
Samuel GOUGEON [Wed, 7 Feb 2018 08:33:44 +0000 (09:33 +0100)]
  http://bugzilla.scilab.org/13984

Change-Id: Ic55450e64ecc04631c64d482aee8911b16a2227e

scilab/CHANGES.md
scilab/modules/graphics/macros/iscolor.sci [new file with mode: 0644]
scilab/modules/graphics/macros/setPlotProperty.sci
scilab/modules/graphics/tests/nonreg_tests/bug_13984.tst [new file with mode: 0644]
scilab/modules/graphics/tests/unit_tests/iscolor.tst [new file with mode: 0644]

index ea2d0a6..fac82c5 100644 (file)
@@ -425,6 +425,7 @@ Known issues
 * [#13900](http://bugzilla.scilab.org/show_bug.cgi?id=13900): `nanmin` and `nanmax` were useless duplicates of `min` and `max`
 * [#13962](http://bugzilla.scilab.org/show_bug.cgi?id=13962): Preselecting a groupnamed radiobutton was impossible after having created it already once.
 * [#13979](http://bugzilla.scilab.org/show_bug.cgi?id=13979): A variable whose name is longer than 24 characters could not be cleared.
+* [#13984](http://bugzilla.scilab.org/show_bug.cgi?id=13984): For a set of curves, `plot(.., "color", colors..)` was not vectorized for colors specifications. Moreover, only 10 basic named colors were accepted, instead of the full list of predefined colors names.
 * [#14221](http://bugzilla.scilab.org/show_bug.cgi?id=14221): `msprintf` and `mprintf` ignored row-wise processing with `%%`.
 * [#14254](http://bugzilla.scilab.org/show_bug.cgi?id=14254): When installing an ATOMS module that is not locally registered and available, the error message was unclear.
 * [#14376](http://bugzilla.scilab.org/show_bug.cgi?id=14376): `input()` was broken: \n was introduced before prompting; multiple prompts occurred; the output value could be missing; "%" "\n" "\t" were no longer supported in messages...
diff --git a/scilab/modules/graphics/macros/iscolor.sci b/scilab/modules/graphics/macros/iscolor.sci
new file mode 100644 (file)
index 0000000..0bb9b36
--- /dev/null
@@ -0,0 +1,110 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - Samuel GOUGEON
+//
+// Copyright (C) 2012 - 2016 - Scilab Enterprises
+//
+// This file is hereby licensed under the terms of the GNU GPL v2.0,
+// pursuant to article 5.3.4 of the CeCILL v.2.1.
+// This file was originally licensed under the terms of the CeCILL v2.1,
+// and continues to be available under such terms.
+// For more information, see the COPYING file which you should have received
+// along with this program.
+
+// INTERNAL function to be called by plot(), bar(), barh(), nicholschart(),
+// hallchart(), etc..
+function rgb = iscolor(C, acceptedFormats)
+    // C: scalar, vector, matrix.. of colors specifications:
+    //    - integers: => % T if > 0 and <= cmapSize
+    //                       default colormap used = gcf().color_map
+    //    - strings: can mix predefined colors names and "#RRGGBB" specifications
+    //    - (Nx3) [r g b] matrix, with 0 =< r,g,b <= 1 as in the color map.
+    // acceptedFormats: single string (optional) made of 0 to 4 chars in any
+    //      order among
+    //     "1" : indices accepted
+    //     "." : [r g b] expected
+    //     "#" : "#RRGGBB" accepted (case insensitive)
+    //     "a" : colors names accepted
+    //   Default = "1.#a"  (all)
+    //
+    // rgb: (Nx3) matrix of 0 <= decimal numbers <= 1, with N = size(C, "*")
+    //      If C(i) is not a known color specification, rgb(i,:) = [-1 -1 -1]
+
+    fname = "iscolor"
+
+    // CHECKING INPUT ARGUMENTS
+    // ------------------------
+    rhs = argn(2)
+    if rhs<1 | rhs>3 then
+        msg = _("%s: Wrong number of input arguments: %d to %d expected.\n")
+        error(msprintf(msg, fname, 1, 3))
+    end
+    if ~or(type(C)==[1 10]) then
+        msg = _("%s: Argument #%d: Numbers or text expected.\n")
+        error(msprintf(msg, fname, 1, 3))
+    end
+    if type(C)==1 then
+        if ~isreal(C,0) then
+            msg = _("%s: Argument #%d: Decimal number(s) expected.\n")
+            error(msprintf(msg, fname, 1))
+        else
+            C = real(C)
+        end
+    end
+    if isdef("acceptedFormats", "l") then
+        if type(acceptedFormats)~= 10
+            msg = _("%s: Argument #%d: Text(s) expected.\n")
+            error(msprintf(msg, fname, 2))
+        end
+        acceptedFormats = acceptedFormats(1)
+    else
+        acceptedFormats = "1.#a";
+    end
+
+    // PROCESSING
+    // ----------
+    if type(C)==10 then
+        rgb = -ones(size(C,"*"), 3);
+        C = stripblanks(C);
+
+        // "#RRGGBB" cases
+        if grep(acceptedFormats, "#")~=[]
+            k = grep(C, "/^#[0-9a-f]{6}$/i", "r");
+            if k~=[]
+                s = strcat(part(C(k), 2:7));
+                s = matrix(strsplit(s, 2:2:length(s)-1), -1,3);
+                rgb(k,:) = hex2dec(s)/255;
+            end
+        end
+        // Predefined named colors
+        if grep(acceptedFormats, "a")~=[]
+            k = grep(C, "/^[a-z0-9 ]+$/i", "r");
+            for i = k
+                r = name2rgb(C(i));
+                if r~=[]
+                    rgb(i,:) = r / 255;
+                end
+            end
+        end
+    else
+        C = real(C)
+
+        // [r g b] case
+        if size(C,"c")==3 & grep(acceptedFormats, ".")~=[]
+            rgb = -ones(C);
+            k = find(and(C>=0 & C<=1, "c"));
+            rgb(k,:) = C(k,:);
+        else
+            // Colors indices
+            rgb = -ones(size(C, "*"), 3);
+            if grep(acceptedFormats, "1")~=[]
+                if winsid()==[]
+                    cmap = gdf().color_map;
+                else
+                    cmap = gcf().color_map;
+                end
+                k = find(C==int(C) & C>0 & C<=size(cmap,"r"))
+                rgb(k,:) = cmap(C(k),:);
+            end
+        end
+    end
+endfunction
index 573110f..7a2660e 100644 (file)
@@ -1,5 +1,7 @@
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) 2004-2006 - INRIA - Fabrice Leray
+// Copyright (C) 2018 - Samuel GOUGEON
+//
 // Copyright (C) 2012 - 2016 - Scilab Enterprises
 //
 // This file is hereby licensed under the terms of the GNU GPL v2.0,
@@ -28,46 +30,30 @@ function [fail]=setPlotProperty(PropertyName,PropertyValue,Curves,current_figure
         return;
     end
 
-
     select PName
 
         /////////////////////////
     case "foreground"         // <=> Color
         /////////////////////////
-
-        if (type(PropertyValue) == 10)
-
-            index = getColorIndex(PropertyValue);
-
-            ColorVal   = ["red" "green" "blue" "cyan" "magenta" "yellow" "black" "black" "white"];
-
-            if (index < 10)
-                Curves.line_mode="on";
-                Curves.foreground = color(ColorVal(index));
-                Curves.mark_foreground = color(ColorVal(index));
-            else  // 'none' selected
-                warning(msprintf(gettext("%s: Wrong value for input argument #%d: A color of the colormap expected.\n"),"setPlotProperty",2));
-                ResetFigureDDM(current_figure, cur_draw_mode);
-                return;
-            end
-        elseif (type(PropertyValue) == 1) // we entered plot(x,y,'Color',[R,G,B])
-
-            if (size(PropertyValue,"*")==3)
-                Curves.line_mode="on";
-                Curves.foreground = addcolor(PropertyValue);
-                Curves.mark_foreground = addcolor(PropertyValue);
-            else
-                warning(msprintf(gettext("%s: Wrong size for input argument #%d: 3x1 or 1x3 vector expected.\n"),"setPlotProperty",2));
-                ResetFigureDDM(current_figure, cur_draw_mode);
-                return;
-            end
-
-        else
-            warning(msprintf(gettext("%s: Wrong type for input argument #%d: 3 elements vector or index in the colormap expected.\n"),"setPlotProperty",2));
+        c = iscolor(PropertyValue);
+        if or(c(:,1)==-1)
+            msg = _("%s: Argument #%d: Wrong color specification.\n")
+            warning(msprintf(msg, "setPlotProperty", 2));
             ResetFigureDDM(current_figure, cur_draw_mode);
-            return;
+            return
+        end
+        if size(c,"r")<length(Curves)
+            msg = _("%s: Arguments #%d and #%d: Incompatible sizes.\n")
+            warning(msprintf(msg, "setPlotProperty", 2, 3));
+            ResetFigureDDM(current_figure, cur_draw_mode);
+            return
+        end
+        Curves.line_mode = "on";
+        ind = addcolor(c(1:length(Curves),:));
+        for i = 1:length(ind)
+            Curves($-i+1).foreground = ind(i);
+            Curves($-i+1).mark_foreground = ind(i);
         end
-
 
         /////////////////////////
     case "clipping"           // Clipping
diff --git a/scilab/modules/graphics/tests/nonreg_tests/bug_13984.tst b/scilab/modules/graphics/tests/nonreg_tests/bug_13984.tst
new file mode 100644 (file)
index 0000000..d529ddf
--- /dev/null
@@ -0,0 +1,30 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- INTERACTIVE TEST -->
+
+// <-- Non-regression test for bug 13984 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/13984
+//
+// <-- Short Description -->
+// plot(.., "color", colors)
+//  - did not accept a vector of colors for multiple curves
+//  - did accept only 10 named colors instead of the full list of predefined ones
+
+x = (-5:0.2:5)';
+clf
+subplot(1,3,1)
+plot(x,[sin(x) cos(x)])   // Default colors
+
+subplot(1,3,2)
+plot(x,[sin(x) cos(x)], "color", ["orange" "#99BB22"])
+
+subplot(1,3,3)
+plot(x,[sin(x) cos(x)], "color", [ 255 0 0; 0 200 0]/255)
+
diff --git a/scilab/modules/graphics/tests/unit_tests/iscolor.tst b/scilab/modules/graphics/tests/unit_tests/iscolor.tst
new file mode 100644 (file)
index 0000000..cae0944
--- /dev/null
@@ -0,0 +1,70 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- TEST WITH GRAPHIC -->
+// <-- NO CHECK REF -->
+
+// Unitary tests of iscolor()
+
+// Names
+assert_checkequal(iscolor([]), []);
+assert_checkequal(iscolor("red"), [1 0 0]);
+assert_checkequal(iscolor("red", "a"), [1 0 0]);
+assert_checkequal(iscolor("red", "1"), [-1 -1 -1]);
+assert_checkequal(iscolor("red", "."), [-1 -1 -1]);
+assert_checkequal(iscolor("red", "#"), [-1 -1 -1]);
+assert_checkequal(iscolor("orange"), [255 165 0]/255);
+assert_checkequal(iscolor("OrAngE"), [255 165 0]/255);
+assert_checkequal(iscolor("Scilab Blue2"), [0 0 208]/255);
+
+// #RRGGBB
+assert_checkequal(iscolor("#123456"), [18 52 86]/255);
+assert_checkequal(iscolor("#123456"), [18 52 86]/255);
+assert_checkequal(iscolor("#12345G"), [-1 -1 -1]);
+assert_checkequal(iscolor("#abcdef"), [171 205 239]/255);
+assert_checkequal(iscolor("#ABcDeF"), [171 205 239]/255);
+
+// Mixed names + #RRGGBB
+assert_checkequal(iscolor(["#abcdef" "orange"]), [171 205 239 ; 255 165 0]/255);
+
+// Indices
+cmap = gdf().color_map;
+assert_checkequal(iscolor(-1), [-1 -1 -1]);
+assert_checkequal(iscolor(0), [-1 -1 -1]);
+assert_checkequal(iscolor(1.5), [-1 -1 -1]);
+assert_checkequal(iscolor(4), cmap(4,:));
+assert_checkequal(iscolor(4, "a"), [-1 -1 -1]);
+assert_checkequal(iscolor(4, "."), [-1 -1 -1]);
+assert_checkequal(iscolor(4, "#"), [-1 -1 -1]);
+assert_checkequal(iscolor(100), [-1 -1 -1]);
+assert_checkequal(iscolor(1:2), cmap(1:2,:));
+assert_checkequal(iscolor(1:3), [-1 -1 -1]);    // warning
+assert_checkequal(iscolor(1:3, "1"), cmap(1:3,:));
+assert_checkequal(iscolor(1:4), cmap(1:4,:));
+
+f = scf(123);
+cmap = jetcolormap(64);
+f.color_map = cmap;
+assert_checkequal(iscolor(4), cmap(4,:));
+assert_checkequal(iscolor(1:2), cmap(1:2,:));
+assert_checkequal(iscolor(1:3, "1"), cmap(1:3,:));
+assert_checkequal(iscolor(1:4), cmap(1:4,:));
+assert_checkequal(iscolor(65), [-1 -1 -1]);
+delete(f)
+
+// [r g b]
+assert_checkequal(iscolor([-0.1 .2 .3]), [-1 -1 -1]);
+assert_checkequal(iscolor([0.1 .2  3]), [-1 -1 -1]);
+assert_checkequal(iscolor([0.1 .2 .3]), [0.1 0.2 0.3]);
+assert_checkequal(iscolor([0.1 .2 .3], "a"), -ones(3,3));
+assert_checkequal(iscolor([0.1 .2 .3], "#"), -ones(3,3));
+assert_checkequal(iscolor([0.1 .2 .3], "1"), -ones(3,3));
+assert_checkequal(iscolor([0.1 .2 .3], "1."), [.1 .2 .3]);
+assert_checkequal(iscolor([0.1 .2 .3 .4]), -ones(4,3));
+m = rand(10,3);
+assert_checkequal(iscolor(m), m);
+