To get to House of Rainbow you turn off the Apata Road from Ibadan at the Moco petrol station and you make your way down a rutted track through a cluttered market. Ibadan is Yoruba heartland, the region’s commercial hub and the home of what was once Africa’s most illustrious university. Here, on Ibadan’s western fringes, shanty-commerce rubs against gated residential compounds, and at the end of the track you enter one of these: three buildings, in various stages of distress, around a car park.

You knock on the door through the locked gate on the corner unit of one of the buildings; once you are appraised through the keyhole the gate will be unlocked and you will enter a living room with white plastic chairs set in makeshift pews around an altar, beneath a rainbow banner with the word “PEACE” painted on to it.

Pastor Jude Onwambor – a furniture-salesman by day – will remind you that while you are to “comport yourselves decently” on arrival, you are now free to take your feminine attire out of your bags and become your true selves, the “daughters” or “darlings” (dar-liiiiiiiiiiiings!) you really are.

Pastor Jude, in his 30s, is genial and taciturn; his deputy, Ayo, a decade younger, is a motor-mouthed rap poet, wiry and explosive: “Peace! Do not be afraid! You are not going to die!” he exclaimed during his sermon the day I visited, in October 2014. He was citing Judges 6 23. “As we listen to these words today, we feel the peace that has been lacking in our minds,” he said in his sermon. It was a few months after Nigeria’s Same-Sex Marriage (Prohibition) Act had been signed into law by its president, Goodluck Jonathan. This is the world’s harshest anti-gay law outside of Islamic sharia: there are mandatory sentences of 14 years for any kind of “homosexual behaviour” and you can be prosecuted for not turning in a gay neighbour, friend or family member.

“If ever anyone is telling you you’re not perfect, tell them that God made you,” Ayo continued, “and God does not make mistakes. He says, ‘I’ve selected you to be a prophet, to be different, to be unique.’ Tell your neighbour, ‘I’m unique!’”

“I’m unique!” the congregants – young men and “TVs”, as transgender women called themselves – murmured to one another, a quiet ripple of self-affirmation across the room.

“We are not the only ones passing through this,” Ayo concluded his sermon, the day I visited. “In Uganda they’re facing the same thing. In Russia they’re facing the same thing. So I encourage us to be calm, to be focused on our education, our work, our health status, to value whatever we’re doing very well. God will grant us our peace.”

House of Rainbow has been around since 2005, when a legendary activist named Jide Macaulay started the first chapter in Lagos, Nigeria’s mega-city 130km to the south of Ibadan. Two years later he was forced to leave Nigeria after he came out in the local media, and he is now an ordained Anglican deacon in Britain. With funding he had raised, Reverend Jide – as he was universally known – rented this flat, one of several underground House of Rainbow “local groups” in Nigerian cities.

The more rights are gained by minorities in parts of the world, the stronger the backlash is against them in others

The men I met that day had horror stories, from even before the new law had been passed. Charles, a young physiotherapy aide in tight lemon slacks and a frilly vest now that he was inside, used male pronouns but understood himself to be a “TV”, and dreamed of gender transition somewhere, somehow. He was discovered having sex with a friend, he told me, and subjected to a 40-day deliverance process in the family church that involved being beaten repeatedly with a broom. “If you try to kill the gay in me, you will have to kill me,” he had eventually said to his mother.

Charles’ friend Desmond, lithe and handsome, affected an area-boy (street-thug) style and boasted of his conquests. He had been arrested twice: frame-ups that were resolved through the paying of a bribe. A few months after I met him Desmond would be arrested for a third time, one of 21 young men attending a birthday party. Following a complaint by a neighbor there was a police raid: they found 122 condoms in a bag belonging to an HIV peer-educator, and on the basis of this and the fact that there were no women present, they arrested the men on suspicion of being part of a “gay cult”. Faced with the prospect of 14 years’ imprisonment, the men or their families found the funds needed to bribe their captors, between 10,000 and 25,000 naira each (between US$60 and US$150).

Although there were almost no prosecutions under the new law, enacting it had – Jide Macaulay told me – “broken into the very homes of LGBTI people”, because of the way “it permits family to snitch on each other” and “makes things impossible in health, in the workplace, in housing. Anyone has an excuse to deny you housing, or treatment, or a roof over your head. In Nigeria you have to put down a year’s deposit on a place: the landlord finds out you’re gay, he chucks you out and keeps the money. What are you going to do?”

A 2016 Human Rights Watch report found that the law’s impact was “far-reaching and severe”: it had become “a tool used by some police officers and members of the public to legitimise multiple human rights violations perpetrated against LGBTI people”, including “torture, sexual violence, arbitrary detention, violations of due process rights, and extortion”. HRW was particularly concerned about “rising incidents of mob violence” against suspected homosexuals.

A few weeks after Goodluck Jonathan signed the Same-Sex Marriage (Prohibition) Act into law in early 2014, the United Kingdom (excepting Northern Ireland) became the 14th country in the world to permit same-sex couples to wed, and the third in the Commonwealth after Canada and South Africa. Queen Elizabeth had given her “royal assent” to this. And yet in 36 of the 52 states of the Commonwealth, the association of former British colonies which the Queen headed, homosexuality remained illegal. And in early 2018 the British overseas territory of Bermuda actually re-criminalised same-sex unions a year after its supreme court had legalised them.

In the 21st century, there are two parts of the world where the law still generally criminalises male homosexual sex: Muslim countries and former colonies of the British empire. And in some of these latter countries, including Nigeria and Uganda, the original penal code proscriptions inherited from Britain have being strengthened by harsh new legislation. In colonial times, Cecil John Rhodes vowed to paint a line in pink – the colour of British dominion, on the maps – from the Cape to Cairo; now, a century later, a new pink line seems to be being drawn across the globe, a new human rights frontier around sexuality and gender identity that divides and describes the world in an entirely new way. In the past few years a troubling new global equation seems to have come into play: the more rights are gained by sexual and gender minorities in some parts of the world, the stronger the backlash is against them in others.

The irony is that it was the British empire that criminalised homosexuality in the first place. The original colonial penal code proscribing “carnal intercourse against the order of nature” was written by Thomas Macaulay for India, passed into law in 1860. Sir Samuel Griffith’s Queensland penal code, passed in 1899, criminalised both the active and the passive participants in such acts and widened the definition of them beyond “penetration”. Between them, these two codes provided the template for most of Britain’s colonies, with the Queensland law particularly influential in African colonies.

In Africa and Asia, the British were preoccupied with what the legal scholar Alok Gupta has termed “fears of moral infection” from the natives, and with the mission of “moral reform” among these new subjects. In Australia, “same-sex relationships among convicts were considered the most serious threat to lawful authority” the marriage-equality activist Rodney Croome said in the Guardian in October 2017. This legacy of “convictism”, Croome believes, is the best way to understand Australia’s resistance to LGBTI equality well into the 21st century. Homosexual acts were only fully decriminalised in South Australia in 1975, and Queensland itself only followed suit 15 years later, in 1990. Tasmania was forced to repeal its sodomy law in 1997, after a landmark case taken to the United Nations human rights committee.

People who identify themselves as members of the LGBT community parade in Entebbe, Uganda, in 2015. Photograph: Reuters

Because the shedding of empire preceded Britain’s decriminalisation of homosexuality in 1967, newly independent countries in Africa, Asia and the Caribbean maintained the laws that criminalised homosexuality, although they were seldom enforced before LGBTI rights began to be asserted in the 21st century. The irony, of course, was that many of these countries were using colonial legislation to back up their claims that homosexuality was unacceptable, and that the demand for its decriminalisation was a neocolonial slight on their sovereignty.

The British prime minister David Cameron did not help things when he said, at the Perth Commonwealth heads of government meeting in October 2011, that British aid to countries should now be conditional on their decriminalisation of homosexuality.

African governments and commentators responded with fury. A Ugandan presidential spokesman described Cameron’s remarks as “patronising, colonial rhetoric” and the Tanzanian foreign minister fumed: “We have our own moral values. Homosexuality is not part of our culture and we will never legalise it … We are not ready to allow any rich nation to give us aid based on unacceptable conditions simply because we are poor.”

African LGBTI activists reported a significant uptick in public homophobia in their home countries as a consequence of the backlash against Cameron’s statement. For this reason, activists are treading gingerly at the moment, in the run-up to the Commonwealth heads of government meeting in London next month. “Every effort is being made to get other leaders to raise the issue rather than just Theresa May and Justin Trudeau,” Paul Dillane, the executive director of the British Kaleidoscope Trust told me, “to break the cycle of this being seen as a ‘northern’ preoccupation.”

Kaleidocope convenes the Commonwealth Equality Network, comprised of LGBTI organisations from 41 countries, that lobbies within the body for legal reform. The network is pinning its hopes on Malta’s Joseph Muscat: the country had become a global leader on the issue (it was the first European country to protect LGBTI rights explicitly in its constitution), and at the 2015 Chogm he hosted, Muscat used the platform to argue strongly for decriminalisation. The new Commonwealth secretary general, Baroness (Patricia) Scotland – a British woman of West Indian descent – has also vowed to take the issue up: in a 2017 interview she said: “We are working alongside countries, making an economic case for inclusion [of LGBTI people and rights].”

The “economic argument”, Dillane believes, has proven to be the winning one thus far, in Mozambique, Seychelles and Nauru, the Commonwealth countries that have voluntarily decriminalised homosexuality in the past three years: the costs of policing and jailing otherwise law-abiding citizens versus the benefits of tourism, reputation, the ability to attract foreign talent and to develop local human resources fully. A fourth Commonwealth country, Belize, has been compelled to decriminalise homosexuality after a successful supreme court appeal, and, in late 2017, a similar appeal was being prepared in Trinidad and Tobago. There were also ongoing legal appeals and policy reforms in several African Commonwealth countries, most notably Kenya, Malawi, Namibia and Botswana – not around decriminalisation per se, but rather the rights to equality and protection from discrimination.

In Nigeria, there are no such prospects for reform.

Shortly after I visited Ibadan, the House of Rainbow operation there shut down because it had become, Jude Onwambor told me, “a security risk”. It would be “much safer working under the cover of public health”. And so he had set up a new organisation that did Aids-awareness work. In 2017 he opened a drop-in centre that functioned as a community gathering place in the centre of town. As with the church, the “comportment” rules prevailed: arrive properly but be yourselves when inside.

Still, the Aids epidemic only heightened many African countries’ dependence on the west, and in this context a new impetus to fight the “neocolonialism” of development aid emerged. As Africans became increasingly uncomfortable with their countries’ dependence on the west, some looked to a different way to assert their pride: despite their poverty, at least they had values.

The battle against gay rights became the surest way of rubbing salt in the colonial wound, of drawing a pink line between the proud Africans – Christian and Muslim – who wished to save their “traditional values”’ and protect their “cultural sovereignty” against rapacious neocolonialists who not only had lost their moral compass in the rapidly secularising west, but whose call for rights threatened the status quo. The African homophobia of the 21st century is thus a toxic mixture of religion and politics, fuelled by the Aids epidemic and the stigmas associated with it.

Now there’s a new generation that says, ‘We exist!’ … They are the fearless generation. There will be consequences Jide Macaulay

Nowhere is this more visible than in Nigeria. On 30 July 2017, 42 men were arrested at the Vintage hotel in northern Lagos. They were attending a monthly party; a fight had broken out and the police had been called and detained everyone present, having caught them “in the act”, as a news report put it. “In the act”, of course, of being gathered together as suspected homosexuals: under the terms of the new legislation no other other evidence was necessary. After their bails had been posted (and their names and faces splashed all over the media), the national Sun newspaper led with the headline: HIV epidemic looms. The author, Chioma Igbokwe, reported that “99%” of the men had tested HIV-positive in jail, “and now roam freely in the society” as a result of the “watering down of the bail condition – for an offence that is one of the gravest in the country’s criminal code”. Igbokwe opined that it was “not far-fetched” that they could be “infecting new persons by the day” given the “propensity among some of them to suddenly go on an infection-spree mission”.

Ranged against this hate speech, though, is an extraordinary new generation of outspoken queer Nigerian voices: people like Richard Akuson, whose online magazine A Nasty Boy is dedicated to “otherness in fashion, people and culture”, or Chibuihe Obi, who was abducted and tortured for several weeks after publishing an essay called ‘We’re Queer, We’re Here’ in Brittle Paper, an online journal that has become the hub of this new queer literary scene.

Jide Macaulay, House of Rainbow’s founder, tells me he is both “thrilled” and “terrified” by the advent of such outspoken voices. Of the 10 African countries in which he works, his native land is “by far the most challenging”, he says. “But now there’s a new generation that says, ‘We exist!’ Of people being pushed too far and now fed up: ‘This is who I am! Kill me if you want.’” It is a powder keg: “They are the fearless generation. There will be consequences.”

Is it coincidence that this efflorescence is occurring in the very years of the Same-Sex Marriage (Prohibition) Act? “I certainly do not think of those laws every time I sit down to write,” Brittle Paper’s submissions editor, Otosirieze Obi-Young, tells me. Still, he believes the “ferocity” of his generation’s writing is a product of two things: the state-licensed homophobia they have encountered, and the ability they have to express themselves – and connect – on the newly available broadbands of social media.

Thus do they live on the pink line, these young Nigerians, toggling between their new virtual freedom and the dangers of life offline. For the first time they are present, and visible. As Chibuihe Obi puts it, “Fnally, we are here.”

• This is an edited extract from the Griffith Review 59: Commonwealth Now