import { assert } from "../../assert.js";
import { clamp01 } from "../../math/clamp01.js";
import { Color } from "../Color.js";
import { linear_srgb_to_okhsv } from "../oklab/linear_srgb_to_okhsv.js";
import { okhsv_to_linear_srgb } from "../oklab/okhsv_to_linear_srgb.js";

const hsv = [0, 0, 0];

/**
 *
 * @param {Color} input
 * @param {number} [amount]
 * @returns {Color}
 */
export function color_desaturate(input, amount = 1) {
    assert.isNumber(amount, 'amount');
    assert.greaterThanOrEqual(amount, 0, 'amount');

    if (amount === 0) {
        // no change
        return input;
    }

    const r = new Color();

    linear_srgb_to_okhsv(hsv, input.r, input.g, input.b);

    hsv[1] = clamp01(hsv[1] * (1 - amount));

    okhsv_to_linear_srgb(r, ...hsv);

    return r;
}