Skip to content

Commit

Permalink
Cleanup chromaticity code
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Jun 20, 2023
1 parent c71f512 commit d8ce5d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
31 changes: 11 additions & 20 deletions shaders/cshade/cChromaticity.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"

/*
[Shader Options]
Expand All @@ -8,7 +9,7 @@ uniform int _Select <
ui_label = "Method";
ui_tooltip = "Select Chromaticity";
ui_type = "combo";
ui_items = " Length (RG)\0 Length (RGB)\0 Average (RG)\0 Average (RGB)\0 Sum (RG)\0 Sum (RGB)\0";
ui_items = " Length (RG)\0 Length (RGB)\0 Average (RG)\0 Average (RGB)\0 Sum (RG)\0 Sum (RGB)\0 Polar (RG)\0";
> = 0;

/*
Expand All @@ -18,40 +19,30 @@ uniform int _Select <
float4 PS_Chromaticity(VS2PS_Quad Input) : SV_TARGET0
{
float3 Color = tex2D(CShade_SampleColorTex, Input.Tex0).rgb;
float Sum = 0.0;
float3 Chromaticity = 0.0;

switch(_Select)
{
case 0: // Length (RG)
Sum = length(Color.rgb);
Chromaticity.rg = saturate(Color.rg / Sum);
Chromaticity.rg = (Sum != 0.0) ? Chromaticity.rg : 1.0 / sqrt(3.0);
Chromaticity.rg = GetChromaticity(Color, 0).rg;
break;
case 1: // Length (RGB)
Sum = length(Color.rgb);
Chromaticity.rgb = saturate(Color.rgb / Sum);
Chromaticity.rgb = (Sum != 0.0) ? Chromaticity.rgb : 1.0 / sqrt(3.0);
Chromaticity.rgb = GetChromaticity(Color, 0).rgb;
break;
case 2: // Average (RG)
Sum = dot(Color.rgb, 1.0 / 3.0);
Chromaticity.rg = saturate(Color.rg / Sum);
Chromaticity.rg = (Sum != 0.0) ? Chromaticity.rg : 1.0;
Chromaticity.rg = GetChromaticity(Color, 1).rg;
break;
case 3: // Average (RGB)
Sum = dot(Color.rgb, 1.0 / 3.0);
Chromaticity.rgb = saturate(Color.rgb / Sum);
Chromaticity.rgb = (Sum != 0.0) ? Chromaticity.rgb : 1.0;
Chromaticity.rgb = GetChromaticity(Color, 1).rgb;
break;
case 4: // Sum (RG)
Sum = dot(Color.rgb, 1.0);
Chromaticity.rg = saturate(Color.rg / Sum);
Chromaticity.rg = (Sum != 0.0) ? Chromaticity.rg : 1.0 / 3.0;
Chromaticity.rg = GetChromaticity(Color, 2).rg;
break;
case 5: // Sum (RGB)
Sum = dot(Color.rgb, 1.0);
Chromaticity.rgb = saturate(Color.rgb / Sum);
Chromaticity.rgb = (Sum != 0.0) ? Chromaticity.rgb : 1.0 / 3.0;
Chromaticity.rgb = GetChromaticity(Color, 2).rgb;
break;
case 6: // Polar (RG)
Chromaticity.rg = GetPolar(Color);
break;
default: // No Chromaticity
Chromaticity.rgb = 0.0;
Expand Down
33 changes: 25 additions & 8 deletions shaders/cshade/shared/cImageProcessing.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
return Radius * SinCosTheta;
}

/*
[Convolutions - Edge Detection]
*/

/*
Linear filtered Sobel filter
*/
Expand Down Expand Up @@ -126,16 +130,29 @@
[Color Processing]
*/

float2 GetRG(float3 Color)
float3 GetChromaticity(float3 Color, int Method)
{
float SumRGB = dot(Color, 1.0);
float2 NRGB = (SumRGB == 0.0) ? 1.0 / 3.0 : Color.xy / SumRGB;
return NRGB;
}
float Sum = 0.0;
float White = 0.0;

float3 GetRGB(float2 Color)
{
return float3(Color.rg, saturate(1.0 - dot(Color.rg, 1.0)));
switch(Method)
{
case 0: // Length
Sum = length(Color);
White = 1.0 / sqrt(3.0);
break;
case 1: // Dot3 Average
Sum = dot(Color, 1.0 / 3.0);
White = 1.0;
break;
case 2: // Dot3 Sum
Sum = dot(Color, 1.0);
White = 1.0 / 3.0;
break;
}

float3 Chromaticity = (Sum == 0.0) ? White : Color / Sum;
return Chromaticity;
}

/*
Expand Down

0 comments on commit d8ce5d6

Please sign in to comment.