import { MESSAGES } from '~/modules/constants';
import { invariant } from '~/modules/invariant';
import { resolveColor } from '~/modules/parsed-color';
import { round } from '~/modules/utils';
import { isString } from '~/modules/validators';

/**
 * Get the chroma of a color.
 *
 * @param input - The input color string.
 * @returns The chroma value (0-1).
 */
export default function chroma(input: string): number {
  invariant(isString(input), MESSAGES.inputString);

  const { r, g, b } = resolveColor(input).rgb;

  const max = Math.max(r, g, b);
  const min = Math.min(r, g, b);

  return round((max - min) / 255, 4);
}
