Преглед изворни кода

Average grayscale color filter

CPKreuz пре 1 година
родитељ
комит
2c84b8ec98

+ 18 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Filters.cs

@@ -5,18 +5,36 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public static class Filters
 {
+    /// <summary>
+    /// Maps red to the red, green and blue channels. Sets alpha to 1
+    /// </summary>
     public static readonly ColorFilter RedGrayscaleFilter =
         ColorFilter.CreateColorMatrix(
             ColorMatrix.UseRed + ColorMatrix.MapRedToGreenBlue + ColorMatrix.OpaqueAlphaOffset);
 
+    /// <summary>
+    /// Maps green to the red, green and blue channels. Sets alpha to 1
+    /// </summary>
     public static readonly ColorFilter GreenGrayscaleFilter =
         ColorFilter.CreateColorMatrix(ColorMatrix.UseGreen + ColorMatrix.MapGreenToRedBlue +
                                       ColorMatrix.OpaqueAlphaOffset);
 
+    /// <summary>
+    /// Maps blue to the red, green and blue channels. Sets alpha to 1
+    /// </summary>
     public static readonly ColorFilter BlueGrayscaleFilter =
         ColorFilter.CreateColorMatrix(ColorMatrix.UseBlue + ColorMatrix.MapBlueToRedGreen +
                                       ColorMatrix.OpaqueAlphaOffset);
 
+    /// <summary>
+    /// Maps alpha to the red, green and blue channels. Sets alpha to 1
+    /// </summary>
     public static readonly ColorFilter AlphaGrayscaleFilter =
         ColorFilter.CreateColorMatrix(ColorMatrix.MapAlphaToRedGreenBlue + ColorMatrix.OpaqueAlphaOffset);
+    
+    /// <summary>
+    /// The rgb values become averaged into a grayscale image. Sets alpha to 1 <br/>
+    /// </summary>
+    public static readonly ColorFilter AverageGrayscaleFilter =
+        ColorFilter.CreateColorMatrix(ColorMatrix.AverageGrayscale + ColorMatrix.OpaqueAlphaOffset);
 }

+ 12 - 1
src/PixiEditor.Numerics/ColorMatrix.cs

@@ -121,7 +121,7 @@ public record struct ColorMatrix
         (0, 0, 1, 0, 0),
         (0, 0, 0, 0, 0)
     );
-    
+
     /// <summary>
     /// The alpha value will stay the alpha value <br/>
     /// (_, _, _, w) => (0, 0, 0, w)
@@ -139,6 +139,17 @@ public record struct ColorMatrix
     /// </summary>
     public static ColorMatrix OpaqueAlphaOffset => Offset(0, 0, 0, 1);
     
+    /// <summary>
+    /// The rgb values become averaged into a grayscale image. Alpha becomes zero <br/>
+    /// (r, g, b, _) => (r, g, b, 0) / 3
+    /// </summary>
+    public static ColorMatrix AverageGrayscale => new(
+        (1 / 3f, 1 / 3f, 1 / 3f, 0, 0),
+        (1 / 3f, 1 / 3f, 1 / 3f, 0, 0),
+        (1 / 3f, 1 / 3f, 1 / 3f, 0, 0),
+        (0, 0, 0, 0, 0)
+    );
+
     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,