apply method

ZetaColorSwatch apply(
  1. {ZetaContrast contrast = ZetaContrast.aa,
  2. Brightness brightness = Brightness.light}
)

Creates a copy of the current ZetaColorSwatch with potential modifications based on the provided contrast and brightness.

The contrast determines which shade of the color should be used as the primary color in the copied swatch.

  • contrast : The shade to use as the primary color in the new swatch. Defaults to ZetaContrast.aa.
  • brightness : The brightness value for the new swatch. Defaults to Brightness.light.

Implementation

ZetaColorSwatch apply({
  ZetaContrast contrast = ZetaContrast.aa,
  Brightness brightness = Brightness.light,
}) {
  if (this.contrast == contrast && this.brightness == brightness) return this;

  // Generate a list of indices based on brightness level
  final indices = List.generate(10, (index) => (index + 1) * 10);

  // Create a new map (swatch) based on the indices and current swatch values
  final swatch = Map<int, Color>.fromEntries(indices.map((i) => MapEntry(i, super[i] ?? this)));

  // Determine the primaryIndex color of the new swatch based on the accessibility level
  final primaryIndex = brightness == Brightness.light ? contrast.primary : 110 - contrast.primary;

  // Return a new ZetaColorSwatch object with the new primaryIndex color and swatch
  return ZetaColorSwatch(
    contrast: contrast,
    brightness: brightness,
    primary: swatch[primaryIndex]!.value,
    swatch: swatch,
  );
}