Okay so let's say I have a group-chat and the owner is able to change the background color of the chat-bubbles. The usernames on top of the bubble have different colors which are assigned to them from a list with 10 colors when the group chat gets generated. That means some of the 10 colors in the list would fit with the background, some wouldn't and would need an adjustment so the readability is good again. And since the background color is not always the same, the text color of the usernames sometimes don't have a good readability.
What I'm doing now is to calculate the contrast ratio of the background- and the username text color and if it's below a certain value I generate a new random text color for that user until the contrast ratio reaches the threshhold again. This is for sure not really efficient since I have to run a loop to generate a random color and calculate the contrast ratio again until it fits.
I've also looked into just adjusting the brightness of the color depending on the background but that mostly leads to having the same text color for multiple users because the brightness got changed so much that the original color is not really recognizable anymore.
Does anyone have an idea how to or adjust the current text-color or calculate a new color which fits a specific contrast ratio out of two colors directly?