getContrastingTextColor function
Calculates a contrasting text color (black or white) for a given background color. This is used to ensure text is readable on a colored background.
Implementation
Color getContrastingTextColor(Color backgroundColor) {
// Formula to determine luminance (YIQ color space).
// Using the new recommended properties for color components (r, g, b) which are 0.0-1.0.
double luminance = (0.299 * backgroundColor.r +
0.587 * backgroundColor.g +
0.114 * backgroundColor.b) *
255.0;
// Return black for light backgrounds, white for dark backgrounds.
return luminance > 128 ? Colors.black : Colors.white;
}