Skip to content

Commit

Permalink
Rewrite normalization algorithms
Browse files Browse the repository at this point in the history
papadanku committed Feb 14, 2022
1 parent f6612f7 commit 3463a67
Showing 1 changed file with 15 additions and 61 deletions.
76 changes: 15 additions & 61 deletions shaders/cColorNormalization.fx
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@

uniform int _Select <
ui_type = "combo";
ui_items = " Built-in RG\0 Built-in RGB\0 Standard RG\0 Standard RGB\0 Jamie Wong's RG\0 Jamie Wong's RGB\0 Ratio\0 UV\0 Maxwell\0 Angle-Retaining\0 None\0";
ui_items = " Length (RG)\0 Length (RGB)\0 Average (RG)\0 Average (RGB)\0 Sum (RG)\0 Sum (RGB)\0 Max (RG)\0 Max (RGB)\0 None\0";
ui_label = "Method";
ui_tooltip = "Select Chromaticity";
> = 0;
@@ -65,23 +65,6 @@ void PostProcessVS(in uint ID : SV_VertexID, out float4 Position : SV_Position,
// Pixel shaders

/*
Sources
Ratio, UV, Maxwell, and Angle-Retaining Chromaticity
Copyright 2020 Marco Buzzelli, Simone Bianco, Raimondo Schettini.
If you use this code in your research, please cite:
@article{buzzelli2020arc,
title = {ARC: Angle-Retaining Chromaticity diagram for color constancy error analysis},
author = {Marco Buzzelli and Simone Bianco and Raimondo Schettini},
journal = {J. Opt. Soc. Am. A},
number = {11},
pages = {1721--1730},
publisher = {OSA},
volume = {37},
month = {Nov},
year = {2020},
doi = {10.1364/JOSAA.398692}
}
Jamie Wong's Chromaticity
Title = "Color: From Hexcodes to Eyeballs"
Authors = Jamie Wong
@@ -96,58 +79,29 @@ void NormalizationPS(in float4 Position : SV_Position, in float2 TexCoord : TEXC
float3 Color = max(tex2D(_SampleColor, TexCoord).rgb, Minima);
switch(_Select)
{
case 0:
// Built-in RG
case 0: // Length (RG)
OutputColor0.rg = saturate(normalize(Color).rg);
break;
case 1:
// Built-in RGB
case 1: // Length (RGB)
OutputColor0 = saturate(normalize(Color));
break;
case 2:
// Standard RG
OutputColor0.rg = saturate(Color.rg / dot(Color, 1.0));
break;
case 3:
// Standard RGB
OutputColor0 = saturate(Color / dot(Color, 1.0));
case 2: // Average (RG)
OutputColor0.rg = saturate(Color.rg / dot(Color, 1.0 / 3.0));
break;
case 4:
// Jamie Wong's RG
float3 NormalizedRGB = Color / dot(Color, 1.0);
OutputColor0.rg = saturate(NormalizedRGB.rg / max(max(NormalizedRGB.r, NormalizedRGB.g), NormalizedRGB.b));
case 3: // Average (RGB)
OutputColor0 = saturate(Color / dot(Color, 1.0 / 3.0));
break;
case 5:
// Jamie Wong's RGB
OutputColor0 = Color / dot(Color, 1.0);
OutputColor0 = saturate(OutputColor0 / max(max(OutputColor0.r, OutputColor0.g), OutputColor0.b));
case 4: // Sum (RG)
OutputColor0.rg = saturate(Color.rg / dot(Color, 1.0));
break;
case 6: // Ratio
OutputColor0.rg = saturate(Color.rg / Color.bb);
break;
case 7: // UV
OutputColor0.rg = saturate(log(Color.rg / Color.bb));
case 5: // Sum (RGB)
OutputColor0 = saturate(Color / dot(Color, 1.0));
break;
case 8: // Maxwell
float TotalColor = dot(Color, 1.0);
OutputColor0.r = saturate(dot(Color, float3(-1.0, 2.0, -1.0)) / (sqrt(3.0) * TotalColor));
OutputColor0.g = saturate((Color.r - Color.b) / TotalColor);
case 6: // Max (RG)
OutputColor0.rg = saturate(Color.rg / max(max(Color.r, Color.g), Color.b));
break;
case 9:
// Angle-Retaining Chromaticity (Optimized for GPU)
float2 AlphaA;
AlphaA.x = dot(Color.gb, float2(sqrt(3.0), -sqrt(3.0)));
AlphaA.y = dot(Color, float3(2.0, -1.0, -1.0));
float AlphaR = acos(dot(Color, 1.0) / (sqrt(3.0) * length(Color)));
float AlphaC = AlphaR / length(AlphaA);
float2 Alpha = AlphaC * AlphaA.yx;

float2 AlphaMin, AlphaMax;
AlphaMin.y = -(sqrt(3.0) / 2.0) * acos(rsqrt(3.0));
AlphaMax.y = (sqrt(3.0) / 2.0) * acos(rsqrt(3.0));
AlphaMin.x = -acos(sqrt(2.0 / 3.0));
AlphaMax.x = AlphaMin.x + (AlphaMax.y - AlphaMin.y);
OutputColor0.rg = saturate((Alpha.xy - AlphaMin.xy) / (AlphaMax.xy - AlphaMin.xy));
case 7: // Max (RGB)
OutputColor0 = saturate(Color / max(max(Color.r, Color.g), Color.b));
break;
default:
// No Chromaticity

0 comments on commit 3463a67

Please sign in to comment.