generate static method

GeneratedColors generate({
  1. required String appearance,
  2. required dynamic accent,
  3. required dynamic gray,
  4. required dynamic background,
})

Generate colors from base colors.

Returns a GeneratedColors object containing all generated color scales. See the class documentation for usage examples.

Parameters

  • appearance - "light" or "dark" mode
  • accent - Accent color (Color object or hex string like "#3D63DD")
  • gray - Gray color (Color object or hex string like "#8B8D98")
  • background - Background color (Color object or hex string like "#111111")

Implementation

static GeneratedColors generate({
  required String appearance,
  required dynamic accent,
  required dynamic gray,
  required dynamic background,
}) {
  final isLight = appearance == 'light';
  final accentColor = accent is Color
      ? accent
      : hexToColor(accent as String);
  final grayColor = gray is Color ? gray : hexToColor(gray as String);
  final backgroundColor = background is Color
      ? background
      : hexToColor(background as String);

  // Load pre-built scales and convert to OKLCH
  final scales = _loadScales(isLight);

  var accentScale = _getScaleFromColor(
    sourceColor: accentColor,
    scales: scales,
    backgroundColor: backgroundColor,
  );

  final grayScale = _getScaleFromColor(
    sourceColor: grayColor,
    scales: scales,
    backgroundColor: backgroundColor,
  );

  // Make sure we use the tint from the gray scale for when base is pure white or black
  final accentBaseHex = _colorToHex(accentColor).toLowerCase();
  if (accentBaseHex == '#000' ||
      accentBaseHex == '#fff' ||
      accentBaseHex == '#000000' ||
      accentBaseHex == '#ffffff') {
    accentScale = grayScale.map((c) => Color(c.value)).toList();
  }

  final accentOklch = accentColor.toOkLch();
  final accent9AndContrast = _getStep9Colors(accentScale, accentOklch);
  accentScale[8] = accent9AndContrast.step9.toColor();
  accentScale[9] = _getButtonHoverColor(accent9AndContrast.step9, [
    accentScale,
  ]);

  final accentScaleOklch = accentScale.map((c) => c.toOkLch()).toList();
  final minChroma = max(accentScaleOklch[8].c, accentScaleOklch[7].c);
  final step10Oklch = accentScaleOklch[10];
  final step11Oklch = accentScaleOklch[11];
  accentScale[10] = OkLch(
    step10Oklch.l,
    min(minChroma, step10Oklch.c),
    step10Oklch.h,
  ).toColor();
  accentScale[11] = OkLch(
    step11Oklch.l,
    min(minChroma, step11Oklch.c),
    step11Oklch.h,
  ).toColor();

  final contrastColor = accent9AndContrast.contrast.toColor();

  final accentScaleAlpha = accentScale
      .map((color) => _getAlphaColorSrgb(color, backgroundColor))
      .toList();

  final grayScaleAlpha = grayScale
      .map((color) => _getAlphaColorSrgb(color, backgroundColor))
      .toList();

  final surfaceColor = _getAlphaColorSrgb(
    accentScale[1],
    backgroundColor,
    targetAlpha: isLight ? 0.8 : 0.5,
  );

  return GeneratedColors(
    accentScale: accentScale,
    accentScaleAlpha: accentScaleAlpha,
    grayScale: grayScale,
    grayScaleAlpha: grayScaleAlpha,
    accentContrast: contrastColor,
    accentSurface: surfaceColor,
    background: backgroundColor,
  );
}