Skip to content

Commit 653b43f

Browse files
authored
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.
1 parent 21b1b9c commit 653b43f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/gmt_api.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7806,10 +7806,12 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i
78067806
/* Take comma-separated color entries given in lieu of a file and build a linear, discrete CPT.
78077807
* This may be converted to a continuous CPT if -Z is used by makecpt/grd2cpt.
78087808
* We check if a color is valid then write the given entries verbatim to the temp file.
7809+
* If a soft hinge has been requested then we must add flag to temp file and pass hinge on via filename.
78097810
* Returns GMT_NOTSET on error, 0 if no CPT is created (str presumably held a CPT name) and 1 otherwise.
78107811
*/
7811-
unsigned int pos = 0, z = 0;
7812-
char *pch = NULL, color[GMT_LEN256] = {""}, tmp_file[GMT_LEN64] = "";
7812+
unsigned int pos = 0;
7813+
int z = 0, n;
7814+
char *pch = NULL, color[GMT_LEN256] = {""}, tmp_file[GMT_LEN64] = "", *h = NULL;
78137815
double rgb[4] = {0.0, 0.0, 0.0, 0.0};
78147816
FILE *fp = NULL;
78157817

@@ -7840,6 +7842,22 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i
78407842
}
78417843
fprintf (fp, "# COLOR_LIST\n"); /* Flag that we are building a CPT from a list of discrete colors */
78427844

7845+
if ((h = strstr (*str, "+h"))) /* See if we got a hinge modifier */
7846+
h[0] = '\0'; /* Hide hinge modifier */
7847+
7848+
n = gmt_count_char (API->GMT, *str, ',') + 1; /* Number of colors */
7849+
if (n && (*str)[strlen(*str)-1] == ',') n--; /* Stray trailing comma with no color ignored */
7850+
if (h) { /* Check if number of colors is odd when a hinge is specified */
7851+
if (n%2) { /* Should be OK */
7852+
fprintf (fp, "# SOFT_HINGE\n"); /* Flag that we are building a CPT with a soft hinge */
7853+
z = -(n - 1)/2; /* Start z value for CPT centered on 0 */
7854+
}
7855+
else {
7856+
GMT_Report (API, GMT_MSG_ERROR, "Cannot build soft-hinge CPT from an even number of colors\n");
7857+
return (GMT_NOTSET);
7858+
}
7859+
}
7860+
78437861
if ((*mode) & GMT_CPT_CONTINUOUS) { /* Make a continuous cpt from the colors */
78447862
char last_color[GMT_LEN256] = {""};
78457863
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
78527870
fclose (fp);
78537871
return (GMT_NOTSET);
78547872
}
7855-
while (gmt_strtok (*str, ",", &pos, color)) { /* Get color entries */
7873+
while (gmt_strtok (*str, ",", &pos, color) && color[0]) { /* Get color entries */
78567874
if (gmt_getrgb (API->GMT, color, rgb)) {
78577875
GMT_Report (API, GMT_MSG_ERROR, "Badly formatted color entry: %s\n", color);
78587876
fclose (fp);
@@ -7863,14 +7881,14 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i
78637881
z++; /* Increment z-slice values */
78647882
}
78657883
*mode -= GMT_CPT_CONTINUOUS; /* Served its purpose */
7866-
if (z == 0) { /* Needed at least two colors to specify a ramp */
7884+
if (n == 1) { /* Needed at least two colors to specify a ramp */
78677885
GMT_Report (API, GMT_MSG_ERROR, "Cannot make a continuous color ramp from a single color: %s\n", *str);
78687886
fclose (fp);
78697887
return (GMT_NOTSET);
78707888
}
78717889
}
78727890
else {
7873-
while (gmt_strtok (*str, ",", &pos, color)) { /* Get color entries */
7891+
while (gmt_strtok (*str, ",", &pos, color) && color[0]) { /* Get color entries */
78747892
if (gmt_getrgb (API->GMT, color, rgb)) {
78757893
GMT_Report (API, GMT_MSG_ERROR, "Badly formatted color entry: %s\n", color);
78767894
fclose (fp);
@@ -7884,6 +7902,10 @@ GMT_LOCAL int gmtapi_colors2cpt (struct GMTAPI_CTRL *API, char **str, unsigned i
78847902

78857903
GMT_Report (API, GMT_MSG_DEBUG, "Converted %s to CPT %s\n", *str, tmp_file);
78867904

7905+
if (h) { /* Restore hinge modifier and append it to filename */
7906+
h[0] = '+';
7907+
strcat (tmp_file, h);
7908+
}
78877909
gmt_M_str_free (*str); /* Because it was allocated with strdup */
78887910
*str = strdup (tmp_file); /* Pass out the temp file name instead */
78897911

0 commit comments

Comments
 (0)