all parents of a transform

i want to chance the material color the parents of a transform. what is i need to use on for

this should do it (should be valid in js and c#)

var currentParent = transform.parent;
while(currentParent != null)
{
    var parentRenderer = currentParent.renderer;
    if (parentRenderer != null)
    {
        //do something with parentRenderer
    }
    currentParent = currentParent.parent;
}