From ad6a7bec26ce0234ed3d3df2d6fdfd8bbbc9d2b4 Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Sat, 8 Feb 2020 12:59:45 -0500 Subject: [PATCH] [theme] actually doing colors differently --- src/ax.h | 6 +++--- src/ctxt/ctxt.c | 22 +++++++++++++++++----- src/ctxt/theme.c | 45 ++++++++++++++++++++++++++------------------- src/ctxt/theme.h | 29 +++++++++++++++++++++-------- test/main.c | 12 ++++++++++-- 5 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/ax.h b/src/ax.h index 642c769..e90e313 100644 --- a/src/ax.h +++ b/src/ax.h @@ -26,11 +26,11 @@ void ax_log(struct ax_ctxt* ax, const char* string); void ax_begin_theme(struct ax_ctxt* ax); int ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm); -void ax_set_theme_color( +int ax_set_theme_color( struct ax_ctxt* ax, const char* cat, int64_t rgb); -void ax_set_theme_font( +int ax_set_theme_font( struct ax_ctxt* ax, const char* cat, const char* fnt_path); -void ax_set_theme_iconset( +int ax_set_theme_iconset( struct ax_ctxt* ax, const char* iconset_dir); void ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm); diff --git a/src/ctxt/ctxt.c b/src/ctxt/ctxt.c index 042e2a9..a45fdee 100644 --- a/src/ctxt/ctxt.c +++ b/src/ctxt/ctxt.c @@ -79,21 +79,33 @@ int ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm) return 0; } -void ax_set_theme_color( - struct ax_ctxt* ax, const char* cat, int64_t rgb) +int ax_set_theme_color( + struct ax_ctxt* ax, const char* cat_name, int64_t rgb) { ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme"); - ax__theme_add_color(ax->thmb, cat, rgb); + int rv; + + enum ax_color_cat cat; + if ((rv = ax__string_to_color_cat(cat_name, &cat)) != 0) { +#define FMT "invalid color category `%s'" + sprintf(ax__make_error_buf(ax, strlen(FMT) + strlen(cat_name)), + FMT, cat_name); + return rv; +#undef FMT + } + + ax__theme_set_color(ax->thmb, cat, rgb); + return 0; } -void ax_set_theme_font( +int ax_set_theme_font( struct ax_ctxt* ax, const char* cat, const char* fnt_path) { ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme"); UNIMPLEMENTED(); } -void ax_set_theme_iconset( +int ax_set_theme_iconset( struct ax_ctxt* ax, const char* iconset_dir) { ASSERT(ax->thmb != NULL, "`ax_set_theme_iconset' called while not building a theme"); diff --git a/src/ctxt/theme.c b/src/ctxt/theme.c index f566cb3..972bf4f 100644 --- a/src/ctxt/theme.c +++ b/src/ctxt/theme.c @@ -5,26 +5,30 @@ static void theme_init(struct ax_theme* thm) { - thm->colors.len = 0; - thm->colors.cap = 0; - thm->colors.keys = NULL; - thm->colors.vals = NULL; + thm->colors[AX_COLOR_BACKGROUND] = 0xffffff; + thm->colors[AX_COLOR_SURFACE] = 0xffffff; + thm->colors[AX_COLOR_PRIMARY] = 0xff0000; + thm->colors[AX_COLOR_PRIMARY_ALT] = 0xff4444; + thm->colors[AX_COLOR_SECONDARY] = 0x0000ff; + thm->colors[AX_COLOR_SECONDARY_ALT] = 0x4444ff; + thm->colors[AX_COLOR_ON_BACKGROUND] = + thm->colors[AX_COLOR_ON_SURFACE] = + thm->colors[AX_COLOR_ON_PRIMARY] = + thm->colors[AX_COLOR_ON_SECONDARY] = 0x000000; } void ax__theme_builder_init(struct ax_theme_builder* thmb) { - rgn_init(&thmb->rgn, THMB_RGN_SIZE); theme_init(&thmb->thm); } void ax__theme_builder_cleanup(struct ax_theme_builder* thmb) { - rgn_cleanup(&thmb->rgn); } -void ax__theme_add_color( +void ax__theme_set_color( struct ax_theme_builder* thmb, - const char* key, int64_t val) + enum ax_color_cat cat, int64_t val) { UNIMPLEMENTED(); } @@ -34,17 +38,20 @@ void ax__theme_builder_finish( struct rgn* rgn, struct ax_theme** out_thm) { - ASSERT_NON_NULL(out_thm, "out_thm"); - - // TODO: this might be a common pattern, to transfer ownership - // of 'thmb->rgn' to 'rgn' by allocating and pinning - struct rgn* thm_internal_rgn = ralloc_typed(rgn, struct rgn, 1); - *thm_internal_rgn = thmb->rgn; - rgn_pin(rgn, thm_internal_rgn, (void*) rgn_cleanup); - struct ax_theme* thm = ralloc_typed(rgn, struct ax_theme, 1); *thm = thmb->thm; - *out_thm = thm; - - ax__theme_builder_init(thmb); + if (out_thm != NULL) { + *out_thm = thm; + } +} + +int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat) +{ + // TODO: this + return 1; +} + +void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str) +{ + UNIMPLEMENTED(); } diff --git a/src/ctxt/theme.h b/src/ctxt/theme.h index 0e95c77..72ee545 100644 --- a/src/ctxt/theme.h +++ b/src/ctxt/theme.h @@ -3,25 +3,38 @@ #include #include "../util/region.h" +enum ax_color_cat { + AX_COLOR_BACKGROUND = 0, + AX_COLOR_SURFACE, + AX_COLOR_PRIMARY, + AX_COLOR_PRIMARY_ALT, + AX_COLOR_SECONDARY, + AX_COLOR_SECONDARY_ALT, + AX_COLOR_ON_BACKGROUND, + AX_COLOR_ON_SURFACE, + AX_COLOR_ON_PRIMARY, + AX_COLOR_ON_SECONDARY, + AX_COLOR__COUNT, + AX_COLOR__DEFAULT, +}; + struct ax_theme { - struct { - size_t len, cap; - const char** keys; - int64_t* vals; - } colors; + int64_t colors[AX_COLOR__COUNT]; }; struct ax_theme_builder { - struct rgn rgn; struct ax_theme thm; }; +int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat); +void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str); + void ax__theme_builder_init(struct ax_theme_builder* thmb); void ax__theme_builder_cleanup(struct ax_theme_builder* thmb); -void ax__theme_add_color( +void ax__theme_set_color( struct ax_theme_builder* thmb, - const char* key, int64_t val); + enum ax_color_cat cat, int64_t val); void ax__theme_builder_finish( struct ax_theme_builder* thmb, diff --git a/test/main.c b/test/main.c index 52a4cf3..c9d5567 100644 --- a/test/main.c +++ b/test/main.c @@ -6,13 +6,21 @@ int main(void) { struct ax_ctxt* ax = ax_new(); + int rv; ax_begin_theme(ax); - ax_set_theme_color(ax, "default", 0x112233); + if ((rv = ax_set_theme_color(ax, "default", 0x112233)) != 0) { + goto cleanup; + } + struct ax_theme* thm; ax_end_theme(ax, &thm); ax_select_theme(ax, thm); +cleanup: + if (rv != 0) { + printf("error: %s\n", ax_get_error(ax)); + } ax_free(ax); - return 0; + return rv; }