StandardizedDiffuseAlbedoMaps.CalibratedTexture.ComputeAverageSwatchColor C# (CSharp) Method

ComputeAverageSwatchColor() public method

Computes the average color within a rectangle in UV space
public ComputeAverageSwatchColor ( ImageUtility _TopLeft, ImageUtility _BottomRight ) : ImageUtility.float3
_TopLeft ImageUtility The top left corner (in UV space) of the rectangle to sample
_BottomRight ImageUtility The bottom right corner (in UV space) of the rectangle to sample
return ImageUtility.float3
        public ImageUtility.float3 ComputeAverageSwatchColor( ImageUtility.float2 _TopLeft, ImageUtility.float2 _BottomRight )
        {
            // Average xyY values in the specified rectangle
            int		X0 = Math.Max( 0, Math.Min( m_Texture.Width-1, (int) Math.Floor( _TopLeft.x * m_Texture.Width ) ) );
            int		Y0 = Math.Max( 0, Math.Min( m_Texture.Height-1, (int) Math.Floor( _TopLeft.y * m_Texture.Height ) ) );
            int		X1 = Math.Min( m_Texture.Width, Math.Max( X0+1, (int) Math.Floor( _BottomRight.x * m_Texture.Width ) ) );
            int		Y1 = Math.Min( m_Texture.Height, Math.Max( Y0+1, (int) Math.Floor( _BottomRight.y * m_Texture.Height ) ) );
            int		W = X1 - X0;
            int		H = Y1 - Y0;

            ImageUtility.float4	AverageXYZ = new ImageUtility.float4( 0, 0, 0, 0 );
            for ( int Y=Y0; Y < Y1; Y++ )
                for ( int X=X0; X < X1; X++ )
                {
                    ImageUtility.float4	XYZ = m_Texture.ContentXYZ[X,Y];
                    AverageXYZ += XYZ;
                }
            AverageXYZ = (1.0f / (W*H)) * AverageXYZ;

            ImageUtility.float3	xyY =  ImageUtility.ColorProfile.XYZ2xyY( (ImageUtility.float3) AverageXYZ );
            return xyY;
        }