getPickerGeometry static method

PickerGeometry getPickerGeometry(
  1. double lightness
)

Implementation

static PickerGeometry getPickerGeometry(double lightness) {
  // Array of lines
  final lines = Hsluv.getBounds(lightness);
  final numLines = lines.length;
  var outerCircleRadius = 0.0;

  // Find the line closest to origin
  int? closestIndex2;
  double? closestLineDistance;

  for (int i = 0; i < numLines; i += 1) {
    final d = Geometry.distanceLineFromOrigin(lines[i]);
    if (closestLineDistance == null || d < closestLineDistance) {
      closestLineDistance = d;
      closestIndex2 = i;
    }
  }

  final closestLine = lines[closestIndex2!];
  final perpendicularLine = Line(
    slope: 0 - (1 / closestLine.slope),
    intercept: 0.0,
  );
  final startIntersection = Geometry.intersectLineLine(
    closestLine,
    perpendicularLine,
  );
  final startingAngle = Geometry.angleFromOrigin(startIntersection);

  final List<_LineIntersection> intersections = [];

  for (int i1 = 0; i1 < numLines - 1; i1 += 1) {
    for (int i2 = i1 + 1; i2 < numLines; i2 += 1) {
      final intersectionPoint = Geometry.intersectLineLine(
        lines[i1],
        lines[i2],
      );
      final intersectionPointAngle = Geometry.angleFromOrigin(
        intersectionPoint,
      );
      intersections.add(
        _LineIntersection(
          line1: i1,
          line2: i2,
          intersectionPoint: intersectionPoint,
          intersectionPointAngle: intersectionPointAngle,
          relativeAngle: Geometry.normalizeAngle(
            intersectionPointAngle - startingAngle,
          ),
        ),
      );
    }
  }

  intersections.sort((a, b) => a.relativeAngle.compareTo(b.relativeAngle));

  final List<Line> orderedLines = [];
  final List<Point> orderedVertices = [];
  final List<double> orderedAngles = [];

  int currentIndex2 = closestIndex2;

  for (final currentIntersection in intersections) {
    int? nextIndex2;
    if (currentIntersection.line1 == currentIndex2) {
      nextIndex2 = currentIntersection.line2;
    } else if (currentIntersection.line2 == currentIndex2) {
      nextIndex2 = currentIntersection.line1;
    }
    if (nextIndex2 != null) {
      currentIndex2 = nextIndex2;

      orderedLines.add(lines[nextIndex2]);
      orderedVertices.add(currentIntersection.intersectionPoint);
      orderedAngles.add(currentIntersection.intersectionPointAngle);

      final double intersectionPointDistance = Geometry.distanceFromOrigin(
        currentIntersection.intersectionPoint,
      );
      if (intersectionPointDistance > outerCircleRadius) {
        outerCircleRadius = intersectionPointDistance;
      }
    }
  }

  return PickerGeometry(
    lines: orderedLines,
    vertices: orderedVertices,
    angles: orderedAngles,
    outerCircleRadius: outerCircleRadius,
    innerCircleRadius: closestLineDistance!,
  );
}