32 static char* colorscheme;
36 extern int strncasecmp(
const char *
s1,
const char *s2,
unsigned int n);
40 static void hsv2rgb(
double h,
double s,
double v,
41 double *r,
double *g,
double *b)
57 q = v * (1 - (s * f));
58 t = v * (1 - (s * (1 - f)));
94 static void rgb2hsv(
double r,
double g,
double b,
95 double *h,
double *s,
double *v)
98 double rgbmin, rgbmax;
100 double ht = 0.0, st = 0.0;
102 rgbmin =
MIN(r,
MIN(g, b));
103 rgbmax =
MAX(r,
MAX(g, b));
106 st = (rgbmax - rgbmin) / rgbmax;
109 rc = (rgbmax - r) / (rgbmax - rgbmin);
110 gc = (rgbmax - g) / (rgbmax - rgbmin);
111 bc = (rgbmax - b) / (rgbmax - rgbmin);
114 else if (g == rgbmax)
116 else if (b == rgbmax)
127 static void rgb2cmyk(
double r,
double g,
double b,
double *c,
double *m,
128 double *y,
double *k)
133 *k = *c < *m ? *c : *m;
134 *k = *y < *k ? *y : *k;
140 static int colorcmpf(
const void *p0,
const void *p1)
147 static unsigned char *canon;
148 static size_t allocated;
149 unsigned char c, *p, *q;
152 p = (
unsigned char *) str;
154 if (len >= allocated) {
155 allocated = len + 1 + 10;
160 q = (
unsigned char *) canon;
165 c = (
unsigned char) tolower(c);
175 static char* fullColor (
char* prefix,
char*
str)
178 static size_t allocated;
179 size_t len = strlen(prefix) + strlen(str) + 3;
181 if (len >= allocated) {
182 allocated = len + 10;
185 sprintf (fulls,
"/%s/%s", prefix, str);
221 #define DFLT_SCHEME "X11/"
222 #define DFLT_SCHEME_LEN ((sizeof(DFLT_SCHEME)-1)/sizeof(char))
223 #define ISNONDFLT(s) ((s) && *(s) && strncasecmp(DFLT_SCHEME, s, DFLT_SCHEME_LEN-1))
225 static char* resolveColor (
char* str)
231 if (!strcmp(str,
"black"))
return str;
232 if (!strcmp(str,
"white"))
return str;
233 if (!strcmp(str,
"lightgrey"))
return str;
236 if ((ss = strchr(c2,
'/'))) {
240 s = fullColor (colorscheme, c2+1);
249 else if (
ISNONDFLT(colorscheme)) s = fullColor (colorscheme, str);
257 static unsigned char *canon;
258 static size_t allocated;
259 unsigned char *p, *q;
262 double H, S, V, A, R, G, B;
264 unsigned int r, g, b, a;
268 color->
type = target_type;
271 for (; *str ==
' '; str++);
272 p = (
unsigned char *) str;
278 && (sscanf((
char *) p,
"#%2x%2x%2x%2x", &r, &g, &b, &a) >= 3)) {
279 switch (target_type) {
281 R = (double) r / 255.0;
282 G = (double) g / 255.0;
283 B = (double) b / 255.0;
284 A = (double) a / 255.0;
285 rgb2hsv(R, G, B, &H, &S, &V);
286 color->
u.
HSVA[0] = H;
287 color->
u.
HSVA[1] = S;
288 color->
u.
HSVA[2] = V;
289 color->
u.
HSVA[3] = A;
292 color->
u.
rgba[0] = r;
293 color->
u.
rgba[1] = g;
294 color->
u.
rgba[2] = b;
295 color->
u.
rgba[3] = a;
298 R = (double) r / 255.0;
299 G = (double) g / 255.0;
300 B = (double) b / 255.0;
301 rgb2cmyk(R, G, B, &C, &M, &Y, &K);
314 color->
u.
RGBA[0] = (double) r / 255.0;
315 color->
u.
RGBA[1] = (double) g / 255.0;
316 color->
u.
RGBA[2] = (double) b / 255.0;
317 color->
u.
RGBA[3] = (double) a / 255.0;
328 if (((c = *p) ==
'.') || isdigit(c)) {
329 len = strlen((
char*)p);
330 if (len >= allocated) {
331 allocated = len + 1 + 10;
346 if (sscanf((
char *) canon,
"%lf%lf%lf", &H, &S, &V) == 3) {
348 H =
MAX(
MIN(H, 1.0), 0.0);
349 S =
MAX(
MIN(S, 1.0), 0.0);
350 V =
MAX(
MIN(V, 1.0), 0.0);
351 switch (target_type) {
353 color->
u.
HSVA[0] = H;
354 color->
u.
HSVA[1] = S;
355 color->
u.
HSVA[2] = V;
356 color->
u.
HSVA[3] = 1.0;
359 hsv2rgb(H, S, V, &R, &G, &B);
363 color->
u.
rgba[3] = 255;
366 hsv2rgb(H, S, V, &R, &G, &B);
367 rgb2cmyk(R, G, B, &C, &M, &Y, &K);
374 hsv2rgb(H, S, V, &R, &G, &B);
381 hsv2rgb(H, S, V, &R, &G, &B);
382 color->
u.
RGBA[0] = R;
383 color->
u.
RGBA[1] = G;
384 color->
u.
RGBA[2] = B;
385 color->
u.
RGBA[3] = 1.0;
397 fake.
name = resolveColor(str);
402 || (strcmp(last->
name, fake.
name))) {
410 switch (target_type) {
412 color->
u.
HSVA[0] = ((double) last->
h) / 255.0;
413 color->
u.
HSVA[1] = ((double) last->
s) / 255.0;
414 color->
u.
HSVA[2] = ((double) last->
v) / 255.0;
415 color->
u.
HSVA[3] = ((double) last->
a) / 255.0;
418 color->
u.
rgba[0] = last->
r;
419 color->
u.
rgba[1] = last->
g;
420 color->
u.
rgba[2] = last->
b;
421 color->
u.
rgba[3] = last->
a;
424 R = (last->
r) / 255.0;
425 G = (last->
g) / 255.0;
426 B = (last->
b) / 255.0;
427 rgb2cmyk(R, G, B, &C, &M, &Y, &K);
440 color->
u.
RGBA[0] = last->
r / 255.0;
441 color->
u.
RGBA[1] = last->
g / 255.0;
442 color->
u.
RGBA[2] = last->
b / 255.0;
443 color->
u.
RGBA[3] = last->
a / 255.0;
455 switch (target_type) {
458 color->
u.
HSVA[3] = 1.0;
462 color->
u.
rgba[3] = 255;
474 color->
u.
RGBA[3] = 1.0;
484 static void rgba_wordToByte (
int* rrggbbaa,
unsigned char* rgba)
488 for (i = 0; i < 4; i++) {
489 rgba[i] = rrggbbaa[i] * 255 / 65535;
493 static void rgba_dblToByte (
double* RGBA,
unsigned char* rgba)
497 for (i = 0; i < 4; i++) {
498 rgba[i] = (
unsigned char)(RGBA[i] * 255);
514 unsigned char rgba[4];
517 memcpy (&ncolor->
u, &ocolor->
u,
sizeof(ocolor->
u));
521 switch (ocolor->
type) {
523 sprintf (buf,
"%.03f %.03f %.03f %.03f",
527 sprintf (buf,
"#%02x%02x%02x%02x",
531 rgba_wordToByte (ocolor->
u.
rrggbbaa, rgba);
532 sprintf (buf,
"#%02x%02x%02x%02x", rgba[0], rgba[1], rgba[2], rgba[3]);
535 rgba_dblToByte (ocolor->
u.
RGBA, rgba);
536 sprintf (buf,
"#%02x%02x%02x%02x", rgba[0], rgba[1], rgba[2], rgba[3]);
void s1(graph_t *, node_t *)
void * grealloc(void *ptr, size_t size)
#define COLOR_MALLOC_FAIL
int strncasecmp(const char *s1, const char *s2, unsigned int n)
int colorxlate(char *str, gvcolor_t *color, color_type_t target_type)
char * canontoken(char *str)
int colorCvt(gvcolor_t *ocolor, gvcolor_t *ncolor)
int strcasecmp(const char *s1, const char *s2)
void setColorScheme(char *s)
struct hsvrgbacolor_t hsvrgbacolor_t