Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
colxlate.c
Go to the documentation of this file.
1
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 <cgraph/agxbuf.h>
17#include <cgraph/gv_ctype.h>
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#ifdef HAVE_SEARCH_H
22#include <search.h>
23#endif
24#include <ctype.h>
25typedef struct {
26 char *name;
27 unsigned char h, s, b;
29
30#include "colortbl.h"
31#include "colorxlate.h"
32
33static 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
42static int colorcmpf(const void *a0, const void *a1)
43{
44 const hsbcolor_t *p1 = a1;
45 return strcmp(a0, p1->name);
46}
47
48void 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}
static void out(agerrlevel_t level, const char *fmt, va_list args)
Report messages using a user-supplied or default write function.
Definition agerror.c:84
static agxbuf last
last message
Definition agerror.c:29
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:77
static size_t agxbput(agxbuf *xb, const char *s)
append string s into xb
Definition agxbuf.h:249
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:213
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Definition agxbuf.h:256
static char * agxbuse(agxbuf *xb)
Definition agxbuf.h:286
hsbcolor_t color_lib[]
Definition colortbl.h:11
static int colorcmpf(const void *a0, const void *a1)
Definition colxlate.c:42
void colorxlate(char *str, agxbuf *buf)
Definition colxlate.c:48
static void canoncolor(const char *orig, agxbuf *out)
Definition colxlate.c:33
node NULL
Definition grammar.y:149
replacements for ctype.h functions
static bool gv_isalnum(int c)
Definition gv_ctype.h:43
static bool gv_isdigit(int c)
Definition gv_ctype.h:41
agxbuf * str
Definition htmlparse.c:97
static char * canon(graph_t *g, char *s)
Definition output.c:94
struct agxbuf::@59::@60 s
char * name
Definition colxlate.c:26
unsigned char b
Definition colxlate.c:27
Definition grammar.c:93