Last week, I wrote an article about using Flash 8 Perlin noise to generate a wood texture. Today, it's marble.
According to this link, the Perlin formula for marble is:
texture = cosine( x + perlin(x,y,z) )
Again, we don't want to do math on every pixel -- too slow. So, how do we do this with the existing Flash 8 API? First, let's pretend that the perlin term in the above equation is not there. Here's what our image would look like (this image is arbitrarily blue monochrome):
One pixel scan line of the above image was generated with Math.cos() and BitmapData.setPixel(). The bitmap was then stretched vertially -- no need to call setPixel on w * h pixels -- w x 1 is enough.
The perlin term randomly distorts the phase. In the context of the above image, this means pixels are randomly displaced horizontally. A DisplacementMapFilter driven by perlin is exactly what we need:
Then we can map the colors to realistic marble colors using ColorMatrixFilter:
I find the sinusoid basis for the marble to be too regular. We can use perlin noise instead of a sinusoid and our pre-DisplacementMapFilter image looks like this:
You can play with the settings here. BTW, I find it takes a lot of trial-and-error to find parameters that result in a decent marble.

Here's the code:
Here are a few generated marbles.

