Just spent an hour putting this together. It’s a member function for a Mesh class but you could pass in the Mesh. The name is misleading as it’s not actually doing a fill, it’s just colouring the vertices – it looks like a fill though.
It should work with any sort of Mesh, including rodrigo’s MeshRoundRect or an image with a Scale9grid. You supply the angle, relative to the x-axis, in radians and two colours.
It works like a gradient fill in an art package where you specify the direction of fill. The two colours are the two extremes of the gradient, which goes from one extreme of the Mesh to the other, typically corner to corner or edge to edge.
Note it’s not likely to be very fast, querying and setting every vertex individually, but I’ve not tried any performance tests. It works with Meshes with alpha, as the setColor function leaves the alpha in place (it reads it to do so which also makes it slow to call repeatedly).
private static const aDistance:Vector.<Number> = new Vector.<Number>();
private static const pt:Point = new Point();
public function gradientFill(bearing:Number, col0:uint, col1:uint):void {
var fCos:Number, fSin:Number, fDist:Number, fMin:Number, fMax:Number;
var fR0:Number, fR1:Number, fG0:Number, fG1:Number, fB0:Number, fB1:Number;
var iNum:int, iVertex:int, vData:VertexData;
var fScale:Number, fR:Number, fG:Number, fB:Number;
vData = vertexData;
iNum = vData.numVertices;
aDistance.length = iNum;
fCos = Math.cos(bearing);
fSin = Math.sin(bearing);
fMin = 1000000000;
fMax = -fMin;
for (iVertex = 0; iVertex < iNum; iVertex++) {
vData.getPoint(iVertex, "position", pt);
fDist = pt.x * fCos + pt.y * fSin;
fMin = MathUtil.min(fDist, fMin);
fMax = MathUtil.max(fDist, fMax);
aDistance[iVertex] = fDist;
}
fR0 = (col0 >> 16) / 256.0;
fG0 = ((col0 >> 8) & 0xff) / 256.0;
fB0 = (col0 & 0xff) / 256.0;
fR1 = (col1 >> 16) / 256.0;
fG1 = ((col1 >> 8) & 0xff) / 256.0;
fB1 = (col1 & 0xff) / 256.0;
for (iVertex = 0; iVertex < iNum; iVertex++) {
fDist = aDistance[iVertex];
fScale = (fDist - fMin) / (fMax - fMin);
fR = fScale * fR0 + (1 - fScale) * fR1;
fG = fScale * fG0 + (1 - fScale) * fG1;
fB = fScale * fB0 + (1 - fScale) * fB1;
col0 = (int(fR * 256.0) << 16) + (int(fG * 256.0) << 8) + int(fB * 256.0);
vData.setColor(iVertex, "color", col0);
}
}