C# FunctionSequence

posted in Beals Software
Published May 23, 2010
Advertisement
I was screwing around with image manipulation (just for fun) and came up with a class to sequence functions:
public sealed class FunctionSequence{    List> functions = new List>();    public int Count    {        get        {            return functions.Count;        }    }    public FunctionSequence(params Func[] Funcs)    {        functions.AddRange(Funcs);    }    public void Add(Func Item)    {        functions.Add(Item);    }    public void AddRange(IEnumerable> Collection)    {        functions.AddRange(Collection);    }    public void Insert(int Index, Func Item)    {        functions.Insert(Index, Item);    }    public bool Remove(Func Item)    {        return functions.Remove(Item);    }    public void RemoveAt(int Index)    {        functions.RemoveAt(Index);    }    public void Clear()    {        functions.Clear();    }    public TValueType Invoke(TValueType Input)    {        TValueType Temp = Input;        foreach(var Function in functions)        Temp = Function(Temp);        return Temp;    }    public static implicit operator Func(FunctionSequence Sequence)    {        return new Func(Input =>        {            return Sequence.Invoke(Input);        });    }}


And an example usage:
System.Drawing.Color GrayscaleShader(System.Drawing.Color Color){    float Value = (0.299f * (float)Color.R) + (0.587f * (float)Color.G) + (0.114f * (float)Color.B);    return System.Drawing.Color.FromArgb(Color.A, (int)Value, (int)Value, (int)Value);}System.Drawing.Color SepiaShader(System.Drawing.Color Color){    return System.Drawing.Color.FromArgb(Color.A, Color.R + 40, Color.G + 20, Color.B);}Func ToSepia = (Func)new FunctionSequence(GrayscaleShader, SepiaShader);System.Drawing.Color RedAsSepia = ToSepia(System.Drawing.Color.Red);


I can't really see any decent use for this, but I was just happy it worked like it was supposed to as I'm still new to lambda functions and expressions.

And as I was typing this, it becomes apparent to me that:
new Func(Input =>    {        Input = GrayscaleShader(Input);        Input = SepiaShader(Input);        return Input;    });

would be a much simpler solution and would have taken much less time XD. Oh well!
Previous Entry Logo Concepts
Next Entry Still Here
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement