From 11cb4d5a442cf2bd8bc1e4135cd755a5b409b352 Mon Sep 17 00:00:00 2001
From: Gary Talent <gtalent2@gmail.com>
Date: Sun, 29 Mar 2020 13:35:23 -0500
Subject: [PATCH] [nostalgia/core] Add color 16 functions

---
 src/nostalgia/core/gfx.cpp | 11 ++++++++---
 src/nostalgia/core/gfx.hpp | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/nostalgia/core/gfx.cpp b/src/nostalgia/core/gfx.cpp
index 9de8d23f..68d7f6e6 100644
--- a/src/nostalgia/core/gfx.cpp
+++ b/src/nostalgia/core/gfx.cpp
@@ -164,15 +164,15 @@ uint8_t blue32(Color32 c) noexcept {
 
 
 uint8_t red32(Color16 c) noexcept {
-	return ((c & 0b0000000000011111) >> 0) * 8;
+	return red16(c) * 8;
 }
 
 uint8_t green32(Color16 c) noexcept {
-	return ((c & 0b0000001111100000) >> 5) * 8;
+	return green16(c) * 8;
 }
 
 uint8_t blue32(Color16 c) noexcept {
-	return ((c & 0b0111110000000000) >> 10) * 8;
+	return blue16(c) * 8;
 }
 
 void puts(Context *ctx, int column, int row, const char *str) {
@@ -181,4 +181,9 @@ void puts(Context *ctx, int column, int row, const char *str) {
 	}
 }
 
+static_assert(color16(0, 31, 0, 0) == 992);
+static_assert(color16(16, 31, 0, 0) == 1008);
+static_assert(color16(16, 31, 8, 0) == 9200);
+static_assert(color16(16, 31, 8, 1) == 41968);
+
 }
diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp
index e703dff5..94926749 100644
--- a/src/nostalgia/core/gfx.hpp
+++ b/src/nostalgia/core/gfx.hpp
@@ -83,12 +83,32 @@ ox::Error model(T *io, NostalgiaGraphic *ng) {
 [[nodiscard]] uint8_t blue32(Color16 c) noexcept;
 
 
+[[nodiscard]] constexpr Color16 color16(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
+	return r | (g << 5) | (b << 10) | (a << 15);
+}
+
 [[nodiscard]] uint8_t red32(Color32 c) noexcept;
 
 [[nodiscard]] uint8_t green32(Color32 c) noexcept;
 
 [[nodiscard]] uint8_t blue32(Color32 c) noexcept;
 
+[[nodiscard]] constexpr uint8_t red16(Color16 c) noexcept {
+	return c & 0b0000000000011111;
+}
+
+[[nodiscard]] constexpr uint8_t green16(Color16 c) noexcept {
+	return (c & 0b0000001111100000) >> 5;
+}
+
+[[nodiscard]] constexpr uint8_t blue16(Color16 c) noexcept {
+	return (c & 0b0111110000000000) >> 10;
+}
+
+[[nodiscard]] constexpr uint8_t alpha16(Color16 c) noexcept {
+	return c >> 15;
+}
+
 void puts(Context *ctx, int column, int row, const char *str);
 
 void setTile(Context *ctx, int layer, int column, int row, uint8_t tile);