gamutClipProjectToLCusp function

RGB gamutClipProjectToLCusp(
  1. RGB rgb
)

Implementation

RGB gamutClipProjectToLCusp(RGB rgb) {
  if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0) {
    return rgb;
  }

  OkLab lab = linearRgbToOkLab(rgb);

  double L = lab.L;
  double eps = 0.00001;
  double C = math.max(eps, math.sqrt(lab.a * lab.a + lab.b * lab.b));
  double a_ = lab.a / C;
  double b_ = lab.b / C;

  // The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
  LC cusp = findCusp(a_, b_);

  double L0 = cusp.L;

  double t = findGamutIntersection(a_, b_, L, C, L0, cusp);

  double lClipped = L0 * (1 - t) + t * L;
  double cClipped = t * C;

  return okLabToLinearRgb(OkLab(lClipped, cClipped * a_, cClipped * b_));
}