blend static method

Color blend(
  1. Color dst,
  2. Color src,
  3. double opacity
)

Blends two colors with a specified opacity.

This method creates a new Color by blending the destination dst color with the source src color. The opacity parameter determines the influence of the source color, where 0.0 means no influence (returns the destination color) and 1.0 means full influence (returns the source color).

Parameters

  • dst: The destination Color (background).
  • src: The source Color (foreground).
  • opacity: The opacity of the source color in the blend. Must be between 0.0 and 1.0.

Returns

A new Color representing the blend of the two colors.

Example

final blended = ColorUtils.blend(Colors.red, Colors.blue, 0.5); // Purple blend.

Implementation

static Color blend(Color dst, Color src, double opacity) {
  // Color channels are normalized (0.0-1.0); fromARGB expects 0-255.
  int channel(double from, double to) =>
      ((from * (1.0 - opacity) + to * opacity) * 255).round().clamp(0, 255);
  return Color.fromARGB(
    255,
    channel(dst.r, src.r),
    channel(dst.g, src.g),
    channel(dst.b, src.b),
  );
}