materialName static method

String materialName(
  1. Color color,
  2. {Map<ColorSwatch<Object>, String>? colorSwatchNameMap,
  3. bool withIndex = true}
)

Returns the Material swatch name or custom color swatch name for a given color.

The name will include the color index if the flag withIndex is true. If the given color is not a material color or one of the accents colors, an empty string is returned. The function can also take as input an optional custom color swatch to name map and return a custom name for any color found in any of the custom color swatches in the map.

Implementation

static String materialName(Color color,
    {Map<ColorSwatch<Object>, String>? colorSwatchNameMap,
    bool withIndex = true}) {
  // If it is a black or white shade, return name, shade and optional index.
  for (final ColorSwatch<Object> swatch in blackAndWhiteNames.keys) {
    for (final int i in _indexPrimary) {
      if (swatch[i] == color || swatch[i]?.value == color.value) {
        if (withIndex) {
          return '${blackAndWhiteNames[swatch]} [$i]';
        } else {
          return blackAndWhiteNames[swatch]!;
        }
      }
    }
  }
  // If it is a primary color, return name, shade and and optional index.
  for (final ColorSwatch<Object> swatch in primaryColorNames.keys) {
    for (final int i in _indexPrimaryWith850) {
      if (swatch[i] == color || swatch[i]?.value == color.value) {
        if (withIndex) {
          return '${primaryColorNames[swatch]} [$i]';
        } else {
          return primaryColorNames[swatch]!;
        }
      }
    }
  }
  // If it is an accent color, return name, shade and optional index.
  // index = <int>[100, 200, 400, 700];
  for (final ColorSwatch<Object> swatch in accentColorsNames.keys) {
    for (final int i in _indexAccent) {
      if (swatch[i] == color || swatch[i]?.value == color.value) {
        if (withIndex) {
          return '${accentColorsNames[swatch]} [$i]';
        } else {
          return accentColorsNames[swatch]!;
        }
      }
    }
  }
  // If we have a custom color and name map passed, we will check if the
  // color exists in it as well and what name it has in the map and
  // return name, shade and optional index.
  if (colorSwatchNameMap != null) {
    for (final ColorSwatch<Object> swatch in colorSwatchNameMap.keys) {
      for (final int i in _indexPrimary) {
        if (swatch[i] == color || swatch[i]?.value == color.value) {
          if (withIndex) {
            return '${colorSwatchNameMap[swatch]} [$i]';
          } else {
            return colorSwatchNameMap[swatch]!;
          }
        }
      }
    }
  }
  // If all the above did not yield a name, it has no defined name,
  // return an empty string.
  return '';
}