That sounds like the best way to do it that I can think of.
You can save some effory by using textWidth to make sure your PGraphics(2,3,GL) aren't way too big/small so you only generate the minimum data required to do it.
Just knocked this up quickly, seems to work like you'd probably expect:
Code:PGraphics3 grad;
PGraphics3 txt;
PFont f;
PImage res;
void setup()
{
size(400,200);
f=createFont("Arial",32);
textFont(f,32);
float w=textWidth("MyText");
PGraphics3 grad=new PGraphics3((int)w,50,null);
PGraphics3 txt=new PGraphics3((int)w,50,null);
grad.defaults();
txt.defaults();
grad.background(random(255));
for(int i=0;i<200;i++)
{
grad.stroke(random(255),random(255),random(255));
grad.line(random(grad.width),random(grad.height),random(grad.width),random(grad.height));
}
txt.background(0);
txt.fill(255);
txt.textFont(f,32); // this may not be needed, I don't know
txt.text("MyText",0,32);
res=grad;
res.mask(txt);
}
void draw()
{
background(255);
image(res,width/2,height/2);
}
It's probably wrong in some respects, but gives the effecty you're looking for.
(You'll have to change the fill/random stroke part to do hatever you want to get the gradient.