FREE(dists);
FREE(lab);
}
-
-
-
-
-
-color_rgb color_blend_rgb(char *color_list, real ratio, int *flag){
- /* give a color list of the form "#ff0000,#00ff00,...", get a blend at ratio*100 percent of the position in that list and return in string color0 of the form #abcdef
- If *colors0 is NULL, it will be allocated.
- color_list: either "#ff0000,#00ff00,...", or "pastel"
- */
-
- int nc = 1, r, g, b, i, ii;
- char *cl;
- color_lab *lab = NULL;
- color_rgb rgb;
- double *dists = NULL;
- char *cp;
- color_lab clab;
- double color[3];
-
- *flag = 0;
-
- ratio = MAX(ratio, 0);
- ratio = MIN(ratio, 1);
-
- cp = color_palettes_get(color_list);
- if (cp){
- color_list = cp;
- }
-
- cl = color_list;
- while ((cl=strstr(cl, ",")) != NULL){
- cl++; nc++;
- }
-
- lab = malloc(sizeof(color_lab)*MAX(nc,1));
-
- cl = color_list - 1;
- nc = 0;
- do {
- cl++;
- if (sscanf(cl,"#%02X%02X%02X", &r, &g, &b) != 3) break;
- rgb.r = r; rgb.g = g; rgb.b = b;
- lab[nc++] = RGB2LAB(rgb);
- } while ((cl=strstr(cl, ",")) != NULL);
-
- if (nc == 1 || ratio == 0){
- rgb = LAB2RGB(lab[0]);
- goto RETURN;
- } else if (nc == 0){
- fprintf(stderr, "no color\n");
- *flag = -1;
- goto RETURN;
- }
-
- dists = malloc(sizeof(double)*MAX(1, nc));
- dists[0] = 0;
- for (i = 0; i < nc - 1; i++){
- dists[i+1] = lab_dist(lab[i], lab[i+1]);
- }
- /* dists[i] is now the summed color distance from the 0-th color to the i-th color */
- for (i = 0; i < nc - 1; i++){
- dists[i+1] += dists[i];
- }
- if (dists[nc-1] == 0){/* same color in the list */
- rgb = LAB2RGB(lab[0]);
- goto RETURN;
- }
- for (i = 0; i < nc; i++){
- dists[i] /= dists[nc - 1];
- }
-
- ii = 0;
- while (dists[ii] < ratio) ii++;
-
- assert(ii < nc && ii > 0);
-
- lab_interpolate(lab[ii - 1], lab[ii], (ratio - dists[ii - 1])/MAX(0.001, (dists[ii] - dists[ii - 1])), color);
- clab = color_lab_init(color[0], color[1], color[2]);
- rgb = LAB2RGB(clab);
-
-
-
- RETURN:
- if (dists) FREE(dists);
- if (lab) FREE(lab);
- return rgb;
-}
void color_blend_rgb2lab(char *color_list, const int maxpoints, double **colors); /* give a color list of the form "#ff0000,#00ff00,...", get a list of around maxpoints
colors in an array colors0 of size [maxpoints*3] of the form {{l,a,b},...}.
If *colors0 is NULL, it will be allocated. */
-
-color_rgb color_blend_rgb(char *color_list, const real ratio, int *flag);/* blend a color list to get one color at ratio*100 percent of the list */
#endif