Example #1
0
static char* resolveColor (char* str)
{
    char* s;
    char* ss;   /* second slash */
    char* c2;   /* second char */

    if ((*str == 'b') || !strncmp(str+1,"lack",4)) return str;
    if ((*str == 'w') || !strncmp(str+1,"hite",4)) return str;
    if ((*str == 'l') || !strncmp(str+1,"ightgrey",8)) return str;
    if (*str == '/') {   /* if begins with '/' */
	c2 = str+1;
        if ((ss = strchr(c2, '/'))) {  /* if has second '/' */
	    if (*c2 == '/') {    /* if second '/' is second character */
		    /* Do not compare against final '/' */
		if (ISNONDFLT(colorscheme))
		    s = fullColor (colorscheme, c2+1);
		else
		    s = c2+1;
	    }
	    else if (strncasecmp(DFLT_SCHEME, c2, DFLT_SCHEME_LEN)) s = str;
	    else s = ss + 1;
	}
	else s = c2;
    }
    else if (ISNONDFLT(colorscheme)) s = fullColor (colorscheme, str);
    else s = str;
    return canontoken(s);
}
Example #2
0
static void mif_set_color(char *name)
{
    int i;
    char *tok;

    static char *mifcolor[] = {
	"black", "white", "red", "green", "blue", "cyan",
	"magenta", "yellow", "comment",
	"aquamarine", "plum", "peru", "pink", "mediumpurple", "grey",
	"lightgrey", "lightskyblue", "lightcoral", "yellowgreen",
	(char *) 0
    };

    tok = canontoken(name);
    for (i = 0; mifcolor[i]; i++) {
	if (strcasecmp(mifcolor[i], tok) == 0) {
	    cstk[SP].color_ix = i;
	    mif_color(i);
	    return;
	}
    }
    agerr(AGERR, "color %s not supported in MIF\n", name);
}
Example #3
0
void colorxlate(char *str, color_t *color, color_type target_type)
{
	static hsbcolor_t	*last;
	hsbcolor_t	fake;
	char		canon[SMALLBUF];
	char		*p, *q, ch;
	double		H,S,V,R,G,B;
	double		C,M,Y,K;
	int		r,g,b,a;
	int		i;

	color->type = target_type;

	/* skip over any leading whitespace */
	for (; *str == ' '; str++);
    p = str;

	/* test for rgb value such as: "#ff0000" or rgba value such as "#ff000080" */
	a = 255; /* default alpha channel value in case not supplied */
	if ((*p == '#') && (sscanf(p,"#%2x%2x%2x%2x",&r,&g,&b,&a) >= 3)) {
		switch (target_type) {
		case HSV_DOUBLE:
			R = (double)r/255.0;
			G = (double)g/255.0;
			B = (double)b/255.0;
			rgb2hsv(R,G,B,&H,&S,&V);
			color->u.HSV[0] = H;
			color->u.HSV[1] = S;
			color->u.HSV[2] = V;
			break;
		case RGBA_BYTE:
			color->u.rgba[0] = r;
			color->u.rgba[1] = g;
			color->u.rgba[2] = b;
			color->u.rgba[3] = a;
			break;
		case CMYK_BYTE:
			R = (double)r/255.0;
			G = (double)g/255.0;
			B = (double)b/255.0;
			rgb2cmyk(R,G,B,&C,&M,&Y,&K);
			color->u.cmyk[0] = (int)C*255;
			color->u.cmyk[1] = (int)M*255;
			color->u.cmyk[2] = (int)Y*255;
			color->u.cmyk[3] = (int)K*255;
			break;
		case RGBA_WORD:
			color->u.rrggbbaa[0] = r*65535/255;
			color->u.rrggbbaa[1] = g*65535/255;
			color->u.rrggbbaa[2] = b*65535/255;
			color->u.rrggbbaa[3] = a*65535/255;
			break;
		}
		/* color->type = target_type; */
		return; 
	}

	/* test for hsv value such as: ".6,.5,.3" */
    if (((ch = *p) == '.') || isdigit(ch)) {
	    q = canon;
	    i = SMALLBUF;
	    while ((ch = *p++) && (--i)) {
            if (ch == ',') ch = ' ';
	        *q++ = ch;
	    }
        if (ch) agerr (AGWARN, "color value '%s' truncated\n", str);
	    *q = '\0';
    
	    if (sscanf(canon,"%lf%lf%lf",&H,&S,&V) == 3) {
		    /* clip to reasonable values */
		    H = MAX(MIN(H,1.0),0.0);
		    S = MAX(MIN(S,1.0),0.0);
		    V = MAX(MIN(V,1.0),0.0);
		    switch (target_type) {
		    case HSV_DOUBLE:
			    color->u.HSV[0] = H;
			    color->u.HSV[1] = S;
			    color->u.HSV[2] = V;
			    break;
		    case RGBA_BYTE:
			    hsv2rgb(H,S,V,&R,&G,&B);
			    color->u.rgba[0] = (int)(R*255);
			    color->u.rgba[1] = (int)(G*255);
			    color->u.rgba[2] = (int)(B*255);
			    color->u.rgba[3] = 255;
			    break;
		    case CMYK_BYTE:
			    hsv2rgb(H,S,V,&R,&G,&B);
			    rgb2cmyk(R,G,B,&C,&M,&Y,&K);
			    color->u.cmyk[0] = (int)C*255;
			    color->u.cmyk[1] = (int)M*255;
			    color->u.cmyk[2] = (int)Y*255;
			    color->u.cmyk[3] = (int)K*255;
			    break;
		    case RGBA_WORD:
			    hsv2rgb(H,S,V,&R,&G,&B);
			    color->u.rrggbbaa[0] = (int)(R*65535);
			    color->u.rrggbbaa[1] = (int)(G*65535);
			    color->u.rrggbbaa[2] = (int)(B*65535);
			    color->u.rrggbbaa[3] = 65535;
			    break;
		    }
		/* color->type = target_type; */
		    return;
	    }
	}

	/* test for known color name */
	fake.name = canontoken(str);
	if ((last == NULL)
	  ||(last->name[0] != fake.name[0])
	  ||(strcmp(last->name,fake.name))) {
		last = (hsbcolor_t*) bsearch((void*)&fake,
				(void*)color_lib,
				sizeof(color_lib)/sizeof(hsbcolor_t),
				sizeof(fake),(bsearch_cmpf)colorcmpf);
	}
	if (last != NULL) {
		switch (target_type) {
		case HSV_DOUBLE:
			color->u.HSV[0] = ((double)last->h)/255.0;
			color->u.HSV[1] = ((double)last->s)/255.0;
			color->u.HSV[2] = ((double)last->b)/255.0;
			break;
		case RGBA_BYTE:
			H = (last->h)/255.0;
			S = (last->s)/255.0;
			V = (last->b)/255.0;
			hsv2rgb(H,S,V,&R,&G,&B);
			color->u.rgba[0] = (int)(R*255);
			color->u.rgba[1] = (int)(G*255);
			color->u.rgba[2] = (int)(B*255);
			color->u.rgba[3] = 255;
			break;
		case CMYK_BYTE:
			H = (last->h)/255.0;
			S = (last->s)/255.0;
			V = (last->b)/255.0;
			hsv2rgb(H,S,V,&R,&G,&B);
			rgb2cmyk(R,G,B,&C,&M,&Y,&K);
			color->u.cmyk[0] = (int)C*255;
			color->u.cmyk[1] = (int)M*255;
			color->u.cmyk[2] = (int)Y*255;
			color->u.cmyk[3] = (int)K*255;
			break;
		case RGBA_WORD:
			H = (last->h)/255.0;
			S = (last->s)/255.0;
			V = (last->b)/255.0;
			hsv2rgb(H,S,V,&R,&G,&B);
			color->u.rrggbbaa[0] = (int)(R*65535);
			color->u.rrggbbaa[1] = (int)(G*65535);
			color->u.rrggbbaa[2] = (int)(B*65535);
			color->u.rrggbbaa[3] = 65535;
			break;
		}
		/* color->type = target_type; */
		return;
	}

	/* if we're still here then we failed to find a valid color spec */
	agerr (AGWARN, "%s is not a known color. Using black.\n",str);
	switch (target_type) {
	case HSV_DOUBLE:
		color->u.HSV[0] =
		       	color->u.HSV[1] =
		       	color->u.HSV[2] = 0.0;
		break;
	case RGBA_BYTE:
		color->u.rgba[0] =
		       	color->u.rgba[1] =
		       	color->u.rgba[2] = 0;
		color->u.rgba[3] = 255;
		break;
	case CMYK_BYTE:
		color->u.cmyk[0] = 
			color->u.cmyk[1] = 
			color->u.cmyk[2] = 
			color->u.cmyk[3] = 0;
		break;
	case RGBA_WORD:
		color->u.rrggbbaa[0] =
		       	color->u.rrggbbaa[1] =
		       	color->u.rrggbbaa[2] = 0;
		color->u.rrggbbaa[3] = 65535;
		break;
	}
	/* color->type = target_type; */
	return;
}