/**
 * Calculates an LMS component from an LMS' component.
 * This function is relevant for the conversion between
 * Oklab and CIE 1931 XYZ.
 *
 * @param component The LMS' component.
 *
 * @returns The LMS component.
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toLMSComponentFromLMSDashComponent = (
    component: number,
) => component * component * component;

/*
The following numbers stem from the matrix generated by: https://www.emathhelp.net/calculators/linear-algebra/inverse-of-matrix-calculator/?i=%5B%5B0.2104542553%2C0.7936177850%2C-0.0040720468%5D%2C%5B1.9779984951%2C-2.4285922050%2C0.4505937099%5D%2C%5B0.0259040371%2C0.7827717662%E2%80%8B%E2%80%8B%2C-0.8086757660%E2%80%8B%E2%80%8B%E2%80%8B%5D%5D&m=g.
*/

/**
 * Calculates the l' component of LMS' (Oklab) from Oklab.
 *
 * @param l The luminance component of Oklab.
 * @param a The a component of Oklab.
 * @param b The b component of Oklab.
 *
 * @returns The l' component of LMS' (Oklab).
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toLMSDashLFromOklab = (
    l: number,
    a: number,
    b: number,
) => l * 0.999999998450520 + a * 0.396337792173768 + b * 0.215803758060759;

/**
 * Calculates the m' component of LMS' (Oklab) from Oklab.
 *
 * @param l The luminance component of Oklab.
 * @param a The a component of Oklab.
 * @param b The b component of Oklab.
 *
 * @returns The m' component of LMS' (Oklab).
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toLMSDashMFromOklab = (
    l: number,
    a: number,
    b: number,
) => l * 1.000000008881761 + a * -0.105561342323656 + b * -0.063854174771706;

/**
 * Calculates the s' component of LMS' (Oklab) from Oklab.
 *
 * @param l The luminance component of Oklab.
 * @param a The a component of Oklab.
 * @param b The b component of Oklab.
 *
 * @returns The s' component of LMS' (Oklab).
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toLMSDashSFromOklab = (
    l: number,
    a: number,
    b: number,
) => l * 1.000000054672411 + a * -0.089484182094966 + b * -1.291485537864092;

/*
The following numbers stem from the matrix generated by: https://www.emathhelp.net/calculators/linear-algebra/inverse-of-matrix-calculator/?i=%5B%5B0.8189330101%2C0.3618667424%2C-0.1288597137%5D%2C%5B0.0329845436%2C0.9293118715%2C-0.0361456387%5D%2C%5B0.0482003018%2C0.2643662691%2C0.6338517070%5D%5D&m=g
*/

/**
 * Calculates the X component of CIE 1931 XYZ from LMS'.
 *
 * @param l The l' component of LMS'.
 * @param m The m' component of LMS'.
 * @param s The s' component of LMS'.
 *
 * @returns The X component of CIE 1931 XYZ.
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toCIE1931XYZXFromLMS = (
    l: number,
    m: number,
    s: number,
) => l * 1.227013851103521 + m * -0.557799980651822 + s * 0.281256148966468;

/**
 * Calculates the Y component of CIE 1931 XYZ from LMS'.
 *
 * @param l The l' component of LMS'.
 * @param m The m' component of LMS'.
 * @param s The s' component of LMS'.
 *
 * @returns The Y component of CIE 1931 XYZ.
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toCIE1931XYZYFromLMS = (
    l: number,
    m: number,
    s: number,
) => l * -0.040580178423281 + m * 1.112256869616830 + s * -0.071676678665601;

/**
 * Calculates the Z component of CIE 1931 XYZ from LMS'.
 *
 * @param l The l' component of LMS'.
 * @param m The m' component of LMS'.
 * @param s The s' component of LMS'.
 *
 * @returns The Z component of CIE 1931 XYZ.
 * @see https://bottosson.github.io/posts/oklab/#converting-from-xyz-to-oklab
 */
export const toCIE1931XYZZFromLMS = (
    l: number,
    m: number,
    s: number,
) => l * -0.076381284505707 + m * -0.421481978418013 + s * 1.586163220440795;