From 2bf599b7865f86ade6a24f594aa9804bca29f4d5 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Mon, 13 Jun 2016 14:04:16 +0200 Subject: [PATCH] Kandinsky: Introduce KDColorRGB Change-Id: Ic64f2ff9a441580940d37151e190fd0a3d954d8a --- kandinsky/Makefile | 5 ++++- kandinsky/include/kandinsky/color.h | 15 ++++++++++++++- kandinsky/test/color.c | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 kandinsky/test/color.c diff --git a/kandinsky/Makefile b/kandinsky/Makefile index 70f5038a1..ee9103e40 100644 --- a/kandinsky/Makefile +++ b/kandinsky/Makefile @@ -8,7 +8,10 @@ objs += $(addprefix kandinsky/src/,\ text.o\ types.o\ ) -tests += $(addprefix kandinsky/test/, set_pixel.c) +tests += $(addprefix kandinsky/test/,\ + color.c\ + set_pixel.c\ +) FREETYPE_PATH := /usr/local/Cellar/freetype/2.6.3 # LIBPNG_PATH is optional. If LIBPNG_PATH is not defined, rasterizer will be diff --git a/kandinsky/include/kandinsky/color.h b/kandinsky/include/kandinsky/color.h index f8bc2232e..07fb5a99b 100644 --- a/kandinsky/include/kandinsky/color.h +++ b/kandinsky/include/kandinsky/color.h @@ -3,8 +3,21 @@ #include +// Kandinsky uses RGB565 + typedef uint16_t KDColor; -#define KDColorRGB(r,g,b) ((KDColor)(((r&0x1F)<<11)&((g&0x3F)<<5)&(b&0x1F))) +/* KDColorRGB takes a 32-bits RGB color and builds a KDColor from it. + * Given KDColor is RGB565 and that we take 8-bit values for R, G, and B, + * some shifting has to happen. */ + +#define KDColorRGB(r,g,b) ((KDColor)((((((uint16_t)(r))>>3)&0x1F)<<11)|(((((uint16_t)(g))>>2)&0x3F)<<5)|((((uint16_t)(b))>>3)&0x1F))) + +#define KDColorBlack KDColorRGB(0x00, 0x00, 0x00) +#define KDColorWhite KDColorRGB(0xFF, 0xFF, 0xFF) + +#define KDColorRed KDColorRGB(0xFF, 0x00, 0x00) +#define KDColorGreen KDColorRGB(0x00, 0xFF, 0x00) +#define KDColorBlue KDColorRGB(0x00, 0x00, 0xFF) #endif diff --git a/kandinsky/test/color.c b/kandinsky/test/color.c new file mode 100644 index 000000000..bc22f6dd6 --- /dev/null +++ b/kandinsky/test/color.c @@ -0,0 +1,18 @@ +#include +#include +#include + +QUIZ_CASE(kandinsky_color_rgb) { + KDColor foo = KDColorRGB(0xFF, 0, 0); + assert(KDColorRGB(0xFF, 0, 0) == 0xF800); + assert(KDColorRGB(0, 0xFF, 0) == 0x07E0); + assert(KDColorRGB(0, 0, 0xFF) == 0x1F); + /* R = 0x12 = 0b 0001 0010. 5 most important bits are 00010. + * G = 0x34 = 0b 0011 0100. 6 most important bits are 001101. + * B = 0x56 = 0b 0101 0110. 5 most important bits are 01010. + * KDColor = 0b 00010 001101 01010 + * = 0b 0001 0001 1010 1010 + * = 0x 1 1 A A */ + assert(KDColorRGB(0x12, 0x34, 0x56) == 0x11AA); +} +