Sunday, 12 May 2019

isDarkColor() Is it?

The Goal

To decide what colour text to place on an unknown background color.

Example

In this example of using the function isDarkColor(), we switch the background color every time the test button is clicked, then ask the function "is this new background color dark or light" in order to decide what tint color to set for the test buttons title.

isDarkColor() function

func isDarkColor(color){
	const inputColor:Color = color

	const r:Float = Float(inputColor.red)
	const g:Float = Float(inputColor.green)
	const b:Float = Float(inputColor.blue)

	const luminannce = Math.sqrt( ((r * r) * 0.299) + ((g * g) * 0.587) + ((b * b) * 0.114) )

	if ( luminannce > 0.5 ) {
		return false
	}else{
		return true
	}
}

The function simply takes a color as a parameter, and returns true of false. It either is or it isnt a dark color.
In our example we base the buttons title color on that return value every time the background changes color.

.

No comments:

Post a Comment

isDarkColor() Is it?

The Goal To decide what colour text to place on an unknown background color. In this example of using the function isDarkColor(), we switch ...