Each colour's relative luminance is calculated as WCAG defines it: the sRGB channels are converted to linear light and weighted 0.2126 red, 0.7152 green and 0.0722 blue.
The contrast ratio is (lighter + 0.05) ÷ (darker + 0.05), from 1:1 for identical colours to 21:1 for black on white. Which colour is text and which is background does not change it.
Thresholds are the WCAG 2.2 success criteria: 4.5:1 for normal text and 3:1 for large text at level AA (1.4.3), 7:1 and 4.5:1 at AAA (1.4.6), and 3:1 for interface components and meaningful graphics (1.4.11).
Limitations
A ratio is a property of two flat colours. Text over a gradient, an image or a translucent layer has to be checked at its worst point, which this cannot see.
Passing the ratio does not make text readable: thin weights, small sizes and long lines fail readers at ratios that pass.
The WCAG 2 formula is known to misjudge some pairs, especially light text on mid-tone backgrounds and dark-mode schemes. The draft WCAG 3 work explores a different measure, but it is not a standard and does not replace these thresholds.
Contrast is one requirement among several. Colour must also never be the only way information is conveyed.