INKPOT_FAIL
} inkpot_status_t;
+/* opaque inkpot struct */
typedef struct inkpot_s inkpot_t;
+/* template for writer functions */
size_t (*writer) (void *closure, const char *data, size_t length);
+/* malloc an inkpot and perform some initialization */
extern inkpot_t *inkpot_init ( void );
-extern inkpot_status_t inkpot_writer_fn ( inkpot_t *inkpot, void *writer, void *out_closure, void *err_closure );
-extern inkpot_status_t inkpot_clear ( inkpot_t *inkpot );
-extern inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme );
-extern inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme );
+/* Provide alternate out and err writer functions */
+/* Defaults to internal functions using fwrite() to stdout and stderr */
+/* No need to call these funtions if the defaults suffice. */
+extern inkpot_status_t inkpot_out_writer ( inkpot_t *inkpot, void *writer, void *closure);
+extern inkpot_status_t inkpot_err_writer ( inkpot_t *inkpot, void *writer, void *closure);
+/* The list of schemes for color input interpretation, NULL scheme terminates */
+extern inkpot_status_t inkpot_schemes ( inkpot_t *inkpot, const char *scheme, ... );
+/* The scheme for color output representation, */
+extern inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme );
+
+/* set inkpot color by name as interpeted by the current schemes */
extern inkpot_status_t inkpot_set ( inkpot_t *inkpot, const char *color );
+/* set inkpot color to the default (from the first scheme specified) */
extern inkpot_status_t inkpot_set_default ( inkpot_t *inkpot );
+/* set inkpot color by value, which may or may not have a name in the current or any schemes */
extern inkpot_status_t inkpot_set_rgba ( inkpot_t *inkpot, unsigned char rgba[4] );
-extern inkpot_status_t inkpot_write ( inkpot_t *inkpot );
-
+/* get inkpot color name in the translation scheme, or for colors without a name in the translated scheme,
+ * set NULL and return INKPOT_COLOR_NONAME */
extern inkpot_status_t inkpot_get ( inkpot_t *inkpot, const char **color );
+
+/* get inkpot color value in various formats */
extern inkpot_status_t inkpot_get_rgba ( inkpot_t *inkpot, unsigned char *rgba );
extern inkpot_status_t inkpot_get_hsva ( inkpot_t *inkpot, unsigned char *hsva );
extern inkpot_status_t inkpot_get_cmyk ( inkpot_t *inkpot, unsigned char *cmyk );
extern inkpot_status_t inkpot_get_HSVA ( inkpot_t *inkpot, double *HSVA );
extern inkpot_status_t inkpot_get_index ( inkpot_t *inkpot, unsigned int *index );
-extern inkpot_status_t inkpot_print_schemes ( inkpot_t *inkpot );
-extern inkpot_status_t inkpot_print_names ( inkpot_t *inkpot );
-extern inkpot_status_t inkpot_print_names_out ( inkpot_t *inkpot );
-extern inkpot_status_t inkpot_print_values ( inkpot_t *inkpot );
+/* output the current color to out_writer (default stdout) */
+/* returns INKPOT_COLOR_NONAME if it converted the color to a hex numeric string value */
+extern inkpot_status_t inkpot_write ( inkpot_t *inkpot );
+
+/* debugging and error functions that oput to the err_writer (default stderr) */
+extern inkpot_status_t inkpot_debug_schemes ( inkpot_t *inkpot );
+extern inkpot_status_t inkpot_debug_names ( inkpot_t *inkpot );
+extern inkpot_status_t inkpot_debug_names_out ( inkpot_t *inkpot );
+extern inkpot_status_t inkpot_debug_values ( inkpot_t *inkpot );
+/* write the most recent inkpot status to the err writer */
extern inkpot_status_t inkpot_error ( inkpot_t *inkpot );
#ifdef __cplusplus
**********************************************************/
#include <stdio.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
return fwrite(data, sizeof(char), length, (FILE *)closure);
}
-inkpot_status_t inkpot_clear ( inkpot_t *inkpot )
+static inkpot_status_t inkpot_clear ( inkpot_t *inkpot )
{
inkpot->scheme_bits = 0; /* clear schemes */
- inkpot->name = NULL; /* clear cached value */
+ inkpot->name = NULL; /* clear cached names */
inkpot->scheme_index = NULL;
inkpot->out_name = NULL;
inkpot->out_scheme_index = NULL;
inkpot = malloc(sizeof(inkpot_t));
if (inkpot) {
- inkpot->writer = inkpot_writer;
+ inkpot->out_writer = inkpot_writer;
inkpot->out_closure = stdout;
+ inkpot->err_writer = inkpot_writer;
inkpot->err_closure = stderr;
rc = inkpot_clear ( inkpot );
return inkpot;
}
-inkpot_status_t inkpot_writer_fn ( inkpot_t *inkpot, void *writer, void *out_closure, void *err_closure )
+inkpot_status_t inkpot_out_writer ( inkpot_t *inkpot, void *writer, void *closure )
{
- inkpot->writer = writer;
- inkpot->out_closure = out_closure;
- inkpot->err_closure = err_closure;
+ inkpot->out_writer = writer;
+ inkpot->out_closure = closure;
+ return ((inkpot->status = INKPOT_SUCCESS));
+}
+inkpot_status_t inkpot_err_writer ( inkpot_t *inkpot, void *writer, void *closure )
+{
+ inkpot->err_writer = writer;
+ inkpot->err_closure = closure;
return ((inkpot->status = INKPOT_SUCCESS));
}
inkpot_scheme_index_cmpf);
}
-inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme )
+static inkpot_status_t inkpot_scheme ( inkpot_t *inkpot, const char *scheme )
{
inkpot_scheme_name_t *inkpot_scheme_name;
inkpot_scheme_index_t *inkpot_scheme_index;
if (scheme == NULL)
return ((inkpot->status = INKPOT_SCHEME_UNKNOWN));
+
inkpot_scheme_name = inkpot_find_scheme_name(scheme);
if (inkpot_scheme_name) {
idx = inkpot_scheme_name - TAB_SCHEMES_NAME;
return ((inkpot->status = INKPOT_SUCCESS));
}
+inkpot_status_t inkpot_schemes ( inkpot_t *inkpot, const char *scheme, ... )
+{
+ inkpot_status_t rc;
+ va_list argp;
+ const char *s;
+
+ if (scheme == NULL)
+ return ((inkpot->status = INKPOT_SCHEME_UNKNOWN));
+
+ rc = inkpot_clear(inkpot);
+ if (rc != INKPOT_SUCCESS)
+ return rc;
+
+ va_start(argp, scheme);
+ for (s = scheme; s; s = va_arg(argp, const char*)) {
+ rc = inkpot_scheme(inkpot, s);
+ if (rc != INKPOT_SUCCESS)
+ break;
+ }
+ va_end(argp);
+ return rc;
+}
+
inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme )
{
inkpot_scheme_name_t *inkpot_scheme_name;
if (scheme == NULL)
return ((inkpot->status = INKPOT_SCHEME_UNKNOWN));
+
inkpot_scheme_name = inkpot_find_scheme_name(scheme);
if (inkpot_scheme_name) {
idx = inkpot_scheme_name - TAB_SCHEMES_NAME;
}
return ((inkpot->status = INKPOT_SUCCESS));
}
+
inkpot_scheme_index = inkpot_find_scheme_index(scheme);
if (! inkpot_scheme_index)
return ((inkpot->status = INKPOT_SCHEME_UNKNOWN));
return ((inkpot->status = INKPOT_FAIL));
}
-static void iputs(inkpot_t *inkpot, const char *s)
+static void errputs(inkpot_t *inkpot, const char *s)
{
- inkpot->writer(inkpot->out_closure, s, strlen(s));
+ inkpot->err_writer(inkpot->out_closure, s, strlen(s));
}
-inkpot_status_t inkpot_print_schemes( inkpot_t *inkpot )
+inkpot_status_t inkpot_debug_schemes( inkpot_t *inkpot )
{
IDX_SCHEMES_NAME i;
int found;
- iputs(inkpot, "schemes:\n");
+ errputs(inkpot, "schemes:\n");
for (i = 0; i < SZT_SCHEMES_NAME; i++) {
found = 0;
if ((1<<i) & inkpot->scheme_bits) {
- iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
- iputs(inkpot, " (in)");
+ errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
+ errputs(inkpot, " (in)");
if (i == inkpot->default_scheme_name_idx)
- iputs(inkpot, " (default)");
+ errputs(inkpot, " (default)");
found++;
}
if ((1<<i) & inkpot->out_scheme_bit) {
if (! found)
- iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
- iputs(inkpot, " (out)");
+ errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
+ errputs(inkpot, " (out)");
found++;
}
if (found)
- iputs(inkpot, "\n");
+ errputs(inkpot, "\n");
}
found = 0;
if (inkpot->scheme_index) {
- iputs(inkpot, &TAB_STRINGS[inkpot->scheme_index->string_idx]);
- iputs(inkpot, " (indexed) (in)");
+ errputs(inkpot, &TAB_STRINGS[inkpot->scheme_index->string_idx]);
+ errputs(inkpot, " (indexed) (in)");
found++;
}
if (inkpot->out_scheme_index) {
if (! found) {
- iputs(inkpot, &TAB_STRINGS[inkpot->out_scheme_index->string_idx]);
- iputs(inkpot, " (indexed)");
+ errputs(inkpot, &TAB_STRINGS[inkpot->out_scheme_index->string_idx]);
+ errputs(inkpot, " (indexed)");
}
- iputs(inkpot, " (out)");
+ errputs(inkpot, " (out)");
found++;
}
if (found)
- iputs(inkpot, "\n");
- iputs(inkpot, "\n");
+ errputs(inkpot, "\n");
+ errputs(inkpot, "\n");
return ((inkpot->status = INKPOT_SUCCESS));
}
-static inkpot_status_t inkpot_print_scheme_names( inkpot_t *inkpot, int scheme_bits )
+static inkpot_status_t inkpot_debug_scheme_names( inkpot_t *inkpot, int scheme_bits )
{
IDX_SCHEMES_NAME i;
int found = 0;
- iputs(inkpot, "(");
+ errputs(inkpot, "(");
for (i = 0; i < SZT_SCHEMES_NAME; i++) {
if ((1 << i) & scheme_bits) {
if (found++)
- iputs(inkpot, " ");
- iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
+ errputs(inkpot, " ");
+ errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]);
}
}
- iputs(inkpot, ")");
+ errputs(inkpot, ")");
return INKPOT_SUCCESS;
}
-static void inkpot_print_rgba( inkpot_t *inkpot, unsigned char *rgba )
+static void inkpot_debug_rgba( inkpot_t *inkpot, unsigned char *rgba )
{
char buf[20];
sprintf(buf, "%d,%d,%d,%d", rgba[0], rgba[1], rgba[2], rgba[3]);
- iputs(inkpot, buf);
+ errputs(inkpot, buf);
}
-static inkpot_status_t inkpot_print_names_schemes( inkpot_t *inkpot, BIT_SCHEMES_NAME scheme_bits, inkpot_scheme_index_t *scheme_index )
+static inkpot_status_t inkpot_debug_names_schemes( inkpot_t *inkpot, BIT_SCHEMES_NAME scheme_bits, inkpot_scheme_index_t *scheme_index )
{
inkpot_name_t *name;
IDX_NAMES i;
for (i = 0; i < SZT_NAMES; i++) {
name = &TAB_NAMES[i];
if (scheme_bits & name->scheme_bits) {
- iputs(inkpot, &TAB_STRINGS[TAB_NAMES[i].string_idx]);
- inkpot_print_scheme_names(inkpot, scheme_bits);
- iputs(inkpot, " ");
- inkpot_print_rgba(inkpot, TAB_VALUES[name->value_idx].rgba);
+ errputs(inkpot, &TAB_STRINGS[TAB_NAMES[i].string_idx]);
+ inkpot_debug_scheme_names(inkpot, scheme_bits);
+ errputs(inkpot, " ");
+ inkpot_debug_rgba(inkpot, TAB_VALUES[name->value_idx].rgba);
if (name->value_idx == inkpot->default_value_idx)
- iputs(inkpot, " (default)");
- iputs(inkpot, "\n");
+ errputs(inkpot, " (default)");
+ errputs(inkpot, "\n");
}
}
}
for (k = first; k < last; k++) {
v = TAB_IXVALUES[k];
sprintf(buf, "%d(", k - first);
- iputs(inkpot, buf);
- iputs(inkpot, &TAB_STRINGS[scheme_index->string_idx]);
- iputs(inkpot, ") ");
+ errputs(inkpot, buf);
+ errputs(inkpot, &TAB_STRINGS[scheme_index->string_idx]);
+ errputs(inkpot, ") ");
if (v < SZT_VALUES)
- inkpot_print_rgba(inkpot, TAB_VALUES[v].rgba);
+ inkpot_debug_rgba(inkpot, TAB_VALUES[v].rgba);
else
- inkpot_print_rgba(inkpot, TAB_NONAME_VALUES[v - SZT_VALUES].rgba);
- iputs(inkpot, "\n");
+ inkpot_debug_rgba(inkpot, TAB_NONAME_VALUES[v - SZT_VALUES].rgba);
+ errputs(inkpot, "\n");
}
}
- iputs(inkpot, "\n");
+ errputs(inkpot, "\n");
return ((inkpot->status = INKPOT_SUCCESS));
}
-inkpot_status_t inkpot_print_names( inkpot_t *inkpot )
+inkpot_status_t inkpot_debug_names( inkpot_t *inkpot )
{
BIT_SCHEMES_NAME scheme_bits = inkpot->scheme_bits;
inkpot_scheme_index_t *scheme_index = inkpot->scheme_index;
- iputs(inkpot, "names (in):\n");
- return inkpot_print_names_schemes(inkpot, scheme_bits, scheme_index);
+ errputs(inkpot, "names (in):\n");
+ return inkpot_debug_names_schemes(inkpot, scheme_bits, scheme_index);
}
-inkpot_status_t inkpot_print_names_out( inkpot_t *inkpot )
+inkpot_status_t inkpot_debug_names_out( inkpot_t *inkpot )
{
BIT_SCHEMES_NAME scheme_bits = inkpot->out_scheme_bit;
inkpot_scheme_index_t *scheme_index = inkpot->out_scheme_index;
- iputs(inkpot, "names (out):\n");
- return inkpot_print_names_schemes(inkpot, scheme_bits, scheme_index);
+ errputs(inkpot, "names (out):\n");
+ return inkpot_debug_names_schemes(inkpot, scheme_bits, scheme_index);
}
-/* Print all values that are members of the currently activated
+/* Print all values that are members of the currently listed
* name schemes, with the names in those schemes.
* Does not print the indexes in index schemes that a value may
* be a member of. */
-inkpot_status_t inkpot_print_values( inkpot_t *inkpot )
+inkpot_status_t inkpot_debug_values( inkpot_t *inkpot )
{
inkpot_value_t *value;
inkpot_name_t *name;
BIT_SCHEMES_NAME scheme_bits;
int found;
- iputs(inkpot, "values:\n");
+ errputs(inkpot, "values:\n");
for (i = 0; i < SZT_VALUES; i++) {
value = &TAB_VALUES[i];
found = 0;
scheme_bits = name->scheme_bits & inkpot->scheme_bits;
if (scheme_bits) {
if (found++)
- iputs(inkpot, " ");
+ errputs(inkpot, " ");
else
- inkpot_print_rgba(inkpot, TAB_VALUES[i].rgba);
- iputs(inkpot, " ");
- iputs(inkpot, &TAB_STRINGS[name->string_idx]);
- inkpot_print_scheme_names(inkpot, scheme_bits);
+ inkpot_debug_rgba(inkpot, TAB_VALUES[i].rgba);
+ errputs(inkpot, " ");
+ errputs(inkpot, &TAB_STRINGS[name->string_idx]);
+ inkpot_debug_scheme_names(inkpot, scheme_bits);
}
}
if (found)
- iputs(inkpot, "\n");
+ errputs(inkpot, "\n");
}
- iputs(inkpot, "\n");
+ errputs(inkpot, "\n");
return ((inkpot->status = INKPOT_SUCCESS));
}
rc = inkpot_get(inkpot, &color);
if (rc == INKPOT_SUCCESS)
- inkpot->writer(inkpot->out_closure, color, strlen(color));
+ inkpot->out_writer(inkpot->out_closure, color, strlen(color));
if (rc == INKPOT_COLOR_NONAME) {
value_idx = inkpot->value_idx;
if (value_idx < SZT_VALUES)
for (m = 0; m < 4; m++) *p++ = *q++;
sprintf(buf, "#%02x%02x%02x%02x", rgba[0], rgba[1], rgba[2], rgba[3]);
- inkpot->writer(inkpot->out_closure, buf, sizeof(buf));
+ inkpot->out_writer(inkpot->out_closure, buf, sizeof(buf));
}
return rc;
}
case INKPOT_FAIL:
m = "INKPOT_FAIL\n"; break;
}
- inkpot->writer(inkpot->err_closure, m, strlen(m));
+ inkpot->err_writer(inkpot->err_closure, m, strlen(m));
return ((inkpot->status = INKPOT_SUCCESS));
};