Browse Source

Added factor to grayscale node

CPKreuz 1 year ago
parent
commit
21676cddd2

+ 22 - 4
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/FilterNodes/GrayscaleNode.cs

@@ -6,8 +6,13 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.FilterNodes;
 [NodeInfo("GrayscaleFilter", "GRAYSCALE_FILTER_NODE")]
 public class GrayscaleNode : FilterNode
 {
+    private static readonly ColorMatrix WeightedMatrix = ColorMatrix.WeightedWavelengthGrayscale + ColorMatrix.UseAlpha;
+    private static readonly ColorMatrix AverageMatrix = ColorMatrix.AverageGrayscale + ColorMatrix.UseAlpha;
+    
     public InputProperty<GrayscaleMode> Mode { get; }
     
+    public InputProperty<double> Factor { get; }
+    
     public InputProperty<bool> Normalize { get; }
 
     // TODO: Hide when Mode != Custom
@@ -16,17 +21,31 @@ public class GrayscaleNode : FilterNode
     public GrayscaleNode()
     {
         Mode = CreateInput("Mode", "MODE", GrayscaleMode.Weighted);
+        // TODO: Clamp 0 - 1 in UI
+        Factor = CreateInput("Factor", "FACTOR", 1d);
         Normalize = CreateInput("Normalize", "NORMALIZE", true);
         CustomWeight = CreateInput("CustomWeight", "WEIGHT", new VecD3(1, 1, 1));
     }
 
     protected override ColorFilter GetColorFilter() => ColorFilter.CreateColorMatrix(Mode.Value switch
     {
-        GrayscaleMode.Weighted => ColorMatrix.WeightedWavelengthGrayscale + ColorMatrix.UseAlpha,
-        GrayscaleMode.Average => ColorMatrix.AverageGrayscale + ColorMatrix.UseAlpha,
-        GrayscaleMode.Custom => ColorMatrix.WeightedGrayscale(GetAdjustedCustomWeight()) + ColorMatrix.UseAlpha
+        GrayscaleMode.Weighted => UseFactor(WeightedMatrix),
+        GrayscaleMode.Average => UseFactor(AverageMatrix),
+        GrayscaleMode.Custom => UseFactor(ColorMatrix.WeightedGrayscale(GetAdjustedCustomWeight()) + ColorMatrix.UseAlpha)
     });
 
+    private ColorMatrix UseFactor(ColorMatrix target)
+    {
+        var factor = Factor.Value;
+
+        return factor switch
+        {
+            0 => ColorMatrix.Identity,
+            1 => target,
+            _ => ColorMatrix.Lerp(ColorMatrix.Identity, target, (float)factor)
+        };
+    }
+
     private VecD3 GetAdjustedCustomWeight()
     {
         var weight = CustomWeight.Value;
@@ -45,7 +64,6 @@ public class GrayscaleNode : FilterNode
         }
             
         return weight / weight.Sum();
-
     }
 
     public override Node CreateCopy() => new GrayscaleNode();

+ 6 - 0
src/PixiEditor.Numerics/ColorMatrix.cs

@@ -165,6 +165,12 @@ public record struct ColorMatrix
     public static ColorMatrix WeightedGrayscale(VecD3 vector) =>
         WeightedGrayscale((float)vector.X, (float)vector.Y, (float)vector.Z, 0);
     
+    public static ColorMatrix Lerp(ColorMatrix from, ColorMatrix to, float amount) => new(float.Lerp(from.M11, to.M11, amount),
+        float.Lerp(from.M12, to.M12, amount), float.Lerp(from.M13, to.M13, amount), float.Lerp(from.M14, to.M14, amount), float.Lerp(from.M15, to.M15, amount), float.Lerp(from.M21, to.M21, amount),
+        float.Lerp(from.M22, to.M22, amount), float.Lerp(from.M23, to.M23, amount), float.Lerp(from.M24, to.M24, amount), float.Lerp(from.M25, to.M25, amount), float.Lerp(from.M31, to.M31, amount),
+        float.Lerp(from.M32, to.M32, amount), float.Lerp(from.M33, to.M33, amount), float.Lerp(from.M34, to.M34, amount), float.Lerp(from.M35, to.M35, amount), float.Lerp(from.M41, to.M41, amount),
+        float.Lerp(from.M42, to.M42, amount), float.Lerp(from.M43, to.M43, amount), float.Lerp(from.M44, to.M44, amount), float.Lerp(from.M45, to.M45, amount));
+    
     public static ColorMatrix operator +(ColorMatrix left, ColorMatrix right) => new(left.M11 + right.M11,
         left.M12 + right.M12, left.M13 + right.M13, left.M14 + right.M14, left.M15 + right.M15, left.M21 + right.M21,
         left.M22 + right.M22, left.M23 + right.M23, left.M24 + right.M24, left.M25 + right.M25, left.M31 + right.M31,