getTintOpacityModifier static method

double getTintOpacityModifier(
  1. Color color
)

Implementation

static double getTintOpacityModifier(Color color) {
  // Mid point of HsvV range that these calculations are based on. This is here for easy tuning.
  const midPoint = 0.50;

  const whiteMaxOpacity = 0.45; // 100% luminosity
  const midPointMaxOpacity = 0.90; // 50% luminosity
  const blackMaxOpacity = 0.85; // 0% luminosity

  var hsv = HSVColor.fromColor(color);

  var opacityModifier = midPointMaxOpacity;

  if (hsv.value != midPoint) {
    // Determine maximum suppression amount
    var lowestMaxOpacity = midPointMaxOpacity;
    var maxDeviation = midPoint;

    if (hsv.value > midPoint) {
      lowestMaxOpacity = whiteMaxOpacity; // At white (100% hsvV)
      maxDeviation = 1 - maxDeviation;
    } else if (hsv.value < midPoint) {
      lowestMaxOpacity = blackMaxOpacity; // At black (0% hsvV)
    }

    var maxOpacitySuppression = midPointMaxOpacity - lowestMaxOpacity;

    // Determine normalized deviation from the midpoint
    var deviation = hsv.value - midPoint;
    var normalizedDeviation = deviation / maxDeviation;

    // If we have saturation, reduce opacity suppression to allow that color to come through more
    if (hsv.saturation > 0) {
      // Dampen opacity suppression based on how much saturation there is
      maxOpacitySuppression *= math.max(1 - (hsv.saturation * 2), 0.0);
    }

    var opacitySuppression = maxOpacitySuppression * normalizedDeviation;

    opacityModifier = midPointMaxOpacity - opacitySuppression;
  }

  return opacityModifier;
}