Kesha "begged" rehab staff to allow her to have an instrument and wrote song Rainbow in one hour.
The 30-year-old singer and songwriter, who was in rehab for an eating disorder in 2014, opened up about the experience in a piece she wrote for Refinery29 and confessed she was in a "very dark place" and "wildly out of control".
The Dirty Love singer revealed she had "begged" staff at the facility to allow her to have an instrument. When she was granted an hour with a keyboard her boyfriend Brad had sent her, she penned the track Rainbow.
"The whole album idea and tour and everything, came from me crying and singing and playing and dreaming until my hour was up and they took the keyboard away again," she wrote.
"Every day I would just cry and play that song because I knew I had to get through that incredibly hard time. I knew I had to change and learn to take care of and love myself, and I had no idea how to even begin."
She revealed that Rainbow was only the beginning and writing the songs was her way of making a promise to herself that would look after herself from then onwards.
She confessed that holding onto the idea that she was going to put out the Rainbow album helped her survive rehab and believed it "saved" her life.
"I wanted to call the album Rainbow because after the storm, there's a rainbow - and recently, I feel like I've gone through some things that have felt like a storm in my life," she added.
"This was my way of telling myself that I was going to make it through.
"I used to be very mean to myself. Rainbow is my letter of encouragement, a promise that I want to start a new dialogue and be more supportive and nicer to myself."