diff --git a/src/Artemis.VisualScripting/Nodes/Color/AverageColorNode.cs b/src/Artemis.VisualScripting/Nodes/Color/AverageColorNode.cs new file mode 100644 index 000000000..f4ec35657 --- /dev/null +++ b/src/Artemis.VisualScripting/Nodes/Color/AverageColorNode.cs @@ -0,0 +1,48 @@ +using Artemis.Core; +using SkiaSharp; + +namespace Artemis.VisualScripting.Nodes.Color; + +[Node("Average color", "Calculate the average if all colors in the image", "Image", InputType = typeof(SKBitmap), OutputType = typeof(SKColor))] +public class AverageColorNode : Node +{ + #region Properties & Fields + + public InputPin Image { get; set; } + + public OutputPin Average { get; set; } + + #endregion + + #region Constructors + + public AverageColorNode() + : base("Average color", "Calculate the average if all colors in the image") + { + Image = CreateInputPin(); + Average = CreateOutputPin(); + } + + #endregion + + #region Methods + + public override void Evaluate() + { + SKBitmap? image = Image.Value; + if (image == null) return; + + Span colors = image.Pixels; + int r = 0, g = 0, b = 0; + foreach (SKColor color in colors) + { + r += color.Red; + g += color.Green; + b += color.Blue; + } + + Average.Value = new SKColor((byte)(r / colors.Length), (byte)(g / colors.Length), (byte)(b / colors.Length)); + } + + #endregion +} \ No newline at end of file