From ee7a2b9a832206513be28a6afb7bdaba6bd76353 Mon Sep 17 00:00:00 2001 From: Juergen KOCH Date: Thu, 11 Jun 2015 16:38:00 +0200 Subject: [PATCH] Manage vectors of colors in name2rgb. Change-Id: Ia395ff9b35c01c0cdc111d80d39f90e9110078d3 --- scilab/CHANGES_5.6.X | 2 + .../help/en_US/color_management/name2rgb.xml | 17 +++++- .../modules/graphics/sci_gateway/c/sci_name2rgb.c | 58 ++++++++++++++++---- .../graphics/tests/unit_tests/name2rgb.dia.ref | 14 +++++ .../modules/graphics/tests/unit_tests/name2rgb.tst | 15 +++++ 5 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 scilab/modules/graphics/tests/unit_tests/name2rgb.dia.ref create mode 100644 scilab/modules/graphics/tests/unit_tests/name2rgb.tst diff --git a/scilab/CHANGES_5.6.X b/scilab/CHANGES_5.6.X index c331d1c..9b3e671 100644 --- a/scilab/CHANGES_5.6.X +++ b/scilab/CHANGES_5.6.X @@ -7,6 +7,8 @@ New Features * scatter plot with different mark colors is now available: TODO : save/load+display+doc of new properties +* name2rgb can now handle a single string and a matrix of strings. + Compilation ============ diff --git a/scilab/modules/graphics/help/en_US/color_management/name2rgb.xml b/scilab/modules/graphics/help/en_US/color_management/name2rgb.xml index 247f128..d0cf3f6 100644 --- a/scilab/modules/graphics/help/en_US/color_management/name2rgb.xml +++ b/scilab/modules/graphics/help/en_US/color_management/name2rgb.xml @@ -25,13 +25,13 @@ name - name of the color. + name of the color or matrix with names of the colors. rgb - vector of RGB integer values of a color. + vector or matrix with 3 columns of RGB integer values of the colors. @@ -40,7 +40,7 @@ Description name2rgb returns the RGB values of a color given by its name. The result is - a vector [r,g,b] where r, g and b are integers between 0 and 255 + a vector or a matrix with 3 columns [r,g,b] where r, g and b are integers between 0 and 255 corresponding to colors components red, green and blue. As usual 0 means no intensity and 255 means all the intensity of the color. @@ -72,4 +72,15 @@ rgb2name(rgb) + + History + + + 6.0 + + Function name2rgb can now handle a single string and a matrix of strings. + + + + diff --git a/scilab/modules/graphics/sci_gateway/c/sci_name2rgb.c b/scilab/modules/graphics/sci_gateway/c/sci_name2rgb.c index d1002d6..1577323 100644 --- a/scilab/modules/graphics/sci_gateway/c/sci_name2rgb.c +++ b/scilab/modules/graphics/sci_gateway/c/sci_name2rgb.c @@ -9,6 +9,7 @@ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt * */ +#include "MALLOC.h" #include "gw_graphics.h" #include "api_scilab.h" #include "localization.h" @@ -23,6 +24,13 @@ int sci_name2rgb(char *fname, unsigned long fname_len) int* piAddr = NULL; char* pstColor = NULL; double color[3]; + int i; + int nRows; + int nCols; + int nRowsCols; + char** pstColorMatrix = NULL; + double* pColor = NULL; + CheckInputArgument(pvApiCtx, 1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); @@ -32,26 +40,56 @@ int sci_name2rgb(char *fname, unsigned long fname_len) return 1; } - if (isStringType(pvApiCtx, piAddr) == FALSE || isScalar(pvApiCtx, piAddr) == FALSE) + if (isStringType(pvApiCtx, piAddr) == FALSE) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); return 1; } - getAllocatedSingleString(pvApiCtx, piAddr, &pstColor); - name2rgb(pstColor, color); - freeAllocatedSingleString(pstColor); - - if (color[0] == -1 || color[1] == -1 || color[2] == -1) + if (isScalar(pvApiCtx, piAddr) == TRUE) { - createEmptyMatrix(pvApiCtx, 2); + getAllocatedSingleString(pvApiCtx, piAddr, &pstColor); + name2rgb(pstColor, color); + freeAllocatedSingleString(pstColor); + + if (color[0] == -1 || color[1] == -1 || color[2] == -1) + { + createEmptyMatrix(pvApiCtx, 2); + } + else + { + createMatrixOfDouble(pvApiCtx, 2, 1, 3, color); + } } else { - createMatrixOfDouble(pvApiCtx, 2, 1, 3, color); + getAllocatedMatrixOfString(pvApiCtx, piAddr, &nRows, &nCols, &pstColorMatrix); + nRowsCols = nRows * nCols; + pColor = (double*)MALLOC(3 * nRowsCols * sizeof(double)); + for (i = 0; i < nRowsCols; ++i) + { + name2rgb(pstColorMatrix[i], color); + if (color[0] == -1 || color[1] == -1 || color[2] == -1) + { + break; + } + pColor[i] = color[0]; + pColor[i + nRowsCols] = color[1]; + pColor[i + 2 * nRowsCols] = color[2]; + } + freeAllocatedMatrixOfString(nRows, nCols, pstColorMatrix); + if (color[0] == -1 || color[1] == -1 || color[2] == -1) + { + createEmptyMatrix(pvApiCtx, 2); + FREE(pColor); + } + else + { + createMatrixOfDouble(pvApiCtx, 2, nRowsCols, 3, pColor); + FREE(pColor); + } } - AssignOutputVariable(pvApiCtx, 1) = 2; ReturnArguments(pvApiCtx); return 0; -} \ No newline at end of file +} diff --git a/scilab/modules/graphics/tests/unit_tests/name2rgb.dia.ref b/scilab/modules/graphics/tests/unit_tests/name2rgb.dia.ref new file mode 100644 index 0000000..cac8305 --- /dev/null +++ b/scilab/modules/graphics/tests/unit_tests/name2rgb.dia.ref @@ -0,0 +1,14 @@ +// ============================================================================= +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2015 - Scilab Enterprises - Juergen Koch +// +// This file is distributed under the same license as the Scilab package. +// ============================================================================= +// +// <-- TEST WITH GRAPHIC --> +assert_checkequal(name2rgb("red"), [255 0 0]); +assert_checkequal(name2rgb("nocolor"), []); +assert_checkequal(name2rgb(["red" "green"]), [255 0 0;0 255 0]); +assert_checkequal(name2rgb(["red";"green"]), [255 0 0;0 255 0]); +assert_checkequal(name2rgb(["red" "green";"blue" "black"]), [255 0 0;0 0 255;0 255 0;0 0 0]); +assert_checkequal(name2rgb(["red" "green";"blue" "nocolor"]), []); diff --git a/scilab/modules/graphics/tests/unit_tests/name2rgb.tst b/scilab/modules/graphics/tests/unit_tests/name2rgb.tst new file mode 100644 index 0000000..c89d2d9 --- /dev/null +++ b/scilab/modules/graphics/tests/unit_tests/name2rgb.tst @@ -0,0 +1,15 @@ +// ============================================================================= +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2015 - Scilab Enterprises - Juergen Koch +// +// This file is distributed under the same license as the Scilab package. +// ============================================================================= +// +// <-- TEST WITH GRAPHIC --> + +assert_checkequal(name2rgb("red"), [255 0 0]); +assert_checkequal(name2rgb("nocolor"), []); +assert_checkequal(name2rgb(["red" "green"]), [255 0 0;0 255 0]); +assert_checkequal(name2rgb(["red";"green"]), [255 0 0;0 255 0]); +assert_checkequal(name2rgb(["red" "green";"blue" "black"]), [255 0 0;0 0 255;0 255 0;0 0 0]); +assert_checkequal(name2rgb(["red" "green";"blue" "nocolor"]), []); -- 1.7.9.5