From ab953fbc0c933b7b7c4ff60306888c776cf9da39 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Fri, 22 Apr 2022 18:58:25 +0100
Subject: [PATCH] some colour utilities

---
 fggl/gui/containers.cpp     |  4 +---
 include/fggl/gui/widget.hpp | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/fggl/gui/containers.cpp b/fggl/gui/containers.cpp
index 60056ec..ce6f1fc 100644
--- a/fggl/gui/containers.cpp
+++ b/fggl/gui/containers.cpp
@@ -54,11 +54,9 @@ namespace fggl::gui {
 	}*/
 
 	void Panel::render(gfx::Paint &paint) {
-		spdlog::info("Imma render mah panel! {}, {}", bottomRight().x, bottomRight().y );
-
 		// background painting time
 		gfx::Path2D background(topLeft());
-		background.colour(math::vec3(1.0F, 1.0F, 0.0F));
+		background.colour(math::vec3(32.0f/255.0f, 74.0F/255.0F, 135.0F/255.0F));
 		draw_box(background, topLeft(), bottomRight());
 		paint.fill(background);
 
diff --git a/include/fggl/gui/widget.hpp b/include/fggl/gui/widget.hpp
index f740bf1..813cf39 100644
--- a/include/fggl/gui/widget.hpp
+++ b/include/fggl/gui/widget.hpp
@@ -8,6 +8,43 @@
 
 namespace fggl::gui {
 
+	constexpr float RGB_MAX_VAL = 255.0F;
+	constexpr math::vec3 rgbToNormal(char red, char green, char blue) {
+		return {red / RGB_MAX_VAL, green / RGB_MAX_VAL, blue / RGB_MAX_VAL};
+	}
+
+	/**
+	 * Convert a Hue-saturation-value colour to RGB
+	 *
+	 * @param hue hue component, assumed 0 - 360
+	 * @param saturation assumed 0 - 1
+	 * @param value assumed 0 - 1
+	 * @return RGB, in the range 0,1 for each component
+	 */
+	constexpr math::vec3 HSVtoNormal(float hue, float saturation, float value) {
+		assert( 0 < hue && hue <= 360);
+		assert( 0 < saturation && saturation <= 1);
+		assert( 0 < value && value <= 1);
+
+		const float chroma = value * saturation;
+		const float x = chroma * (1 - fabs( fmod(hue / 60.0F, 2) - 1) );
+		math::vec3 tmp{0,0,0};
+		if ( 0 <= hue && hue < 60 ) {
+			tmp = {chroma,x,0};
+		} else if ( 60 <= hue && hue < 120 ) {
+			tmp = {x, chroma, 0};
+		} else if (120 <= hue && hue < 180 ) {
+			tmp = {0, chroma, x};
+		} else if ( 180 <= hue && hue < 240 ) {
+			tmp = {0, x, chroma};
+		} else if ( 240 <= hue && hue < 300) {
+			tmp = {x, 0, chroma};
+		} else if ( 300 <= hue && hue < 360) {
+			tmp = {chroma, 0, x};
+		}
+		return tmp + (value - chroma);
+	}
+
 	void draw_box( gfx::Path2D& path, math::vec2 topLeft, math::vec2 bottomRight);
 	void draw_progress( gfx::Path2D& path, math::vec2 topLeft, math::vec2 size, float value);
 	void draw_slider( gfx::Path2D& path, math::vec2 topLeft, math::vec2 size, float value);
-- 
GitLab