From 653b43f80cd2c1deedc58a376f8057c188140c7d Mon Sep 17 00:00:00 2001 From: Paul Wessel Date: Tue, 14 Jun 2022 18:52:57 +0100 Subject: [PATCH] Allow -Ccolor1,color2, etc to also take +h for hinge (#6796) For an odd number of colors we may use the +h modifier when building a CPT from colors. --- src/gmt_api.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/gmt_api.c b/src/gmt_api.c index 778da4284a7..0641da50dfc 100644 --- a/src/gmt_api.c +++ b/src/gmt_api.c @@ -7806,10 +7806,12 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i /* Take comma-separated color entries given in lieu of a file and build a linear, discrete CPT. * This may be converted to a continuous CPT if -Z is used by makecpt/grd2cpt. * We check if a color is valid then write the given entries verbatim to the temp file. + * If a soft hinge has been requested then we must add flag to temp file and pass hinge on via filename. * Returns GMT_NOTSET on error, 0 if no CPT is created (str presumably held a CPT name) and 1 otherwise. */ - unsigned int pos = 0, z = 0; - char *pch = NULL, color[GMT_LEN256] = {""}, tmp_file[GMT_LEN64] = ""; + unsigned int pos = 0; + int z = 0, n; + char *pch = NULL, color[GMT_LEN256] = {""}, tmp_file[GMT_LEN64] = "", *h = NULL; double rgb[4] = {0.0, 0.0, 0.0, 0.0}; FILE *fp = NULL; @@ -7840,6 +7842,22 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i } fprintf (fp, "# COLOR_LIST\n"); /* Flag that we are building a CPT from a list of discrete colors */ + if ((h = strstr (*str, "+h"))) /* See if we got a hinge modifier */ + h[0] = '\0'; /* Hide hinge modifier */ + + n = gmt_count_char (API->GMT, *str, ',') + 1; /* Number of colors */ + if (n && (*str)[strlen(*str)-1] == ',') n--; /* Stray trailing comma with no color ignored */ + if (h) { /* Check if number of colors is odd when a hinge is specified */ + if (n%2) { /* Should be OK */ + fprintf (fp, "# SOFT_HINGE\n"); /* Flag that we are building a CPT with a soft hinge */ + z = -(n - 1)/2; /* Start z value for CPT centered on 0 */ + } + else { + GMT_Report (API, GMT_MSG_ERROR, "Cannot build soft-hinge CPT from an even number of colors\n"); + return (GMT_NOTSET); + } + } + if ((*mode) & GMT_CPT_CONTINUOUS) { /* Make a continuous cpt from the colors */ char last_color[GMT_LEN256] = {""}; if (!gmt_strtok (*str, ",", &pos, last_color)) { /* Get 1st color entry */ @@ -7852,7 +7870,7 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i fclose (fp); return (GMT_NOTSET); } - while (gmt_strtok (*str, ",", &pos, color)) { /* Get color entries */ + while (gmt_strtok (*str, ",", &pos, color) && color[0]) { /* Get color entries */ if (gmt_getrgb (API->GMT, color, rgb)) { GMT_Report (API, GMT_MSG_ERROR, "Badly formatted color entry: %s\n", color); fclose (fp); @@ -7863,14 +7881,14 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i z++; /* Increment z-slice values */ } *mode -= GMT_CPT_CONTINUOUS; /* Served its purpose */ - if (z == 0) { /* Needed at least two colors to specify a ramp */ + if (n == 1) { /* Needed at least two colors to specify a ramp */ GMT_Report (API, GMT_MSG_ERROR, "Cannot make a continuous color ramp from a single color: %s\n", *str); fclose (fp); return (GMT_NOTSET); } } else { - while (gmt_strtok (*str, ",", &pos, color)) { /* Get color entries */ + while (gmt_strtok (*str, ",", &pos, color) && color[0]) { /* Get color entries */ if (gmt_getrgb (API->GMT, color, rgb)) { GMT_Report (API, GMT_MSG_ERROR, "Badly formatted color entry: %s\n", color); fclose (fp); @@ -7884,6 +7902,10 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i GMT_Report (API, GMT_MSG_DEBUG, "Converted %s to CPT %s\n", *str, tmp_file); + if (h) { /* Restore hinge modifier and append it to filename */ + h[0] = '+'; + strcat (tmp_file, h); + } gmt_M_str_free (*str); /* Because it was allocated with strdup */ *str = strdup (tmp_file); /* Pass out the temp file name instead */