Graphviz 13.0.0~dev.20250121.0651
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 <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include <util/agxbuf.h>
21#include <util/gv_ctype.h>
22
23typedef struct {
24 char *name;
25 unsigned char h, s, b;
27
28#include "colortbl.h"
29#include "colorxlate.h"
30
31static void canoncolor(const char *orig, agxbuf *out) {
32 char c;
33 while ((c = *orig++)) {
34 if (!gv_isalnum(c))
35 continue;
36 agxbputc(out, (char)tolower(c));
37 }
38}
39
40static int colorcmpf(const void *a0, const void *a1)
41{
42 const hsbcolor_t *p1 = a1;
43 return strcmp(a0, p1->name);
44}
45
46void colorxlate(char *str, agxbuf *buf) {
47 static hsbcolor_t *last;
48 agxbuf canon_buf = {0};
49 const char *canon = NULL;
50
51 if (last == NULL || strcmp(last->name, str)) {
52 canoncolor(str, &canon_buf);
53 canon = agxbuse(&canon_buf);
54 last = bsearch(canon, color_lib, sizeof(color_lib) / sizeof(hsbcolor_t),
55 sizeof(color_lib[0]), colorcmpf);
56 }
57 if (last == NULL) {
58 if (!gv_isdigit(canon[0])) {
59 fprintf(stderr, "warning: %s is not a known color\n", str);
60 agxbput(buf, str);
61 } else
62 for (const char *p = str; *p != '\0'; ++p)
63 agxbputc(buf, *p == ',' ? ' ' : *p);
64 } else
65 agxbprint(buf, "%.3f %.3f %.3f", ((double) last->h) / 255,
66 ((double) last->s) / 255, ((double) last->b) / 255);
67 agxbfree(&canon_buf);
68}
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:78
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:307
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Definition agxbuf.h:277
hsbcolor_t color_lib[]
Definition colortbl.h:11
static int colorcmpf(const void *a0, const void *a1)
Definition colxlate.c:40
void colorxlate(char *str, agxbuf *buf)
Definition colxlate.c:46
static void canoncolor(const char *orig, agxbuf *out)
Definition colxlate.c:31
node NULL
Definition grammar.y:163
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
agxbput(xb, staging)
textitem scanner parser str
Definition htmlparse.y:224
static char * canon(graph_t *g, char *s)
Definition output.c:97
struct agxbuf::@121::@122 s
char * name
Definition colxlate.c:24
unsigned char b
Definition colxlate.c:25
Definition grammar.c:93