changeset 11:d1cdb90d9558

core: implement color manipulation, closes #2446
author David Demelier <markand@malikania.fr>
date Tue, 07 Jan 2020 20:41:11 +0100
parents c91c3272101b
children 29b479760c05
files src/color.h src/font.c src/window.c
diffstat 3 files changed, 88 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /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 <markand@malikania.fr>
+ *
+ * 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 */
--- 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 <SDL_ttf.h>
 
+#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));
--- 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 <SDL.h>
 
+#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