TopicTheme.harmonized constructor

TopicTheme.harmonized(
  1. Color sourceColor,
  2. Brightness brightness
)

A color harmonized topic theme, based on brightness where colors are harmonized towards a given sourceColor.

Implementation

factory TopicTheme.harmonized(Color sourceColor, Brightness brightness) {
  final int sourceColorValue = sourceColor.value;

  // Fallback color value that can be used for all colors in both modes.
  // This use case should never happen when TopicTheme is setup correctly.
  const int fallbackValue = 0xFF1565C0; // Bright dark blue
  // Use the topic theme for target brightness.
  final TopicTheme topicTheme = brightness == Brightness.light ? light : dark;

  // MaterialColorUtilities Blend function is used to harmonize each color
  // towards the source color hue. This way the topic theme will fit better
  // with the overall ColorScheme. This also works well if the source color
  // for the ColorScheme was extracted from OS theme colors or a device
  // wallpaper, or any image and then created with ColorScheme.fromSeed.
  return TopicTheme(
    generalColor: Color(
      Blend.harmonize(
        // As used used in this app we could bang the color, instead of using
        // a fallback, but this is safer generally.
        topicTheme.generalColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    colorsColor: Color(
      Blend.harmonize(
        topicTheme.colorsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    componentsColor: Color(
      Blend.harmonize(
        topicTheme.componentsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    controlsColor: Color(
      Blend.harmonize(
        topicTheme.controlsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    inputsColor: Color(
      Blend.harmonize(
        topicTheme.inputsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    barsColor: Color(
      Blend.harmonize(
        topicTheme.barsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    navigationColor: Color(
      Blend.harmonize(
        topicTheme.navigationColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    surfacesColor: Color(
      Blend.harmonize(
        topicTheme.surfacesColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    textsColor: Color(
      Blend.harmonize(
        topicTheme.textsColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
  );
}