1/* $Id$ $Revision$ */
2/* vim:set shiftwidth=4 ts=8: */
3
4/*************************************************************************
5 * Copyright (c) 2011 AT&T Intellectual Property
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors: See CVS logs. Details at http://www.graphviz.org/
12 *************************************************************************/
13
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#ifdef HAVE_SEARCH_H
18#include <search.h>
19#endif
20#include <ctype.h>
21#ifndef FALSE
22#define FALSE (0)
23#endif
24#ifndef TRUE
25#define TRUE (!FALSE)
26#endif
27#ifndef NOT
28#define NOT(x) (!(x))
29#endif
30#ifndef NIL
31#define NIL(type) ((type)0)
32#endif
33typedef struct hsbcolor_t {
34 char *name;
35 unsigned char h, s, b;
36} hsbcolor_t;
37
38
39#ifndef NOCOLORNAMES
40#include "colortbl.h"
41
42/*
43 char *bsearch ((char *) key, (char *) base, nel, sizeof (*key), compar)
44 unsigned nel;
45 int (*compar)( );
46*/
47
48static unsigned char *canoncolor(char *orig, unsigned char *out)
49{
50 unsigned char c;
51 unsigned char *p = out;
52 while ((c = *(unsigned char *) orig++)) {
53 if (isalnum(c) == FALSE)
54 continue;
55 if (isupper(c))
56 c = tolower(c);
57 *p++ = c;
58 }
59 *p = '\0';
60 return out;
61}
62
63static int colorcmpf(const void *a0, const void *a1)
64{
65 hsbcolor_t *p0 = (hsbcolor_t *) a0;
66 hsbcolor_t *p1 = (hsbcolor_t *) a1;
67 int i = (p0->name[0] - p1->name[0]);
68 return (i ? i : strcmp(p0->name, p1->name));
69}
70
71char *colorxlate(char *str, char *buf)
72{
73 static hsbcolor_t *last;
74 unsigned char canon[128];
75 char *p;
76 hsbcolor_t fake;
77
78 if ((last == NULL) || (last->name[0] != str[0])
79 || (strcmp(last->name, str))) {
80 fake.name = (char *) canoncolor(str, canon);
81 last =
82 (hsbcolor_t *) bsearch(&fake, color_lib,
83 sizeof(color_lib) / sizeof(hsbcolor_t),
84 sizeof(fake), colorcmpf);
85 }
86 if (last == NULL) {
87 if (isdigit(canon[0]) == FALSE) {
88 fprintf(stderr, "warning: %s is not a known color\n", str);
89 strcpy(buf, str);
90 } else
91 for (p = buf; (*p = *str++); p++)
92 if (*p == ',')
93 *p = ' ';
94 } else
95 sprintf(buf, "%.3f %.3f %.3f", ((double) last->h) / 255,
96 ((double) last->s) / 255, ((double) last->b) / 255);
97 return buf;
98}
99#else
100char *colorxlate(char *str, char *buf)
101{
102 return str;
103}
104#endif
105