Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualAmzBee

Posted 24 June 2012 - 02:32 AM

Another excuse to use System.Linq Posted Image

public Color GetAvgColor(Texture2D tex, Rectangle rect, int mipLevel = 0)
{
	Color[] data = new Color[rect.Width * rect.Height];
	if (data.Length == 0) return Color.White;
	tex.GetData<Color>(mipLevel, rect, data, 0, data.Length);
	int r = data.Sum(t => t.R) / data.Length;
	int g = data.Sum(t => t.G) / data.Length;
	int b = data.Sum(t => t.B) / data.Length;
	int a = data.Sum(t => t.A) / data.Length;
	return new Color(r, g, b, a);
}

edit with a bit of explanation: the 3rd overload of the GetData allows you to specify a rectangle to retrieve colours from, so you simply have to make sure your data array is the same size as what your after returning.

#1AmzBee

Posted 24 June 2012 - 02:30 AM

Another excuse to use System.Linq :D

public Color GetAvgColor(Texture2D tex, Rectangle rect, int mipLevel = 0)
{
    Color[] data = new Color[rect.Width * rect.Height];
    if (data.Length == 0) return Color.White;
    tex.GetData<Color>(mipLevel, rect, data, 0, data.Length);
    int r = data.Sum(t => t.R) / data.Length;
    int g = data.Sum(t => t.G) / data.Length;
    int b = data.Sum(t => t.B) / data.Length;
    int a = data.Sum(t => t.A) / data.Length;
    return new Color(r, g, b, a);
}

PARTNERS