getComplement method

Hct getComplement()

Implementation

Hct getComplement() {
  if (_precomputedComplement case final precomputedComplement?) {
    return precomputedComplement;
  }
  final coldestHue = _getColdest().hue;
  final coldestTemp = _getTempsByHct()[_getColdest()]!;
  final warmestHue = _getWarmest().hue;
  final warmestTemp = _getTempsByHct()[_getWarmest()]!;
  final range = warmestTemp - coldestTemp;
  final startHueIsColdestToWarmest = _isBetween(
    input.hue,
    coldestHue,
    warmestHue,
  );
  final startHue = startHueIsColdestToWarmest ? warmestHue : coldestHue;
  final endHue = startHueIsColdestToWarmest ? coldestHue : warmestHue;
  final directionOfRotation = 1.0;

  var smallestError = 1000.0;
  var answer = _getHctsByHue()[input.hue.round()];

  final complementRelativeTemp = (1.0 - getRelativeTemperature(input));

  // Find the color in the other section, closest to the inverse percentile
  // of the input color. This is the complement.
  for (var hueAddend = 0.0; hueAddend <= 360.0; hueAddend += 1.0) {
    final hue = MathUtils.sanitizeDegreesDouble(
      startHue + directionOfRotation * hueAddend,
    );
    if (!_isBetween(hue, startHue, endHue)) {
      continue;
    }
    final possibleAnswer = _getHctsByHue()[hue.round()];
    final relativeTemp =
        (_getTempsByHct()[possibleAnswer]! - coldestTemp) / range;
    final error = (complementRelativeTemp - relativeTemp).abs();
    if (error < smallestError) {
      smallestError = error;
      answer = possibleAnswer;
    }
  }
  return _precomputedComplement = answer;
}