Skip to content

Instantly share code, notes, and snippets.

@JimBobSquarePants
Created January 4, 2016 14:20
Show Gist options
  • Select an option

  • Save JimBobSquarePants/ef9e482470dde4967ba1 to your computer and use it in GitHub Desktop.

Select an option

Save JimBobSquarePants/ef9e482470dde4967ba1 to your computer and use it in GitHub Desktop.
Generic operators
public static Color<T> operator +(Color<T> left, Color<T> right)
{
if (typeof(T) == typeof(byte))
{
return (Color<T>)(object)new Color<byte>(
(byte)((byte)(object)left.R + (byte)(object)right.R),
(byte)((byte)(object)left.G + (byte)(object)right.G),
(byte)((byte)(object)left.B + (byte)(object)right.B),
(byte)((byte)(object)left.A + (byte)(object)right.A));
}
// etc
}
@JimBobSquarePants
Copy link
Copy Markdown
Author

Double cast is required due to C# widening when adding to bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment