# HG changeset patch # User David Demelier # Date 1578426071 -3600 # Node ID d1cdb90d955870a087e947c0d036054a83e9d8ff # Parent c91c3272101bc66ec57785045020cdfab6f852f8 core: implement color manipulation, closes #2446 diff -r c91c3272101b -r d1cdb90d9558 src/color.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/color.h Tue Jan 07 20:41:11 2020 +0100 @@ -0,0 +1,74 @@ +/* + * color.h -- basic color routines + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_COLOR_H +#define MOLKO_COLOR_H + +/** + * \file color.h + * \brief Basic color routines. + */ + +/** + * Get red component of hexadecimal color. + * + * \param c the hexadecimal color + * \return the red component + */ +#define COLOR_R(c) (c >> 24 & 0xff) + +/** + * Get green component of hexadecimal color. + * + * \param c the hexadecimal color + * \return the green component + */ +#define COLOR_G(c) (c >> 16 & 0xff) + +/** + * Get blue component of hexadecimal color. + * + * \param c the hexadecimal color + * \return the blue component + */ +#define COLOR_B(c) (c >> 8 & 0xff) + +/** + * Get alpha component of hexadecimal color. + * + * \param c the hexadecimal color + * \return the alpha component + */ +#define COLOR_A(c) (c & 0xff) + +/** + * Convert individual RGBA components into a hexadecimal color. + * + * \param r the red component + * \param g the green component + * \param b the blue component + * \param a the alpha component + * \return the hexadecimal color + */ +#define COLOR_HEX(r, g, b, a) \ + (r << 24 & 0xff000000 | \ + g << 16 & 0x00ff0000 | \ + b << 8 & 0x0000ff00 | \ + a & 0x000000ff) + +#endif /* !MOLKO_COLOR_H */ diff -r c91c3272101b -r d1cdb90d9558 src/font.c --- a/src/font.c Tue Jan 07 20:28:35 2020 +0100 +++ b/src/font.c Tue Jan 07 20:41:11 2020 +0100 @@ -22,6 +22,7 @@ #include +#include "color.h" #include "font.h" #include "texture_p.h" @@ -78,10 +79,10 @@ /* TODO: refactor this with window.c */ SDL_Color fg = { - .r = color >> 24 & 0xff, - .g = color >> 16 & 0xff, - .b = color >> 8 & 0xff, - .a = color & 0xff + .r = COLOR_R(color), + .g = COLOR_G(color), + .b = COLOR_B(color), + .a = COLOR_A(color) }; return texture_from_surface(TTF_RenderUTF8_Blended(font->handle, text, fg)); diff -r c91c3272101b -r d1cdb90d9558 src/window.c --- a/src/window.c Tue Jan 07 20:28:35 2020 +0100 +++ b/src/window.c Tue Jan 07 20:41:11 2020 +0100 @@ -20,6 +20,7 @@ #include +#include "color.h" #include "window.h" #include "window_p.h" @@ -50,18 +51,19 @@ SDL_GetRenderDrawColor(win.renderer, &r, &g, &b, &a); - return r << 24 | g << 16 | b << 8 | a; + return COLOR_HEX(r, g, b, a); } void window_set_color(uint32_t color) { - const Uint8 r = color >> 24 & 0xff; - const Uint8 g = color >> 16 & 0xff; - const Uint8 b = color >> 8 & 0xff; - const Uint8 a = color & 0xff; - - SDL_SetRenderDrawColor(win.renderer, r, g, b, a); + SDL_SetRenderDrawColor( + win.renderer, + COLOR_R(color), + COLOR_G(color), + COLOR_B(color), + COLOR_A(color) + ); } void