Yeah, that colour formula has the problem that it doesn't compensate for how red, green, and blue suck up different amounts of the luminosity spectrum. That's why the formula used by the WCAG (web content accessibility guidelines) can be a bit... complicated.
Thus most web dev's use this tool:
https://webaim.org/resources/contrastchecker/
Because the formula used there kinda sucks to implrment by hand, when I need a quick and dirty version, I use the Y (luma) component of YCbCr.
Y = 0.299 * R + 0.587 * G + 0.114 * B. It's what's actually in the EGA/VGA specification for monochrome conversion, and was used back in the day on an interface guidelines set up as a joint venture between IBM, Apple, and Microsoft.
If the Y between the two less than 70% the range, it's probably not good in the age of webfonts and font-smoothing. If it's less than 50% it's guaranteed to be broken inaccessible junk.
Thus why colors on the middle 50-70% of Y are considered unusable in designs as they're incapable of hitting that contrast range.
I'm slowly in my spare time (I have way too many plates spinning) putting together a tool that lists both the WCAG standard for contrast and the YCrCb method.
You can't just subtract one from the other because of the fact that blue is only 11.4% of visible luminosity, red is only 29.9%, and green making up the lions share at 58.7%. This is why the old green screens actually used to cause eye strain for a lot of the population, whilst amber was far better. Full bright pure green was borderline, but adding in a little red to shift it towards orange-yellow dragged it up into the 70% range.
Whist a full intensity blue on black is utterly useless to almost half the population, just as yellow on white is. Even pure red on black doesn't even make 30% contrast, so a lot of people can't read it and it causes headaches for even more.
The way your calculation works is utterly banjaxed, as you're basically making red be weighted as 256 times stronger than green, and green weighted as 256 times stronger than blue. (thus red is 65536 times stronger than blue). Can you see the problem with that? Just converting the hex to integer is NOT representative of the colour's luminance. You need to extract each two-digit colour component and scale them appropriately.
Understanding colourspace is... tricky. Especially when emissive (displayS) and reflective (print) operate on different rules 60 degrees off from each-other on the colour wheel. Converting between colour spaces can get you into some hardcore algebra.
I don't have a telegram account, though I think I have a discord login. I really don't use either a whole lot though, but I'll see about getting that set up. I'm always glad to teach others what I can.
Oh, and I am getting a scrollbar when the window is too small in Chrome, but I test in FF and it's broken there. Flex with an auto-margin on the inner content would in all likelihood be a better choice, only the other element being fixed.