useWhiteForeground function
Returns true if white text should be used on the given
backgroundColor for readability.
Uses a weighted luminance formula (ITU-R BT.601) to decide. Pass a
positive bias to make the function favour white more aggressively.
useWhiteForeground(Colors.black); // true
useWhiteForeground(Colors.white); // false
Implementation
bool useWhiteForeground(Color backgroundColor, {double bias = 0.0}) {
int v = math
.sqrt(
math.pow(backgroundColor.red, 2) * 0.299 +
math.pow(backgroundColor.green, 2) * 0.587 +
math.pow(backgroundColor.blue, 2) * 0.114,
)
.round();
return v < 130 + bias ? true : false;
}