Terrain Heights to texture

I want to sample the Terrain heights to a Texture for a custom Editor tool and having some problems.
What ever I do the Texture is always black.
here is a screenshot and the relevant code:

private TerrainData Data
{
	get
	{
		return _data;
	}
	set
	{
		// if the value (TerrainData) has changed re-/ sample the Texture
		if( value != _data )
			SampleTexture( value.GetHeights( 0, 0, value.heightmapWidth, value.heightmapHeight ), value.heightmapWidth, value.heightmapHeight );

		_data = value;
	}
}

private void SampleTexture( float[,] heightMap, int width, int height )
{
	_texture = new Texture2D( width, height, TextureFormat.RGB24, false );
	_texture.wrapMode = TextureWrapMode.Clamp;

	var colors = new Color[ heightMap.Length ];

	for( int x = 0; x < width; x++ )
		for( int y = 0; y < height; y++ )
			colors[x + y] = Color.Lerp( Color.black, Color.white, heightMap[x, y] );

	_texture.SetPixels( colors );
	_texture.Apply();
		
}

// GUI function
protected override void DrawNodeContent()
{
	GUILayout.Label("TerrainData");
	Data = (TerrainData)EditorGUILayout.ObjectField( Data, typeof( TerrainData ), false );
	_scroll = GUILayout.BeginScrollView( _scroll );
			
	if( _texture != null )
	{
		GUILayout.Box( _texture );
		//GUI.DrawTexture( new Rect( rect.x, rect.y, 100, 100 ), _texture, ScaleMode.ScaleAndCrop, false );
	}

	GUILayout.EndScrollView();
}

Anyone having an idea whats going wrong?

EDIT: solved. I did the loop wrong. for every one who is curious

for( int x = 0; x < width; x++ )
	for( int y = 0; y < height; y++ )
		colors[x * height + y] = Color.Lerp( Color.black, Color.white, heightMap[x, y] );

Color is a RGBA, and you create a texture in RGB format, so that might be a problem. Also, your line 26, I’m not sure what you’re trying to do with the x+y expression, that won’t access every pixel in the texture. Next, you are sampling the height map. What range of values does it store? You’re using Color.Lerp, so I guess you assume the height map stores values between zero and one.

Edited the post while you answered.
Yeah the problem was that I indexed the colors array wrong as you mentioned. Heightmap values are indeed in 0-1 range.
Works all fine know