FlexSchemeOnColors.from constructor

FlexSchemeOnColors.from({
  1. required Color primary,
  2. Color? primaryContainer,
  3. required Color secondary,
  4. Color? secondaryContainer,
  5. Color? tertiary,
  6. Color? tertiaryContainer,
  7. required Color surface,
  8. Color? surfaceVariant,
  9. Color? inverseSurface,
  10. required Color background,
  11. required Color error,
  12. Color? errorContainer,
  13. Color? onPrimary,
  14. Color? onPrimaryContainer,
  15. Color? onSecondary,
  16. Color? onSecondaryContainer,
  17. Color? onTertiary,
  18. Color? onTertiaryContainer,
  19. Color? onSurface,
  20. Color? onSurfaceVariant,
  21. Color? onInverseSurface,
  22. Color? onBackground,
  23. Color? onError,
  24. Color? onErrorContainer,
  25. int primaryAlpha = 0,
  26. int primaryContainerAlpha = 0,
  27. int secondaryAlpha = 0,
  28. int secondaryContainerAlpha = 0,
  29. int tertiaryAlpha = 0,
  30. int tertiaryContainerAlpha = 0,
  31. int surfaceAlpha = 0,
  32. int surfaceVariantAlpha = 0,
  33. int inverseSurfaceAlpha = 0,
  34. int backgroundAlpha = 0,
  35. int errorAlpha = 0,
  36. int errorContainerAlpha = 0,
})

Compute on colors for required primary, secondary, surface, background and error colors and returns a valid FlexSchemeOnColors with correct on colors for these colors.

If an optional on color value is given as input, the value for that particular on color will be used instead of the computed on color value for the corresponding provided color.

The factory can also alpha blend the onColor, with each color using an optionally provided alpha blend level, that defaults to 0.

Implementation

factory FlexSchemeOnColors.from({
  required Color primary,
  Color? primaryContainer,
  required Color secondary,
  Color? secondaryContainer,
  Color? tertiary,
  Color? tertiaryContainer,
  required Color surface,
  Color? surfaceVariant,
  Color? inverseSurface,
  required Color background,
  required Color error,
  Color? errorContainer,
  Color? onPrimary,
  Color? onPrimaryContainer,
  Color? onSecondary,
  Color? onSecondaryContainer,
  Color? onTertiary,
  Color? onTertiaryContainer,
  Color? onSurface,
  Color? onSurfaceVariant,
  Color? onInverseSurface,
  Color? onBackground,
  Color? onError,
  Color? onErrorContainer,
  int primaryAlpha = 0,
  int primaryContainerAlpha = 0,
  int secondaryAlpha = 0,
  int secondaryContainerAlpha = 0,
  int tertiaryAlpha = 0,
  int tertiaryContainerAlpha = 0,
  int surfaceAlpha = 0,
  int surfaceVariantAlpha = 0,
  int inverseSurfaceAlpha = 0,
  int backgroundAlpha = 0,
  int errorAlpha = 0,
  int errorContainerAlpha = 0,
}) {
  // Check brightness of primary, secondary, error, surface and background
  // colors, then calculate appropriate colors for their onColors, if an
  // "on" color was not passed in, otherwise we just use its given color.
  final Color usedOnPrimary = onPrimary ??
      (ThemeData.estimateBrightnessForColor(primary) == Brightness.dark
          ? Colors.white.blendAlpha(primary.brighten(20), primaryAlpha)
          : Colors.black.blendAlpha(primary.brighten(20), primaryAlpha));
  final Color? usedOnPrimaryContainer = onPrimaryContainer ??
      (primaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(primaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  primaryContainer.brighten(22), primaryContainerAlpha)
              : Colors.black.blendAlpha(
                  primaryContainer.brighten(8), primaryContainerAlpha)));

  final Color usedOnSecondary = onSecondary ??
      (ThemeData.estimateBrightnessForColor(secondary) == Brightness.dark
          ? Colors.white.blendAlpha(secondary.brighten(20), secondaryAlpha)
          : Colors.black.blendAlpha(secondary.brighten(20), secondaryAlpha));
  final Color? usedOnSecondaryContainer = onSecondaryContainer ??
      (secondaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(secondaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  secondaryContainer.brighten(22), secondaryContainerAlpha)
              : Colors.black.blendAlpha(
                  secondaryContainer.brighten(8), secondaryContainerAlpha)));
  final Color? usedOnTertiary = onTertiary ??
      (tertiary == null
          ? null
          : (ThemeData.estimateBrightnessForColor(tertiary) == Brightness.dark
              ? Colors.white.blendAlpha(tertiary.brighten(20), tertiaryAlpha)
              : Colors.black
                  .blendAlpha(tertiary.brighten(20), tertiaryAlpha)));
  final Color? usedOnTertiaryContainer = onTertiaryContainer ??
      (tertiaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(tertiaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  tertiaryContainer.brighten(22), tertiaryContainerAlpha)
              : Colors.black.blendAlpha(
                  tertiaryContainer.brighten(8), tertiaryContainerAlpha)));

  final Color usedOnSurface = onSurface ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? Colors.white.blendAlpha(surface, surfaceAlpha)
          : Colors.black.blendAlpha(surface, surfaceAlpha));
  final Color usedOnSurfaceVariant = onSurfaceVariant ??
      (ThemeData.estimateBrightnessForColor(surfaceVariant ?? surface) ==
              Brightness.dark
          ? Colors.white
              .blendAlpha(surfaceVariant ?? surface, surfaceVariantAlpha)
          : Colors.black
              .blendAlpha(surfaceVariant ?? surface, surfaceVariantAlpha));
  final Color invSurface = inverseSurface ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? Colors.white
          : Colors.black);
  final Color usedOnInverseSurface = onInverseSurface ??
      (ThemeData.estimateBrightnessForColor(invSurface) == Brightness.dark
          ? Colors.white.blendAlpha(invSurface, inverseSurfaceAlpha)
          : Colors.black.blendAlpha(invSurface, inverseSurfaceAlpha));
  final Color usedOnBackground = onBackground ??
      (ThemeData.estimateBrightnessForColor(background) == Brightness.dark
          ? Colors.white.blendAlpha(background, backgroundAlpha)
          : Colors.black.blendAlpha(background, backgroundAlpha));
  final Color usedOnError = onError ??
      (estimateErrorBrightness(error) == Brightness.dark
          ? Colors.white.blendAlpha(error.brighten(20), errorAlpha)
          : Colors.black.blendAlpha(error.brighten(20), errorAlpha));

  final Color usedOnErrorContainer = onErrorContainer ??
      (estimateErrorBrightness(errorContainer ?? error) == Brightness.dark
          ? Colors.white.blendAlpha(
              errorContainer?.brighten(22) ?? error.brighten(20),
              errorContainerAlpha)
          : Colors.black.blendAlpha(
              errorContainer?.brighten(8) ?? error.brighten(20),
              errorContainerAlpha));

  return FlexSchemeOnColors(
    onPrimary: usedOnPrimary,
    onPrimaryContainer: usedOnPrimaryContainer,
    onSecondary: usedOnSecondary,
    onSecondaryContainer: usedOnSecondaryContainer,
    onTertiary: usedOnTertiary,
    onTertiaryContainer: usedOnTertiaryContainer,
    onSurface: usedOnSurface,
    onSurfaceVariant: usedOnSurfaceVariant,
    onInverseSurface: usedOnInverseSurface,
    onBackground: usedOnBackground,
    onError: usedOnError,
    onErrorContainer: usedOnErrorContainer,
  );
}