getCs function

Cs getCs(
  1. double L,
  2. double a_,
  3. double b_
)

Implementation

Cs getCs(double L, double a_, double b_) {
  LC cusp = findCusp(a_, b_);

  double cMax = findGamutIntersection(a_, b_, L, 1, L, cusp);
  ST stMax = toST(cusp);

  // Scale factor to compensate for the curved part of gamut shape:
  double k = cMax / math.min((L * stMax.S), (1 - L) * stMax.T);

  double cMid;
  {
    ST stMid = getSTMid(a_, b_);

    // Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
    double cA = L * stMid.S;
    double cB = (1 - L) * stMid.T;
    cMid = 0.9 * k * math.sqrt(math.sqrt(1 / (1 / (cA * cA * cA * cA) + 1 / (cB * cB * cB * cB))));
  }

  double c0;
  {
    // for C_0, the shape is independent of hue, so ST are constant. Values picked to roughly be the average values of ST.
    double cA = L * 0.4;
    double cB = (1 - L) * 0.8;

    // Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
    c0 = math.sqrt(1 / (1 / (cA * cA) + 1 / (cB * cB)));
  }

  return Cs(c0, cMid, cMax);
}