lighter static method

double lighter({
  1. required double tone,
  2. required double ratio,
})

Returns a tone >= tone that ensures ratio. Return value is between 0 and 100. Returns -1 if ratio cannot be achieved with tone.

tone Tone return value must contrast with. Range is 0 to 100. Invalid values will result in -1 being returned. ratio Contrast ratio of return value and tone. Range is 1 to 21, invalid values have undefined behavior.

Implementation

static double lighter({required double tone, required double ratio}) {
  if (tone < 0.0 || tone > 100.0) {
    return -1.0;
  }

  final darkY = ColorUtils.yFromLstar(tone);
  final lightY = ratio * (darkY + 5.0) - 5.0;
  final realContrast = _ratioOfYs(lightY, darkY);
  final delta = (realContrast - ratio).abs();
  if (realContrast < ratio && delta > 0.04) {
    return -1;
  }

  // Ensure gamut mapping, which requires a 'range' on tone, will still result
  // the correct ratio by darkening slightly.
  final returnValue = ColorUtils.lstarFromY(lightY) + 0.4;
  if (returnValue < 0 || returnValue > 100) {
    return -1;
  }
  return returnValue;
}