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