colorizr
Version:
Manipulate colors like a boss
31 lines (26 loc) • 1.05 kB
text/typescript
import { MESSAGES, PRECISION } 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 brightness difference between 2 colors.
*
* @param left - The first color string.
* @param right - The second color string.
* @param precision - The decimal precision (default: 5).
* @returns The brightness difference value.
*/
export default function brightnessDifference(
left: string,
right: string,
precision = PRECISION,
): number {
invariant(isString(left), MESSAGES.left);
invariant(isString(right), MESSAGES.right);
const RGBLeft = resolveColor(left).rgb;
const RGBRight = resolveColor(right).rgb;
const brightnessLeft = (RGBLeft.r * 299 + RGBLeft.g * 587 + RGBLeft.b * 114) / 1000;
const brightnessRight = (RGBRight.r * 299 + RGBRight.g * 587 + RGBRight.b * 114) / 1000;
return round(Math.abs(brightnessRight - brightnessLeft), precision);
}