import type { Color } from '../types/color';

/**
 * Convert an array of linear-light sRGB values in the range 0.0-1.0 to gamma corrected form
 * Extended transfer function:
 *  For negative values, linear portion extends on reflection
 *  of axis, then uses reflected pow below that
 *
 * @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
 * @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
 * @see https://en.wikipedia.org/wiki/SRGB
 */
export function gam_sRGB(RGB: Color): Color {
	return [
		gam_sRGB_channel(RGB[0]),
		gam_sRGB_channel(RGB[1]),
		gam_sRGB_channel(RGB[2]),
	];
}

function gam_sRGB_channel(val: number): number {
	const sign = val < 0 ? -1 : 1;
	const abs = Math.abs(val);

	if (abs > 0.0031308) {
		return sign * (1.055 * Math.pow(abs, 1 / 2.4) - 0.055);
	}

	return 12.92 * val;
}
