gui/xcore.c
void
hex2rgba(const char *s, double *r, double *g, double *b, double *a)
{
	unsigned int ir, ig, ib, ia;
	double scale;

	if (NULL == s || '\0' == s[0])
		errx(1, "empty color");

	if ('#' == s[0])
		s++;

	switch (strlen(s)) {
	case 3:
		ia = 15;
		scale = 15.0;
		if (3 != sscanf(s, "%01x%01x%01x", &ir, &ig, &ib))
			errx(1, "malformed rgb(3) color '%s'", s);
		break;
	case 4:
		scale = 15.0;
		if (4 != sscanf(s, "%01x%01x%01x%01x", &ir, &ig, &ib, &ia))
			errx(1, "malformed rgb(4) color '%s'", s);
		break;
	case 6:
		ia = 255;
		scale = 255.0;
		if (3 != sscanf(s, "%02x%02x%02x", &ir, &ig, &ib))
			errx(1, "malformed rgb(6) color '%s'", s);
		break;
	case 8:
		scale = 255.0;
		if (4 != sscanf(s, "%02x%02x%02x%02x", &ir, &ig, &ib, &ia))
			errx(1, "malformed rgba(8) color '%s'", s);
		break;
	default:
		errx(1, "malformed color '%s'", s);
	}

	*r = (double)ir / scale;
	*g = (double)ig / scale;
	*b = (double)ib / scale;
	*a = (double)ia / scale;
}
/* color util: translate a color string to r/g/b/a components */
color util: translate a color string to r/g/b/a components
color util : translate a color string to r / g / b / a components
void
hex2rgba(const char *s, double *r, double *g, double *b, double *a)
{
	unsigned int ir, ig, ib, ia;
	double scale;
	if (NULL == s || '\0' == s[0])
		errx(1, "empty color");
	if ('#' == s[0])
		s++;
	switch (strlen(s)) {
	case 3:
		ia = 15;
		scale = 15.0;
		if (3 != sscanf(s, "%01x%01x%01x", &ir, &ig, &ib))
			errx(1, "malformed rgb(3) color '%s'", s);
		break;
	case 4:
		scale = 15.0;
		if (4 != sscanf(s, "%01x%01x%01x%01x", &ir, &ig, &ib, &ia))
			errx(1, "malformed rgb(4) color '%s'", s);
		break;
	case 6:
		ia = 255;
		scale = 255.0;
		if (3 != sscanf(s, "%02x%02x%02x", &ir, &ig, &ib))
			errx(1, "malformed rgb(6) color '%s'", s);
		break;
	case 8:
		scale = 255.0;
		if (4 != sscanf(s, "%02x%02x%02x%02x", &ir, &ig, &ib, &ia))
			errx(1, "malformed rgba(8) color '%s'", s);
		break;
	default:
		errx(1, "malformed color '%s'", s);
	}
	*r = (double)ir / scale;
	*g = (double)ig / scale;
	*b = (double)ib / scale;
	*a = (double)ia / scale;
}
void hex2rgba ( const char * s , double * r , double * g , double * b , double * a ) { unsigned int ir , ig , ib , ia ; double scale ; if ( NULL == s || ' \0 ' == s [ 0 ] ) errx ( 1 , " " ) ; if ( ' ' == s [ 0 ] ) s ++ ; switch ( strlen ( s ) ) { case 3 : ia = 15 ; scale = 15.0 ; if ( 3 != sscanf ( s , " " , & ir , & ig , & ib ) ) errx ( 1 , " " , s ) ; break ; case 4 : scale = 15.0 ; if ( 4 != sscanf ( s , " " , & ir , & ig , & ib , & ia ) ) errx ( 1 , " " , s ) ; break ; case 6 : ia = 255 ; scale = 255.0 ; if ( 3 != sscanf ( s , " " , & ir , & ig , & ib ) ) errx ( 1 , " " , s ) ; break ; case 8 : scale = 255.0 ; if ( 4 != sscanf ( s , " " , & ir , & ig , & ib , & ia ) ) errx ( 1 , " " , s ) ; break ; default : errx ( 1 , " " , s ) ; } * r = ( double ) ir / scale ; * g = ( double ) ig / scale ; * b = ( double ) ib / scale ; * a = ( double ) ia / scale ; }
color util: translate a color string to r/g/b/a components
color util : translate a color string to r / g / b / a components