getShadeColor method

Color getShadeColor({
  1. int shadeValue = 15,
  2. bool lighten = true,
  3. bool keepBlack = true,
  4. bool keepWhite = true,
})

The getShadeColor extension is used to make a color darker or lighter, the shadeValue defines the amount in % that the shade should be changed.

It can be used to make a shade of a color to be used in a gradient. By default it makes a color that is 15% lighter. If lighten is false it makes a color that is 15% darker by default.

By default it does not affect black and white colors, but if keepWhite is set to false, it will darken white color when lighten is false and return a grey color. Wise versa for black with keepBlack set to false, it will lighten black color, when lighten is true and return a grey shade.

White cannot be made lighter and black cannot be made darker, the extension just returns white or black for such attempts, with a quick exist from the call.

Implementation

Color getShadeColor({
  final int shadeValue = 15,
  final bool lighten = true,
  final bool keepBlack = true,
  final bool keepWhite = true,
}) {
  if (shadeValue <= 0) return this;
  int usedShadeValue = shadeValue;
  if (usedShadeValue > 100) usedShadeValue = 100;

  // Trying to make black darker, just return black
  if (this == Colors.black && !lighten) return this;
  // Black is defined to be kept as black.
  if (this == Colors.black && keepBlack) return this;
  // Make black lighter as lighten was set and we do not keepBlack
  if (this == Colors.black) return this.lighten(usedShadeValue);

  // Trying to make white lighter, just return white
  if (this == Colors.white && lighten) return this;
  // White is defined to be kept as white.
  if (this == Colors.white && keepWhite) return this;
  // Make white darker as we do not keep white.
  if (this == Colors.white) return darken(usedShadeValue);
  // We are dealing with some other color than white or black, so we
  // make it lighter or darker based on flag and requested shade %
  if (lighten) {
    return this.lighten(usedShadeValue);
  } else {
    return darken(usedShadeValue);
  }
}