From 9f0a4756457b0865eec8620bb34e3af8cd02ecf9 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 16 Apr 2006 21:26:25 +0000 Subject: [PATCH] * Moved cucul_set_color() from canvas.c to colour.c. * Added cucul_set_truecolor() for non-ANSI colours. * Added new argb32 -> rgb12 conversion functions. --- cucul/canvas.c | 20 ---------- cucul/colour.c | 87 +++++++++++++++++++++++++++++++++++++++++ cucul/cucul.h | 3 +- cucul/cucul_internals.h | 2 + 4 files changed, 91 insertions(+), 21 deletions(-) diff --git a/cucul/canvas.c b/cucul/canvas.c index da82477..ee76dd0 100644 --- a/cucul/canvas.c +++ b/cucul/canvas.c @@ -37,26 +37,6 @@ #include "cucul.h" #include "cucul_internals.h" -/** \brief Set the default colour pair. - * - * This function sets the default colour pair. String functions such as - * caca_printf() and graphical primitive functions such as caca_draw_line() - * will use these colour pairs. - * - * \param qq A handle to the libcucul canvas. - * \param fgcolor The requested foreground colour. - * \param bgcolor The requested background colour. - */ -void cucul_set_color(cucul_t *qq, unsigned int fgcolor, unsigned int bgcolor) -{ - /* FIXME */ - if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15) - return; - - qq->fgcolor = fgcolor; - qq->bgcolor = bgcolor; -} - /** \brief Print an ASCII character. * * This function prints an ASCII character at the given coordinates, using diff --git a/cucul/colour.c b/cucul/colour.c index d11cb3b..e4cec03 100644 --- a/cucul/colour.c +++ b/cucul/colour.c @@ -27,6 +27,61 @@ static const uint16_t ansitab[16] = 0xf444, 0xf44f, 0xf4f4, 0xf4ff, 0xff44, 0xff4f, 0xfff4, 0xffff, }; +/** \brief Set the default colour pair. + * + * This function sets the default ANSI colour pair. String functions such as + * caca_printf() and graphical primitive functions such as caca_draw_line() + * will use these colours. + * + * Color values are those defined in \e cucul.h, such as CUCUL_COLOR_RED + * or CUCUL_COLOR_TRANSPARENT. + * + * \param qq A handle to the libcucul canvas. + * \param fg The requested foreground colour. + * \param bg The requested background colour. + */ +void cucul_set_color(cucul_t *qq, unsigned char fg, unsigned char bg) +{ + if(fg > 0x20 || bg > 0x20) + return; + + qq->fgcolor = fg; + qq->bgcolor = bg; +} + +/** \brief Set the default colour pair (truecolor version). + * + * This function sets the default colour pair. String functions such as + * caca_printf() and graphical primitive functions such as caca_draw_line() + * will use these colours. + * + * Colors are 16-bit ARGB values, each component being coded on 4 bits. For + * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is + * white with 50% alpha (A=8 R=15 G=15 B=15). + * + * \param qq A handle to the libcucul canvas. + * \param fg The requested foreground colour. + * \param bg The requested background colour. + */ +void cucul_set_truecolor(cucul_t *qq, unsigned int fg, unsigned int bg) +{ + if(fg > 0xffff || bg > 0xffff) + return; + + if(fg < 0x100) + fg += 0x100; + + if(bg < 0x100) + bg += 0x100; + + qq->fgcolor = fg; + qq->bgcolor = bg; +} + +/* + * XXX: the following functions are local + */ + static uint8_t nearest_ansi(uint16_t argb16, uint8_t def) { unsigned int i, best, dist; @@ -88,6 +143,38 @@ uint8_t _cucul_argb32_to_ansi4bg(uint32_t c) return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK); } +uint16_t _cucul_argb32_to_rgb12fg(uint32_t c) +{ + uint16_t fg = c & 0xffff; + + if(fg < CUCUL_COLOR_DEFAULT) + return ansitab[fg] & 0x0fff; + + if(fg == CUCUL_COLOR_DEFAULT) + return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff; + + if(fg == CUCUL_COLOR_TRANSPARENT) + return 0x0fff; + + return fg & 0x0fff; +} + +uint16_t _cucul_argb32_to_rgb12bg(uint32_t c) +{ + uint16_t bg = c >> 16; + + if(bg < CUCUL_COLOR_DEFAULT) + return ansitab[bg] & 0x0fff; + + if(bg == CUCUL_COLOR_DEFAULT) + return ansitab[CUCUL_COLOR_BLACK] & 0x0fff; + + if(bg == CUCUL_COLOR_TRANSPARENT) + return 0x0fff; + + return bg & 0x0fff; +} + void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8]) { uint16_t fg = c & 0xffff; diff --git a/cucul/cucul.h b/cucul/cucul.h index e2c8301..d651de9 100644 --- a/cucul/cucul.h +++ b/cucul/cucul.h @@ -97,7 +97,8 @@ void cucul_free_buffer(cucul_buffer_t *); * higher level graphics functions. * * @{ */ -void cucul_set_color(cucul_t *, unsigned int, unsigned int); +void cucul_set_color(cucul_t *, unsigned char, unsigned char); +void cucul_set_truecolor(cucul_t *, unsigned int, unsigned int); char const *cucul_get_color_name(unsigned int); void cucul_putchar(cucul_t *, int, int, char); void cucul_putstr(cucul_t *, int, int, char const *); diff --git a/cucul/cucul_internals.h b/cucul/cucul_internals.h index df7efbd..bfccf55 100644 --- a/cucul/cucul_internals.h +++ b/cucul/cucul_internals.h @@ -65,6 +65,8 @@ extern uint32_t _cucul_cp437_to_utf32(uint8_t); uint8_t _cucul_argb32_to_ansi8(uint32_t); uint8_t _cucul_argb32_to_ansi4fg(uint32_t); uint8_t _cucul_argb32_to_ansi4bg(uint32_t); +uint16_t _cucul_argb32_to_rgb12fg(uint32_t); +uint16_t _cucul_argb32_to_rgb12bg(uint32_t); void _cucul_argb32_to_argb4(uint32_t, uint8_t[8]); /* Export functions */