CodeTheme.harmonized constructor

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

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

Implementation

factory CodeTheme.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 CodeTheme is setup correctly.
  const int fallbackValue = 0xFF1565C0; // Bright dark blue

  // Use the code theme for target brightness.
  final CodeTheme codeTheme = brightness == Brightness.light ? light : dark;

  // MaterialColorUtilities Blend function is used to harmonize each color
  // towards the source color hue. This way the code view text 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 CodeTheme(
    baseColor: Color(
      Blend.harmonize(
        // As used used in this app we could bang the color, instead of using
        // a fallback, but this is safer generally.
        codeTheme.baseColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    numberColor: Color(
      Blend.harmonize(
        codeTheme.numberColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    commentColor: Color(
      Blend.harmonize(
        codeTheme.commentColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    keywordColor: Color(
      Blend.harmonize(
        codeTheme.keywordColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    stringColor: Color(
      Blend.harmonize(
        codeTheme.stringColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    punctuationColor: Color(
      Blend.harmonize(
        codeTheme.punctuationColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    classColor: Color(
      Blend.harmonize(
        codeTheme.classColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
    constantColor: Color(
      Blend.harmonize(
        codeTheme.constantColor?.value ?? fallbackValue,
        sourceColorValue,
      ),
    ),
  );
}