getLighterShade method

Color getLighterShade(
  1. double factor
)

Lightens the color by a specified factor.

The operation moves the color towards white (RGB 255, 255, 255). The alpha channel remains unchanged.

@param factor A double between 0.0 (no change) and 1.0 (pure white). @returns A new Color object representing the lighter shade.

Example:

final lighterBlue = Colors.blue.getLighterShade(0.2); // 20% lighter
final pureWhite = Colors.blue.getLighterShade(1.0); // pure white

Implementation

Color getLighterShade(double factor) {
  assert(factor >= 0 && factor <= 1, 'Factor must be between 0 and 1');

  // Extract the red, green, and blue components of the color
  int red = (r * 255).toInt();
  int green = (g * 255).toInt();
  int blue = (b * 255).toInt();

  // Calculate the new lighter color components
  int newRed = red + ((255 - red) * factor).round();
  int newGreen = green + ((255 - green) * factor).round();
  int newBlue = blue + ((255 - blue) * factor).round();

  // Ensure the values are within the valid range (0-255)
  newRed = newRed.clamp(0, 255);
  newGreen = newGreen.clamp(0, 255);
  newBlue = newBlue.clamp(0, 255);

  // Return the new lighter color
  return Color.fromARGB((a * 255).toInt(), newRed, newGreen, newBlue);
}