Detecting when the background is too bright and changing the text colour?

Hello! I am working on a shop GUI, and there are some instances where the backgrounds that the text is in front of is sometimes too dark or too light, making it hard to read the text.

Is it possible to detect if a background colour is in a certain range, and change the text colour to either white or black depending on the colour?

Example:
image

These two items have the same white text name labels, but obviously the yellow is way too bright to read. This is where I would want the script to detect if the colour is too bright or in a certain range and change the text to black.

Is this possible?

2 Likes

As far as I know, there’s currently no way to achieve this. But you could, increase your background’s color transparency.

Illustration that may help you :slight_smile:

You can enable Text Stroke for contrast.

Alternatively, you can get a background’s rgb data using BackgroundColor3. For example, BackgroundColor3.R returns the “redness” of the background.

With that in mind you can do something like

local background = backgroundgui_here
local text = textgui_here

local value = background.BackgroundColor3.R + background.BackgroundColor3.G + background.BackgroundColor3.B

if value >= 1.3 then
	text.TextColor3 = Color3.new(0, 0, 0)
else
	text.TextColor3 = Color3.new(1, 1, 1)
end
--Frame is the background UIObject
local color = Frame.BackgroundColor3 
--brightness is a value between 0 and 1
local brightness = (color.R+color.G+color.B)/3

if brightness >= 0.5 then 
	print("bright!")
	--TextLabel is the text UIObject
	TextLabel.TextColor3 = Color3.new(0, 0, 0)
else 
	print("dark!")
	TextLabel.TextColor3 = Color3.new(1, 1, 1)
end
3 Likes