Take the hat in an outside grip. Stretch the leg out, flex the foot and place the far side of the brim on the thigh. Gently flick the brim up and allow it to roll down the leg to land on the foot.
Practice a few times to get the right release position and this trick will then just work automatically.
As a variation try placing the hat crown down (using an inside grip) further down the leg. If you have the upper side of the brim in contact with the leg it will simply roll to foot as soon as you let go.