If you ctrl+click the layer, all the opaque pixels will be selected. Then, you can apply your gradient and only the selection will be affected.
If you ctrl+click the layer, all the opaque pixels will be selected. Then, you can apply your gradient and only the selection will be affected.