Debian Codegraph @

cmd/tools/colxlate.c

raw
1 /**
2 * @file
3 * @brief function colorxlate (one of many) for gvcolor.c
4 */
5
6 /*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16 #include "config.h"
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <util/agxbuf.h>
23 #include <util/gv_ctype.h>
24
25 typedef struct {
26 char *name;
27 unsigned char h, s, b;
28 } hsbcolor_t;
29
30 #include "colortbl.h"
31 #include "colorxlate.h"
32
33 static void canoncolor(const char *orig, agxbuf *out) {
34 char c;
35 while ((c = *orig++)) {
36 if (!gv_isalnum(c))
37 continue;
38 agxbputc(out, (char)tolower(c));
39 }
40 }
41
42 static int colorcmpf(const void *a0, const void *a1)
43 {
44 const hsbcolor_t *p1 = a1;
45 return strcmp(a0, p1->name);
46 }
47
48 void colorxlate(char *str, agxbuf *buf) {
49 static hsbcolor_t *last;
50 agxbuf canon_buf = {0};
51 const char *canon = NULL;
52
53 if (last == NULL || strcmp(last->name, str)) {
54 canoncolor(str, &canon_buf);
55 canon = agxbuse(&canon_buf);
56 last = bsearch(canon, color_lib, sizeof(color_lib) / sizeof(hsbcolor_t),
57 sizeof(color_lib[0]), colorcmpf);
58 }
59 if (last == NULL) {
60 if (!gv_isdigit(canon[0])) {
61 fprintf(stderr, "warning: %s is not a known color\n", str);
62 agxbput(buf, str);
63 } else
64 for (const char *p = str; *p != '\0'; ++p)
65 agxbputc(buf, *p == ',' ? ' ' : *p);
66 } else
67 agxbprint(buf, "%.3f %.3f %.3f", ((double) last->h) / 255,
68 ((double) last->s) / 255, ((double) last->b) / 255);
69 agxbfree(&canon_buf);
70 }