@fimbul-works/vec-color
Version:
A comprehensive, type-safe color manipulation library for TypeScript that provides a wide range of color space conversions, blending operations, and accessibility utilities.
27 lines (26 loc) • 926 B
JavaScript
import { Vec3, Vec4 } from "@fimbul-works/vec";
/**
* Converts RGB color values to CMYK color space
* @param rgb - Vec3 containing RGB values (each channel from 0 to 1)
* @returns Vec4 containing CMYK values (each channel from 0 to 1)
*/
export function rgbToCMYK(rgb) {
const k = 1 - Math.max(rgb.x, rgb.y, rgb.z);
if (k === 1)
return new Vec4(0, 0, 0, 1);
const c = (1 - rgb.x - k) / (1 - k);
const m = (1 - rgb.y - k) / (1 - k);
const y = (1 - rgb.z - k) / (1 - k);
return new Vec4(c, m, y, k);
}
/**
* Converts CMYK color values to RGB color space
* @param cmyk - Vec4 containing CMYK values (each channel from 0 to 1)
* @returns Vec3 containing RGB values (each channel from 0 to 1)
*/
export function cmykToRGB(cmyk) {
const r = (1 - cmyk.x) * (1 - cmyk.w);
const g = (1 - cmyk.y) * (1 - cmyk.w);
const b = (1 - cmyk.z) * (1 - cmyk.w);
return new Vec3(r, g, b);
}