nearSetColor static method

Color nearSetColor(
  1. Color? useColor,
  2. Color fromColor,
  3. int sub
)

useColorfromColor保持 sub 以内 的距离

Implementation

static Color nearSetColor(
  Color? useColor,
  Color fromColor,
  int sub,
) {
  if (null == useColor) {
    return fromColor;
  }
  int subPoint(int usePoint, int fromPoint, int limit) {
    final sub = usePoint - fromPoint;
    if (limit < 0) {
      limit = -limit;
    }
    if (sub <= limit && sub >= -limit) {
      return usePoint;
    }
    final rePoint = fromPoint + ((sub > 0) ? limit : -limit);
    if (rePoint < 0) {
      return 0;
    } else if (rePoint > 255) {
      return 255;
    } else {
      return rePoint;
    }
  }

  return Color.fromRGBO(
    subPoint(useColor.red, fromColor.red, sub),
    subPoint(useColor.green, fromColor.green, sub),
    subPoint(useColor.blue, fromColor.blue, sub),
    1,
  );
}