fromList static method

FlexTonalPalette fromList(
  1. List<int> colors, [
  2. FlexPaletteType paletteType = FlexPaletteType.common
])

Create colors from a fixed-size list of ARGB color ints.

Inverse of FlexTonalPalette.asList.

Implementation

static FlexTonalPalette fromList(
  List<int> colors, [
  FlexPaletteType paletteType = FlexPaletteType.common,
]) {
  assert(
      (colors.length == commonSize &&
              paletteType == FlexPaletteType.common) ||
          (colors.length == extendedSize &&
              paletteType == FlexPaletteType.extended),
      'Length must be $commonSize when using FlexPaletteType.common OR '
      'length must be $extendedSize when using FlexPaletteType.extended.');
  final Map<int, int> cache = <int, int>{};
  switch (paletteType) {
    case FlexPaletteType.common:
      commonTones.asMap().forEach(
          (int index, int toneValue) => cache[toneValue] = colors[index]);
    case FlexPaletteType.extended:
      extendedTones.asMap().forEach(
          (int index, int toneValue) => cache[toneValue] = colors[index]);
  }

  // Approximately deduces the original hue and chroma that generated this
  // list of colors.
  // Uses the hue and chroma of the provided color with the highest chroma.
  double bestHue = 0.0;
  double bestChroma = 0.0;
  for (final int argb in colors) {
    final Hct hct = Hct.fromInt(argb);

    // If the color is too close to white, its chroma may have been
    // affected by a known issue, so we ignore it.
    // https://github.com/material-foundation/material-color-utilities/issues/140
    if (hct.tone > 98.0) continue;

    if (hct.chroma > bestChroma) {
      bestHue = hct.hue;
      bestChroma = hct.chroma;
    }
  }
  return FlexTonalPalette._fromCache(cache, bestHue, bestChroma, paletteType);
}