os-workshop

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit aff03d71b026d65f7153a220172cfe5e98e537f6
parent 2bab211168171b52d45fe0ca0dbb51a45ff99ead
Author: Brian Swetland <swetland@frotz.net>
Date:   Fri, 13 May 2022 11:00:42 -0700

libgfx: color management

Diffstat:
Mlibgfx/inc/gfx/gfx.h | 20+++++++++++++++++++-
Mlibgfx/src/gfx.c | 18++++++++++++++++--
2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/libgfx/inc/gfx/gfx.h b/libgfx/inc/gfx/gfx.h @@ -21,6 +21,7 @@ struct gfx_surface { void (*plot)(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t pxl); void (*hline)(gfx_surface_t* gs, uint32_t x0, uint32_t y, uint32_t x1, uint32_t pxl); void (*putc)(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t ch); + uint32_t (*color)(gfx_surface_t* gs, uint32_t argb8888); }; #define PIXFMT_RGB565 0x03000565 @@ -40,11 +41,15 @@ static inline void gfx_putc(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t gs->putc(gs, x, y, ch); } +static inline uint32_t gfx_color(gfx_surface_t* gs, uint32_t argb8888) { + return gs->color(gs, argb8888); +} + void gfx_fill(gfx_surface_t* gs, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t pxl); void gfx_clear(gfx_surface_t* gs, uint32_t pxl); void gfx_puts(gfx_surface_t* gs, uint32_t x, uint32_t y, const char* s); void gfx_printf(gfx_surface_t* gs, uint32_t x, uint32_t y, const char* fmt, ...); - +void gfx_setcolor(gfx_surface_t* gs, uint32_t fg_argb8888, uint32_t bg_argb8888); typedef struct txt_surface { gfx_surface_t *gs; @@ -58,3 +63,16 @@ void txt_init(txt_surface_t *ts, gfx_surface_t *gs); void txt_puts(txt_surface_t *ts, const char *s); void txt_printf(txt_surface_t *ts, const char *fmt, ...); void txt_goto(txt_surface_t *ts, uint32_t cx, uint32_t cy); + + +#define C_BLACK 0x00000000 +#define C_WHITE 0x00FFFFFF +#define C_GRAY25 0x00404040 +#define C_GRAY50 0x00808080 +#define C_GRAY75 0x00C0C0C0 +#define C_RED 0x00FF0000 +#define C_GREEN 0x0000FF00 +#define C_BLUE 0x000000FF +#define C_YELLOW 0x00FFFF00 +#define C_PURPLE 0x00FF00FF +#define C_CYAN 0x0000FFFF diff --git a/libgfx/src/gfx.c b/libgfx/src/gfx.c @@ -15,11 +15,18 @@ extern uint8_t vga_rom_16[256 * 16]; #define CH_HEIGHT 16 // drawing routines for unknown pixfmt +static uint32_t color0(gfx_surface_t *gs, uint32_t c) { return 0; } static void plot0(gfx_surface_t *gs, uint32_t x, uint32_t y, uint32_t pxl) {} static void hline0(gfx_surface_t *gs, uint32_t x0, uint32_t y, uint32_t x1, uint32_t pxl) {} static void putc0(gfx_surface_t *gs, uint32_t x, uint32_t y, uint32_t c) {} // drawing routines for 16bpp pixfmt +static uint32_t color16(gfx_surface_t *gs, uint32_t c) { + return ((c >> 3) & 0x1F) | + (((c >> 10) & 0x3F) << 5) | + (((c >> 19) & 0x1F) << 11); +} + static void plot16(gfx_surface_t *gs, uint32_t x, uint32_t y, uint32_t pxl) { if (unlikely((x >= gs->width) || (y >= gs->height))) { return; @@ -68,11 +75,13 @@ void gfx_init(gfx_surface_t *gs) { gs->plot = plot16; gs->hline = hline16; gs->putc = putc16; + gs->color = color16; break; default: gs->plot = plot0; gs->hline = hline0; gs->putc = putc0; + gs->color = color0; break; } } @@ -83,9 +92,14 @@ void gfx_init_display(gfx_surface_t *gs) { gs->stride = 640; gs->pixels = (void*) FRAMEBUFFER_BASE; gs->pixfmt = PIXFMT_RGB565; - gs->fgcolor = 0xFFFF; - gs->bgcolor = 0x0000; gfx_init(gs); + gs->fgcolor = gs->color(gs, C_WHITE); + gs->bgcolor = gs->color(gs, C_BLACK); +} + +void gfx_setcolor(gfx_surface_t* gs, uint32_t fg_argb8888, uint32_t bg_argb8888) { + gs->fgcolor = gs->color(gs, fg_argb8888); + gs->bgcolor = gs->color(gs, bg_argb8888); } void gfx_fill(gfx_surface_t* gs, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t pxl) {