Skip to content

Commit

Permalink
Allow -Ccolor1,color2, etc to also take +h for hinge (#6796)
Browse files Browse the repository at this point in the history
For an odd number of colors we may use the +h modifier when building a CPT from colors.
  • Loading branch information
PaulWessel authored Jun 14, 2022
1 parent 21b1b9c commit 653b43f
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/gmt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 */

Expand Down

0 comments on commit 653b43f

Please sign in to comment.