It was a late night in 1968, and Warner Bros. executives filed into a theater in Santa Monica for the test screening of what they were sure would be its next big hit. After all, “Performance” — by first-time director Donald Cammell — promised rock star Mick Jagger in his first leading role, sultry Stones muse Anita Pallenberg as a glamorous groupie, a swinging London milieu and a groovy soundtrack by the leading man himself. The studio had invested $1.5 million on the picture, expecting a fun, sexy romp a la the Beatles’ loveable (and lucrative) “A Hard Day’s Night.”

So imagine the audience’s surprise when the film opened with a shot of a car driving through the dreary British countryside intercut with flashes of two naked bodies having rough, violent sex set to a harsh, wailing guitar.

What followed was an incomprehensible story involving a sleazy gangster (James Fox) who, on the run after committing a murder, ends up at the home of a washed-up former rock star (Jagger, who wouldn’t appear on screen until about 45 minutes into the flick). The grisly violence and louche sex proved too much for the crowd. One exec’s wife vomited into her handbag, or maybe it was her husband’s shoes — reports vary! Another stood up and shouted, “Are you going to sit here and watch this trash?” before exiting in a dramatic huff. The projectionist halted the film before it ended due to the pandemonium.

It would be 18 months before Warner Bros. would release “Performance,” dumping it into theaters on Aug. 3, 1970. Though it barely made a dent then, 50 years later it’s considered a cult hit. But according to “She’s a Rainbow,” Simon Wells’ new biography on Anita Pallenberg (Omnibus Press), out Tuesday, its production was even crazier than the film itself. And according to Wells, everyone attached — especially Pallenberg, a model who seemed on her way to film stardom — was “radically transformed” by the experience.

According to Wells, in 1966, the Stones were supposed to star in a film version of the dystopian teen novel “Only Lovers Left Alive,” with “Rebel Without a Cause” director Nicholas Ray taking the helm, but it never got made. Then there was “Maxigasm,” a sci-fi sexual odyssey penned by Tony Foutz and Sam Shepard specifically for the Rolling Stones, which stalled after getting through pre-production.

Then came Donald Cammell. The former painter-turned-screenwriter — and some would say, celebrity hanger-on — was already friendly with Jagger and had known Pallenberg (who by then had left Stones bad boy Brian Jones for bandmate Keith Richards) when she was a young model in Paris. Cammell’s two previous screenplays, “The Touchables” and “Duffy,” hadn’t made a ton of money, but they fused pop music, fame and crime in a way that intrigued Jagger. The singer agreed to star in his next project.

Cammell — “aided in no small way by the omnipresence of psychedelics,” writes Wells — penned a script that “would descend into a deep, labyrinthine odyssey where violence, sex and attendant humiliations would be realized in more existential realms.” Somehow, Warner Bros. — a mainstream studio — thought that financing this was a good idea.

“It makes you think that they never read the screenplay,” producer Sandy Lieberson would later tell the Irish Times. “You have to remember that it was the 1960s, and anything went then. So, strangely enough, it was relatively easy to raise the money.”

Plus, Warner Bros. were only too keen to greenlight a movie starring Jagger and getting a soundtrack, to boot. It took only 72 hours to sign the contracts.

Almost from the start there were problems. Jagger had never acted before and needed extensive training from his then-girlfriend Marianne Faithfull. Cammell wanted to direct the film himself, despite having no experience, and cinematographer Nicholas Roeg had to take charge to make sure everything was running smoothly. Faithfull had actually been considered for the part of Jagger’s lover, but then she got pregnant, so the role went to Pallenberg, who was also pregnant — with Richards’ child — and had an abortion so she could do the film. Richards wasn’t pleased, especially since the script called for lots of naked lovemaking with his bandmate, and so tried to bribe Pallenberg not to take the part.

“Keith would always say, ‘How much you gonna get for this film? I’ll give you the money — don’t do the film,’ ” Pallenberg said. “He didn’t understand that I wanted to do something that I wanted to do.”

Cammell had the designer create a claustrophobic, bohemian set for Jagger’s home, with an ornately tiled bathtub and a four-poster bed — which Pallenberg reportedly took home with her after the shoot — covered in exotic textiles and pelts.

It was “a psycho-sexual lab . . . a seething cauldron of diabolical ingredients: drugs, incestuous sexual relationships, role reversals, art and life all whipped together in a bitch’s brew,” Marianne Faithfull would write in her memoir.

The atmosphere created tension. Pallenberg was annoyed with Roeg’s meticulous attention to detail, complaining that he “would spend seven hours setting up one shot.”

“We’d sit huddled together in the basement, shivering, getting stoned and waiting for scenes we would eventually do maybe 28 times,” she added.

Because there was so much down time, drug use was rampant.

“You took one breath and you were stoned,” art director John Clark told Wells.

Pallenberg would openly mock Fox, the movie’s lead, for refusing to take part in the shenanigans.

“[He’d] be sitting there with the script every morning, studying [it],” she said. “We’d walk around smoking joints, just the opposite, just to annoy him.”

Then there were the film’s infamous threesomes, featuring Pallenberg, Jagger and a barely pubescent Michele Breton (who was 17 at the time). Camera operator Mike Molloy used a portable 16 mm camera to get the action under the covers, though Cammell and Roeg also “poked” around there too, per Wells.

“When I came up to reload the camera, Nic [Roeg] said, ‘Sod this, you’re having all the fun,’ and dived under bedclothes himself,” Molloy said.

“It was like a porno shoot, and Donald loved it,” Pallenberg would later say. “. . . It was actually quite uncomfortable for me to do this kind of thing with Jagger. Keith wasn’t pleased either. So it was very controversial at the time. It was not easy for us, and the way that Donald would carry on . . . it got very intense.”

It only took three days for rumors of an affair between Jagger and Pallenberg to get out. Richards was so paranoid that he hired the set’s drug dealer to spy on them. (Neither party would confirm or deny the affair.)

On her last day on set, Pallenberg had to film a scene — added at the last minute — in which her perpetually stoned character injects a shot of “Vitamin B12” into her bum. But it’s real heroin the actress is shooting up. She and Richards had just started dabbling in the stuff, so she was happy to go the extra mile to make her performance as authentic as possible.

Once the film wrapped, Roeg sent the footage to lab processors who — after seeing a few flashes of naked bodies — called the cinematographer saying they had to destroy it so they wouldn’t get an obscenity charge. Roeg managed to save most of the material, however.

After the test-screening disaster, Warner Bros. sent an extensive list of cuts to Cammell — hoping to avoid an X-rating. Cammell and Jagger fired back, writing, “This film is about the perverted love affair between Homo sapiens and Lady Violence. . . . To make such a film means accepting that the subject is loaded with every taboo in the book.”

When “Performance” finally hit US screens, it received scathing reviews. One critic called it “the most disgusting, the most completely worthless film I have seen since I began reviewing.” One headline blared “The Most Loathsome Film of All?” It was finally released in England, to somewhat kinder notices.

Now “Performance” is somewhat of a curio. It includes a magnetic performance by Pallenberg, who steals the film. And it helped launched Roeg’s career as a director — particularly of art films starring rock stars, like “The Man Who Fell to Earth” with David Bowie and “Bad Timing” with Art Garfunkel.

The movie also left a lasting impression on its cast and crew. Breton — who shared love scenes with Pallenberg and Jagger — never recovered from the experience and slipped into heroin addiction. Fox quit acting shortly after and became a Christian evangelist. And Cammell would direct a few more films before committing suicide in 1996. Pallenberg, meanwhile, became pregnant with her first child on set — and she and Richards would have two more children.

Pallenberg, who died in 2017, summed up the film best, calling it “Donald’s vision. He was notoriously into threesomes, rock stars and criminal violence. He injected all of his deviant sexual fantasies into the movie . . . [which] seems to me to be about the end of an era of hippie innocence, free love and sexual experimentation.”