brightnessFor static method
Determines whether the given Color is Brightness.light or Brightness.dark.
This compares the luminosity and opacity of the given color to a threshold value that matches the material design specification.
Implementation
static Brightness? brightnessFor(Color? color) {
if (color == null) return null;
// See <https://www.w3.org/TR/WCAG20/#contrast-ratiodef>
// The spec says to use kThreshold=0.0525, but Material Design appears to bias
// more towards using light text than WCAG20 recommends. Material Design spec
// doesn't say what value to use, but 0.15 seemed close to what the Material
// Design spec shows for its color palette on
// <https://material.io/go/design-theming#color-color-palette>.
const kLuminanceThreshold = 0.15;
const kOpaqueThreshold = 0.6;
final relativeLuminance = color.computeLuminance();
final luminance = (relativeLuminance + 0.05) * (relativeLuminance + 0.05);
final isLight = luminance > kLuminanceThreshold;
final isOpaque = color.opacity > kOpaqueThreshold;
return isLight
? isOpaque
? Brightness.light
: Brightness.dark
: isOpaque
? Brightness.dark
: Brightness.light;
}