From 04ebfad3e37eaf20e3b5f3ee705dc21b83e6f3e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= <tseeker@nocternity.net>
Date: Sun, 19 Nov 2017 18:49:01 +0100
Subject: [PATCH] Overrides - Color grading - Color wheel

---
 main.cc | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 134 insertions(+), 1 deletion(-)

diff --git a/main.cc b/main.cc
index 34fe7ef..4be8173 100644
--- a/main.cc
+++ b/main.cc
@@ -304,6 +304,129 @@ bool CGCComponentBar_(
 	return false;
 }
 
+bool HueSaturationPad(
+		char const* const name ,
+		float* const hue ,
+		float* const saturation ,
+		const float size = 180.f ) noexcept
+{
+	using namespace ImGui;
+
+	// Check/set bounding box
+	const auto wSize{ ImMin( size , CalcItemWidth( ) ) };
+	auto* const win( GetCurrentWindow( ) );
+	const ImVec2 cPos( win->DC.CursorPos );
+	const ImRect bb{ cPos , cPos + ImVec2( wSize , wSize ) };
+	auto& style( GetStyle( ) );
+	auto id{ win->GetID( name ) };
+	ItemSize( bb , style.FramePadding.y );
+	if ( !ItemAdd( bb , id ) ) {
+		return false;
+	}
+
+	const ImVec2 wCenter{ cPos + ImVec2( wSize * .5f , wSize * .5f ) };
+	const float wThickness = wSize * 0.08f;
+	const float wOuterRadius = wSize * 0.50f;
+	const float wInnerRadius = wOuterRadius - wThickness;
+
+	auto* const ctx( GetCurrentContext( ) );
+	const auto mPos{ ctx->IO.MousePos };
+	const auto rmPos{ mPos - wCenter };
+	const auto mcSqDist{ rmPos.x * rmPos.x + rmPos.y * rmPos.y };
+	const bool hovered{ mcSqDist <= wSize * wSize * .25f };
+	const bool tabFocus{ FocusableItemRegister( win , id ) };
+	if ( tabFocus || ( hovered && ctx->IO.MouseClicked[ 0 ] ) ) {
+		SetActiveID( id , win );
+		FocusWindow( win );
+	}
+
+	const bool active{ ctx->ActiveId == id };
+	float nHue{ *hue } , nSat{ *saturation };
+	if ( active ) {
+		if ( ctx->IO.MouseDown[ 0 ] ) {
+			nHue = ( 1.f + atan2f( -rmPos.y , -rmPos.x ) / IM_PI ) * .5f;
+			nSat = ImSaturate( sqrtf( mcSqDist ) / wInnerRadius );
+		} else {
+			ClearActiveID( );
+		}
+	} else if ( hovered && ctx->IO.KeyCtrl && ctx->IO.MouseWheel != 0.f ) {
+		const auto change{ ctx->IO.MouseWheel * .005f };
+		if ( ctx->IO.KeyShift ) {
+			nSat = ImSaturate( nSat + change );
+		} else {
+			nHue = fmodf( nHue + change + 1.f , 1.f );
+		}
+	}
+
+	//- DRAW ---------------------------------------------------------------
+
+	auto* const dl( GetWindowDrawList( ) );
+	const float aeps = 1.5f / wOuterRadius; // Half a pixel arc length in radians (2pi cancels out).
+	const int segmentPerArc = ImMax( 4 , (int) wOuterRadius / 12 );
+	const ImU32 hue_colors[] = {
+		IM_COL32( 255 ,	  0 ,   0 , 255 ) ,
+		IM_COL32( 255 , 255 ,   0 , 255 ) ,
+		IM_COL32(   0 , 255 ,   0 , 255 ) ,
+		IM_COL32(   0 , 255 , 255 , 255 ) ,
+		IM_COL32(   0 ,   0 , 255 , 255 ) ,
+		IM_COL32( 255 ,   0 , 255 , 255 ) ,
+		IM_COL32( 255 ,   0 ,   0 , 255 )
+	};
+	const auto avgRadius( ( wInnerRadius + wOuterRadius ) * .5f );
+
+	// Pad background and cross
+	const auto bgColor{ GetColorU32( ( hovered || active )
+			? ImVec4( .4f , .4f , .4f , 1 )
+			: ImVec4( .25f , .25f , .25f , 1 ) ) };
+	dl->AddCircleFilled( wCenter , avgRadius , bgColor );
+	dl->AddLine( wCenter - ImVec2( 0 , avgRadius ) ,
+			wCenter + ImVec2( 0 , avgRadius ) ,
+			GetColorU32( ImVec4( .1 , .1 , .1 , 1 ) ) );
+	dl->AddLine( wCenter - ImVec2( avgRadius , 0 ) ,
+			wCenter + ImVec2( avgRadius , 0 ) ,
+			GetColorU32( ImVec4( .1 , .1 , .1 , 1 ) ) );
+	// Color wheel
+	for ( int n = 0 ; n < 6 ; n ++ ) {
+		const float a0 = ( n )       / 6.f * 2.f * IM_PI - aeps;
+		const float a1 = ( n + 1.f ) / 6.f * 2.f * IM_PI + aeps;
+		const int vStart = dl->_VtxCurrentIdx;
+		dl->PathArcTo( wCenter , avgRadius , a0 , a1 , segmentPerArc );
+		dl->PathStroke( IM_COL32_WHITE , false, wThickness);
+
+		// Paint colors over existing vertices
+		const ImVec2 gradientP0(
+				wCenter.x + cosf( a0 ) * wInnerRadius ,
+				wCenter.y + sinf( a0 ) * wInnerRadius );
+		const ImVec2 gradientP1(
+				wCenter.x + cosf( a1 ) * wInnerRadius ,
+				wCenter.y + sinf( a1 ) * wInnerRadius );
+		ShadeVertsLinearColorGradientKeepAlpha(
+				dl->_VtxWritePtr - ( dl->_VtxCurrentIdx - vStart ),
+				dl->_VtxWritePtr ,
+				gradientP0 , gradientP1 ,
+				hue_colors[ n ] , hue_colors[ n + 1 ] );
+	}
+	// Cursor from hue and saturation
+	const float cAngle{ nHue * 2 * IM_PI };
+	const float cDist{ nSat * wInnerRadius };
+	ImVec4 cColor{ 0 , 0 , 0 , 1 };
+	ColorConvertHSVtoRGB( *hue , *saturation , 1 , cColor.x , cColor.y , cColor.z );
+	dl->AddCircleFilled( wCenter + ImVec2( cos( cAngle ) * cDist , sin( cAngle ) * cDist ) ,
+			( wOuterRadius - wInnerRadius ) * .5f ,
+			GetColorU32( cColor ) );
+	dl->AddCircle( wCenter + ImVec2( cos( cAngle ) * cDist , sin( cAngle ) * cDist ) ,
+			( wOuterRadius - wInnerRadius ) * .5f ,
+			GetColorU32( ImVec4( 1 , 1 , 1 , 1 ) ) );
+
+	// Update values
+	if ( *hue != nHue || *saturation != nSat ) {
+		*hue = nHue;
+		*saturation = nSat;
+		return true;
+	}
+	return false;
+}
+
 bool ColorGradingControls(
 		char const* const name ,
 		float* const red ,
@@ -320,7 +443,7 @@ bool ColorGradingControls(
 
 	ImGuiWindow* const window{ GetCurrentWindow() };
 	ImGuiStorage* const storage{ window->DC.StateStorage };
-	const bool wheelMode{ storage->GetBool( window->GetID( name ) ) };
+	const bool wheelMode{ storage->GetBool( window->GetID( name ) , true ) };
 
 	// Mode selection
 	bool modeChanged{ false };
@@ -334,6 +457,16 @@ bool ColorGradingControls(
 	bool changed;
 	if ( wheelMode ^ modeChanged ) {
 		changed = false;
+		const float scRed  { ImSaturate( ( *red   - base ) / ( unit * 2 ) ) } ,
+			    scGreen{ ImSaturate( ( *green - base ) / ( unit * 2 ) ) } ,
+			    scBlue { ImSaturate( ( *blue  - base ) / ( unit * 2 ) ) };
+		float H , S , V;
+		ColorConvertRGBtoHSV( scRed , scGreen , scBlue , H , S , V );
+
+		// FIXME test
+		static float fh = 0.f , fs = 0.f;
+		HueSaturationPad( "##wheel" , &fh , &fs , 180.f );
+
 #warning implement the fuck
 	} else {
 		PushItemWidth( -1 );