complement property

Hct complement

A color that complements the input color aesthetically.

In art, this is usually described as being across the color wheel. History of this shows intent as a color that is just as cool-warm as the input color is warm-cool.

Implementation

Hct get complement {
  if (_complement != null) {
    return _complement!;
  }

  final coldestHue = coldest.hue;
  final coldestTemp = tempsByHct[coldest]!;

  final warmestHue = warmest.hue;
  final warmestTemp = tempsByHct[warmest]!;
  final range = warmestTemp - coldestTemp;
  final startHueIsColdestToWarmest =
      isBetween(angle: input.hue, a: coldestHue, b: warmestHue);
  final startHue = startHueIsColdestToWarmest ? warmestHue : coldestHue;
  final endHue = startHueIsColdestToWarmest ? coldestHue : warmestHue;
  const directionOfRotation = 1.0;
  var smallestError = 1000.0;
  var answer = hctsByHue[input.hue.round()];

  final complementRelativeTemp = 1.0 - inputRelativeTemperature;
  // 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(angle: hue, a: startHue, b: endHue)) {
      continue;
    }
    final possibleAnswer = hctsByHue[hue.round()];
    final relativeTemp = (_tempsByHct[possibleAnswer]! - coldestTemp) / range;
    final error = (complementRelativeTemp - relativeTemp).abs();
    if (error < smallestError) {
      smallestError = error;
      answer = possibleAnswer;
    }
  }
  _complement = answer;
  return _complement!;
}