generateSwatch method

Map<int, Color> generateSwatch(
  1. {int primary = kZetaSwatchPrimaryIndex,
  2. Map<int, double> targetContrasts = kZetaSwatchTargetContrasts,
  3. Color background = ZetaColorBase.white,
  4. bool adjustPrimary = true}
)

Generates a color swatch for this color. A color swatch is a map with integer keys indexing to Color objects, typically used for design themes.

The function has optional parameters:

  • primary (Default = kZetaSwatchPrimaryIndex) - The primary color index for the swatch. This number should be a key in the swatch map.
  • targetContrasts (Default = kZetaSwatchTargetContrasts) - Map of target contrast values for each color index.
  • background (Default = ZetaColorBase.white) - The color used to determine the contrast of the colors in the swatch. Generally, this should be the background color that the color swatch will be displayed on.
  • adjustPrimary (Default = true) - Determines whether to adjust the contrast of the primary color on the background color. Useful in cases the brand color is being used.

Returns a Map<int, Color> object.

Implementation

Map<int, Color> generateSwatch({
  int primary = kZetaSwatchPrimaryIndex,
  Map<int, double> targetContrasts = kZetaSwatchTargetContrasts,
  Color background = ZetaColorBase.white,
  bool adjustPrimary = true,
}) {
  assert(
    targetContrasts.containsKey(primary),
    'Primary key not found in targetContrasts.',
  );

  assert(
    targetContrasts.values.every((v) => v > 0),
    'All values in targetContrasts should be positive and non-null.',
  );

  final swatch = <int, Color>{};

  final adjustedPrimary = adjustPrimary
      ? adjustContrast(
          on: background,
          target: targetContrasts[primary]!,
        )
      : this;

  swatch[primary] = adjustedPrimary;

  for (final shade in targetContrasts.keys) {
    if (shade != primary) {
      swatch[shade] = adjustedPrimary.adjustContrast(
        on: background,
        target: targetContrasts[shade]!,
      );
    }
  }

  return swatch;
}