From df534fdcb98f20843f58178a7d83adefbe1b4379 Mon Sep 17 00:00:00 2001 From: ellson Date: Thu, 25 Sep 2008 04:44:31 +0000 Subject: [PATCH] now with "#ff0000" input strings --- lib/inkpot/inkpot.h | 2 +- lib/inkpot/inkpot_scheme.c | 116 +++++++++++++++++++++++------------- lib/inkpot/inkpot_structs.h | 15 +++-- lib/inkpot/inkpot_tables.h | 22 +++---- lib/inkpot/test.c | 15 ++--- 5 files changed, 103 insertions(+), 67 deletions(-) diff --git a/lib/inkpot/inkpot.h b/lib/inkpot/inkpot.h index 5e607a715..92a986b0d 100644 --- a/lib/inkpot/inkpot.h +++ b/lib/inkpot/inkpot.h @@ -25,8 +25,8 @@ typedef enum { INKPOT_SUCCESS=0, INKPOT_MALLOC_FAIL, INKPOT_COLOR_UNKNOWN, + INKPOT_COLOR_UNNAMED, INKPOT_SCHEME_UNKNOWN, - INKPOT_MAX_ONE_INDEXED_SCHEME, INKPOT_FAIL } inkpot_status_t; diff --git a/lib/inkpot/inkpot_scheme.c b/lib/inkpot/inkpot_scheme.c index 02e76e16a..25426ab1d 100644 --- a/lib/inkpot/inkpot_scheme.c +++ b/lib/inkpot/inkpot_scheme.c @@ -42,7 +42,7 @@ static int inkpot_scheme_name_cmpf ( const void *key, const void *base) return string_cmpf(k, b); } -static inkpot_scheme_name_t *inkpot_activate_name ( const char *scheme ) +static inkpot_scheme_name_t *inkpot_find_scheme_name ( const char *scheme ) { if (scheme == NULL) return NULL; @@ -60,7 +60,7 @@ static int inkpot_scheme_index_cmpf ( const void *key, const void *base) return string_cmpf(k, b); } -static inkpot_scheme_index_t *inkpot_activate_index ( const char *scheme ) +static inkpot_scheme_index_t *inkpot_find_scheme_index ( const char *scheme ) { if (scheme == NULL) return NULL; @@ -94,7 +94,7 @@ inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ) if (scheme == NULL) return INKPOT_SCHEME_UNKNOWN; - inkpot_scheme_name = inkpot_activate_name(scheme); + inkpot_scheme_name = inkpot_find_scheme_name(scheme); if (inkpot_scheme_name) { idx = inkpot_scheme_name - TAB_SCHEMES_NAME; if (! inkpot->scheme_bits) { @@ -109,24 +109,21 @@ inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ) } if (! (inkpot->scheme_bits & (1 << idx))) { inkpot->scheme_bits |= 1 << idx; - inkpot->name = NULL; /* clear cached value */ + inkpot->name = NULL; /* clear cached name */ } } else { - inkpot_scheme_index = inkpot_activate_index(scheme); + inkpot_scheme_index = inkpot_find_scheme_index(scheme); if (! inkpot_scheme_index) return INKPOT_SCHEME_UNKNOWN; if (inkpot->scheme_index != inkpot_scheme_index) { - if (inkpot->scheme_index) - return INKPOT_MAX_ONE_INDEXED_SCHEME; inkpot->scheme_index = inkpot_scheme_index; if (! inkpot->scheme_bits ) { - /* Set a default color from an index scheme only if no named schemes - * have been specified yet. Will be overwritten by the default of the - * first name scheme if given later */ + /* Set a default color from an index scheme only if no + * named schemes are currently active */ inkpot->default_value_idx = TAB_IXVALUES[inkpot_scheme_index->first_value_idx]; } - inkpot->name = NULL; /* clear cached value */ + inkpot->name = NULL; /* clear cached name */ } } return INKPOT_SUCCESS; @@ -146,6 +143,8 @@ static inkpot_status_t inkpot_set_name ( inkpot_t *inkpot, const char *color ) const char *last_name; IDX_NAMES j; IDX_STRINGS k; + unsigned char *p, *q; + int m; if (inkpot == NULL) return INKPOT_SCHEME_UNKNOWN; @@ -174,7 +173,9 @@ static inkpot_status_t inkpot_set_name ( inkpot_t *inkpot, const char *color ) return INKPOT_COLOR_UNKNOWN; inkpot->name = &TAB_NAMES[j]; /* cache name resolution */ } - inkpot->value = &TAB_VALUES[inkpot->name->value_idx]; + p = inkpot->rgba; + q = TAB_VALUES[inkpot->name->value_idx].rgba; + for (m = 0; m < 4; m++) *p++ = *q++; /* record value */ return INKPOT_SUCCESS; } @@ -183,6 +184,9 @@ static inkpot_status_t inkpot_set_index ( inkpot_t *inkpot, int index ) inkpot_scheme_index_t *scheme_index; IDX_SCHEMES_INDEX j; IDX_IXVALUES first, last; + IDX_VALUES v; + unsigned char *p, *q; + int m; scheme_index = inkpot->scheme_index; if (!scheme_index) @@ -201,18 +205,39 @@ static inkpot_status_t inkpot_set_index ( inkpot_t *inkpot, int index ) index += first; assert(index < SZT_IXVALUES); - assert(TAB_IXVALUES[index] < SZT_VALUES); - inkpot->value = &TAB_VALUES[TAB_IXVALUES[index]]; + v = TAB_IXVALUES[index]; + if (v < SZT_VALUES) + q = TAB_VALUES[v].rgba; + else { + v -= SZT_VALUES; + assert(v < SZT_NONAME_VALUES); + q = TAB_NONAME_VALUES[v].rgba; + } + p = inkpot->rgba; + for (m = 0; m < 4; m++) *p++ = *q++; /* record value */ return INKPOT_SUCCESS; } inkpot_status_t inkpot_set( inkpot_t *inkpot, const char *color ) { - int index; + int index, r, g, b, a=255; + unsigned char rgba[4]; inkpot_status_t rc = INKPOT_COLOR_UNKNOWN; - if (color && sscanf(color, "%d", &index) == 1) - rc = inkpot_set_index(inkpot, index); + if (!color) + return INKPOT_COLOR_UNKNOWN; + + if (sscanf(color, "#%2x%2x%2x%2x", &r, &g, &b, &a) >= 3) { + rgba[0] = r; + rgba[1] = g; + rgba[2] = b; + rgba[3] = a; + rc = inkpot_set_rgba(inkpot, rgba); + } + + if (rc != INKPOT_SUCCESS) + if (sscanf(color, "%d", &index) == 1) + rc = inkpot_set_index(inkpot, index); if (rc != INKPOT_SUCCESS) rc = inkpot_set_name(inkpot, color); @@ -222,7 +247,12 @@ inkpot_status_t inkpot_set( inkpot_t *inkpot, const char *color ) inkpot_status_t inkpot_set_default( inkpot_t *inkpot ) { - inkpot->value = &TAB_VALUES[inkpot->default_value_idx]; + unsigned char *p, *q; + int m; + + p = inkpot->rgba; + q = TAB_VALUES[inkpot->default_value_idx].rgba; + for (m = 0; m < 4; m++) *p++ = *q++; /* record value */ return INKPOT_SUCCESS; } @@ -249,18 +279,23 @@ inkpot_status_t inkpot_set_rgba ( inkpot_t *inkpot, unsigned char *rgba ) inkpot_noname_value_t *noname_value; IDX_VALUES value_idx; IDX_NAMES i; + unsigned char *p, *q; + int m; /* * FIXME - implement caching and check here */ + p = inkpot->rgba; + q = rgba; + for (m = 0; m < 4; m++) *p++ = *q++; /* record value */ + value = (inkpot_value_t *) bsearch( (void*)(rgba), (void*)TAB_VALUES, SZT_VALUES, sizeof(inkpot_value_t), inkpot_rgba_cmpf); if (value) { - inkpot->value = value; /* record value */ inkpot->name = NULL; /* clear name */ value_idx = value - TAB_VALUES; for (i = value->toname_idx; i < SZT_NAMES; i++) { @@ -280,12 +315,10 @@ inkpot_status_t inkpot_set_rgba ( inkpot_t *inkpot, unsigned char *rgba ) SZT_NONAME_VALUES, sizeof(inkpot_noname_value_t), inkpot_rgba_cmpf); - if (noname_value) { - inkpot->value = value; /* record value */ - inkpot->name = NULL; /* clear name */ + if (noname_value) return INKPOT_SUCCESS; - } + return INKPOT_COLOR_UNNAMED; #if 0 /* need some sort of btree here so that we can insert rgba * values and keep sorted */ @@ -297,25 +330,33 @@ inkpot_status_t inkpot_set_rgba ( inkpot_t *inkpot, unsigned char *rgba ) /* insert value and keep sorted */ -#endif - return INKPOT_SUCCESS; + +#endif } -inkpot_status_t inkpot_get ( inkpot_t *inkpot, const char *scheme, const char **color ) +inkpot_status_t inkpot_get ( inkpot_t *inkpot, const char *scheme, const char **tocolor ) { - /* FIXME */ - return INKPOT_FAIL; + inkpot_name_t *name = inkpot->name; + + /* FIXME - implement to-scheme */ + + if (name) + *tocolor = &TAB_STRINGS[name->string_idx]; + else + *tocolor = NULL; + + return INKPOT_SUCCESS; } inkpot_status_t inkpot_get_rgba ( inkpot_t *inkpot, unsigned char *rgba ) { - unsigned char *t = &(inkpot->value->r); + unsigned char *t = inkpot->rgba; *rgba++ = *t++; *rgba++ = *t++; *rgba++ = *t++; - *rgba = *t; + *rgba++ = *t++; return INKPOT_SUCCESS; } @@ -401,7 +442,6 @@ static inkpot_status_t inkpot_print_rgba( unsigned char *rgba, FILE *out ) inkpot_status_t inkpot_print_names( inkpot_t *inkpot, FILE *out ) { inkpot_name_t *name; - unsigned char *rgba; inkpot_scheme_index_t *scheme_index; IDX_NAMES i; IDX_SCHEMES_INDEX j; @@ -416,11 +456,10 @@ inkpot_status_t inkpot_print_names( inkpot_t *inkpot, FILE *out ) name = &TAB_NAMES[i]; scheme_bits = name->scheme_bits & inkpot_scheme_bits; if (scheme_bits) { - rgba = &(TAB_VALUES[name->value_idx].r); fprintf(out, "%s", &TAB_STRINGS[TAB_NAMES[i].string_idx]); inkpot_print_scheme_names(inkpot, scheme_bits, out); fprintf(out, " "); - inkpot_print_rgba(rgba, out); + inkpot_print_rgba(TAB_VALUES[name->value_idx].rgba, out); fprintf(out, "\n"); } } @@ -437,12 +476,11 @@ inkpot_status_t inkpot_print_names( inkpot_t *inkpot, FILE *out ) for (k = first; k < last; k++) { v = TAB_IXVALUES[k]; + fprintf (out, "%d(%s) ", k - first , &TAB_STRINGS[scheme_index->string_idx]); if (v < SZT_VALUES) - rgba = &(TAB_VALUES[v].r); + inkpot_print_rgba(TAB_VALUES[v].rgba, out); else - rgba = &(TAB_NONAME_VALUES[v - SZT_VALUES].r); - fprintf (out, "%d(%s) ", k - first , &TAB_STRINGS[scheme_index->string_idx]); - inkpot_print_rgba(rgba, out); + inkpot_print_rgba(TAB_NONAME_VALUES[v - SZT_VALUES].rgba, out); fprintf(out, "\n"); } } @@ -461,12 +499,10 @@ inkpot_status_t inkpot_print_values( inkpot_t *inkpot, FILE *out ) IDX_NAMES t; BIT_SCHEMES_NAME scheme_bits; int found; - unsigned char *rgba; fprintf(out, "values:\n"); for (i = 0; i < SZT_VALUES; i++) { value = &TAB_VALUES[i]; - rgba = &(TAB_VALUES[i].r); found = 0; for (t = value->toname_idx; t < SZT_NAMES; t++) { name = &TAB_NAMES[TAB_NAMES[t].toname_idx]; @@ -477,7 +513,7 @@ inkpot_status_t inkpot_print_values( inkpot_t *inkpot, FILE *out ) if (found++) fprintf(out, " "); else - inkpot_print_rgba(rgba, out); + inkpot_print_rgba(TAB_VALUES[i].rgba, out); fprintf(out, " %s", &TAB_STRINGS[name->string_idx]); inkpot_print_scheme_names(inkpot, scheme_bits, out); } diff --git a/lib/inkpot/inkpot_structs.h b/lib/inkpot/inkpot_structs.h index 564a4a4b8..a714a0330 100644 --- a/lib/inkpot/inkpot_structs.h +++ b/lib/inkpot/inkpot_structs.h @@ -88,7 +88,7 @@ typedef struct inkpot_value_s { /* Numeric color values used by the set * are numerically sorted by rgb value * in TAB_VALUES[] */ - unsigned char r, g, b, a; /* rgba */ + unsigned char rgba[4]; /* rgba */ IDX_NAMES toname_idx; /* An index into TAB_NAMES to the toname @@ -109,15 +109,16 @@ typedef struct inkpot_noname_value_s { /* Numeric color values used by the remai * are numerically sorted by rgba value * in TAB_NONAME_VALUES[] */ - unsigned char r, g, b, a; /* rgba */ + unsigned char rgba[4]; /* rgba */ } inkpot_noname_value_t; /* typedef struct inkpot_s inkpot_t; */ /* public opaque type in inkpot.h */ struct inkpot_s { /* The Ink Pot */ BIT_SCHEMES_NAME - scheme_bits; /* One bit per inkpot_scheme_name_t, + scheme_bits, /* One bit per inkpot_scheme_name_t, *max 32 schemes */ + out_scheme_bit; /* One scheme only for output. */ IDX_NAMES first_name_idx, last_name_idx; /* The aggregate of the ranges @@ -136,11 +137,13 @@ struct inkpot_s { /* The Ink Pot */ default_value_idx; /* The index of the inkpot_value_t for the * default color */ - inkpot_scheme_index_t *scheme_index; /* Pointer to the indexed - * color scheme, or NULL. (Only 1 allowed) */ + inkpot_scheme_index_t + *scheme_index, /* Pointer to the indexed + * color scheme, or NULL. (Only 1 at a time) */ + *out_scheme_index; /* Indexed output scheme, or NULL */ - inkpot_value_t *value; /* The most recently resolved color value. */ inkpot_name_t *name; /* The most recently resolved color name. */ + unsigned char rgba[4]; /* The most recently resolved color value. */ }; #endif /* INKPOT_STRUCTS_H */ diff --git a/lib/inkpot/inkpot_tables.h b/lib/inkpot/inkpot_tables.h index 728a960ef..8aac9a85c 100644 --- a/lib/inkpot/inkpot_tables.h +++ b/lib/inkpot/inkpot_tables.h @@ -57,21 +57,21 @@ inkpot_name_t TAB_NAMES[] = { /* Must be LC_ALL=C sort'ed by name */ #define SZT_NAMES (sizeof(TAB_NAMES)/sizeof(TAB_NAMES[0])) inkpot_value_t TAB_VALUES[] = { /* Must be LC_ALL=C sort'ed by r,g,b,a */ - { 0, 0, 0, 255, 0, }, /* black */ - { 0, 0, 255, 255, 1, }, /* blue, bleu */ - { 0, 255, 0, 255, 3, }, /* green, vert */ - { 255, 0, 0, 255, 5, }, /* red, rouge */ - { 255, 192, 0, 255, 7, }, /* yellow (svg) */ - { 255, 255, 0, 255, 8, }, /* yellow (x11), jaune */ - { 255, 255, 255, 255, 10, }, /* white */ + {{0, 0, 0, 255}, 0 }, /* black */ + {{0, 0, 255, 255}, 1 }, /* blue, bleu */ + {{0, 255, 0, 255}, 3 }, /* green, vert */ + {{255, 0, 0, 255}, 5 }, /* red, rouge */ + {{255, 192, 0, 255}, 7 }, /* yellow (svg) */ + {{255, 255, 0, 255}, 8 }, /* yellow (x11), jaune */ + {{255, 255, 255, 255}, 10}, /* white */ }; #define SZT_VALUES (sizeof(TAB_VALUES)/sizeof(TAB_VALUES[0])) inkpot_noname_value_t TAB_NONAME_VALUES[] = { /* Must be LC_ALL=C sort'ed by r,g,b,a */ - { 0, 0, 128, 255 }, - { 0, 0, 160, 255 }, - { 0, 0, 192, 255 }, - { 0, 0, 224, 255 }, + {{0, 0, 128, 255}}, + {{0, 0, 160, 255}}, + {{0, 0, 192, 255}}, + {{0, 0, 224, 255}}, }; #define SZT_NONAME_VALUES (sizeof(TAB_NONAME_VALUES)/sizeof(TAB_NONAME_VALUES[0])) diff --git a/lib/inkpot/test.c b/lib/inkpot/test.c index 24e955ecd..40c50b190 100644 --- a/lib/inkpot/test.c +++ b/lib/inkpot/test.c @@ -8,6 +8,7 @@ int main (int argc, char *argv[]) inkpot_t *inkpot; inkpot_status_t rc; char *color; + const char *tocolor; int i; unsigned char rgba[4]; @@ -24,15 +25,10 @@ int main (int argc, char *argv[]) else { for (i = 2; i < argc; i++) { rc = inkpot_activate(inkpot, argv[i]); - if (rc == INKPOT_SCHEME_UNKNOWN) { + if (rc == INKPOT_SCHEME_UNKNOWN) fprintf (stderr, "color scheme \"%s\" was not found\n", argv[i]); - } - else if (rc == INKPOT_MAX_ONE_INDEXED_SCHEME) { - fprintf (stderr, "only one indexed color scheme can be used, \"%s\" was ignored\n", argv[i]); - } - else { + else assert(rc == INKPOT_SUCCESS); - } } } @@ -48,10 +44,11 @@ int main (int argc, char *argv[]) color = argv[1]; rc = inkpot_set(inkpot, color); - if (rc == INKPOT_SUCCESS) { + if (rc == INKPOT_SUCCESS || rc == INKPOT_COLOR_UNNAMED) { inkpot_get_rgba(inkpot, rgba); + inkpot_get(inkpot, "x11", &tocolor); fprintf(stderr, "%s %d,%d,%d,%d\n", - color, rgba[0], rgba[1], rgba[2], rgba[3]); + tocolor, rgba[0], rgba[1], rgba[2], rgba[3]); } else { rc = inkpot_set_default(inkpot); -- 2.40.0