Graphviz 14.1.2~dev.20260118.1035
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 "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
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:86
static agxbuf last
last message
Definition agerror.c:31
Dynamically expanding string buffers.
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:97
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:252
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:325
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Definition agxbuf.h:295
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:181
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:218
static char * canon(graph_t *g, char *s, char *buffer)
Definition output.c:103
char * name
Definition colxlate.c:26
unsigned char b
Definition colxlate.c:27
Definition grammar.c:90