Comprehensive color conversion capabilities for transforming between color models and generating various output formats including CSS strings, objects, and arrays.
Convert Color instances between different color models while preserving color accuracy.
/**
* Convert to RGB color model
* @param args - Optional RGB values to create new instance
* @returns ColorInstance in RGB model
*/
rgb(...args?: number[]): ColorInstance;
/**
* Convert to HSL color model
* @param args - Optional HSL values to create new instance
* @returns ColorInstance in HSL model
*/
hsl(...args?: number[]): ColorInstance;
/**
* Convert to HSV color model
* @param args - Optional HSV values to create new instance
* @returns ColorInstance in HSV model
*/
hsv(...args?: number[]): ColorInstance;
/**
* Convert to HWB color model
* @param args - Optional HWB values to create new instance
* @returns ColorInstance in HWB model
*/
hwb(...args?: number[]): ColorInstance;
/**
* Convert to CMYK color model
* @param args - Optional CMYK values to create new instance
* @returns ColorInstance in CMYK model
*/
cmyk(...args?: number[]): ColorInstance;
/**
* Convert to XYZ color space
* @param args - Optional XYZ values to create new instance
* @returns ColorInstance in XYZ model
*/
xyz(...args?: number[]): ColorInstance;
/**
* Convert to LAB color space
* @param args - Optional LAB values to create new instance
* @returns ColorInstance in LAB model
*/
lab(...args?: number[]): ColorInstance;
/**
* Convert to LCH color space
* @param args - Optional LCH values to create new instance
* @returns ColorInstance in LCH model
*/
lch(...args?: number[]): ColorInstance;
/**
* Convert to ANSI 16-color
* @param args - Optional ANSI16 values to create new instance
* @returns ColorInstance in ANSI16 model
*/
ansi16(...args?: number[]): ColorInstance;
/**
* Convert to ANSI 256-color
* @param args - Optional ANSI256 values to create new instance
* @returns ColorInstance in ANSI256 model
*/
ansi256(...args?: number[]): ColorInstance;
/**
* Convert to HCG color model
* @param args - Optional HCG values to create new instance
* @returns ColorInstance in HCG model
*/
hcg(...args?: number[]): ColorInstance;
/**
* Convert to Apple color format
* @param args - Optional Apple values to create new instance
* @returns ColorInstance in Apple model
*/
apple(...args?: number[]): ColorInstance;Usage Examples:
import Color from "color";
const color = Color('#FF8000'); // Orange
// Convert between color models
const hslColor = color.hsl(); // Convert to HSL
const cmykColor = color.cmyk(); // Convert to CMYK
const labColor = color.lab(); // Convert to LAB
// Chain conversions
const result = Color('red')
.hsl() // Convert to HSL
.lighten(0.2) // Manipulate in HSL space
.rgb(); // Convert back to RGB
// Create new instance with modified values
const modifiedHsl = color.hsl(120, 80, 60); // New HSL colorGenerate CSS-compatible color strings in various formats with optional precision control.
/**
* Generate CSS string representation
* @param places - Optional decimal places for rounding (default: 1)
* @returns CSS color string in current model
*/
string(places?: number): string;
/**
* Generate RGB percentage string representation
* @param places - Optional decimal places for rounding (default: 1)
* @returns CSS rgb() string with percentages
*/
percentString(places?: number): string;
/**
* Generate hex color string (without alpha)
* @returns Hex color string like '#FF8000'
*/
hex(): string;
/**
* Generate hex color string with alpha channel
* @returns Hex color string with alpha like '#FF8000CC'
*/
hexa(): string;
/**
* Get CSS keyword representation if available
* @returns CSS color keyword or hex fallback
*/
keyword(): string;Usage Examples:
const color = Color('hsl(30, 100%, 50%)');
// CSS string outputs
color.string(); // 'hsl(30, 100%, 50%)'
color.string(0); // 'hsl(30, 100%, 50%)' (no decimals)
color.rgb().string(); // 'rgb(255, 128, 0)'
color.percentString(); // 'rgb(100%, 50%, 0%)'
color.hex(); // '#FF8000'
color.alpha(0.8).hexa(); // '#FF8000CC'
Color('red').keyword(); // 'red'
Color('#123456').keyword(); // '#123456' (no keyword available)Export color data as arrays, objects, and other structured formats.
/**
* Get color values as array
* @returns Array of color values (with alpha if not 1)
*/
array(): number[];
/**
* Get color values as object with channel names
* @returns Object with channel properties and optional alpha
*/
object(): ColorObject;
/**
* Get RGB values as 0-1 unit array
* @returns Array with normalized RGB values (0-1 range)
*/
unitArray(): number[];
/**
* Get RGB values as 0-1 unit object
* @returns Object with normalized RGB properties (0-1 range)
*/
unitObject(): {r: number; g: number; b: number; alpha?: number};
/**
* Get RGB as single number
* @returns RGB color as hexadecimal number (0xRRGGBB)
*/
rgbNumber(): number;
/**
* Get JSON representation
* @returns JSON object with model, color array, and alpha
*/
toJSON(): ColorJson;
/**
* Get string representation (same as string())
* @returns CSS color string
*/
toString(): string;
type ColorObject = {
alpha?: number;
} & Record<string, number>;
type ColorJson = {
model: string;
color: number[];
valpha: number;
};Usage Examples:
const color = Color('rgb(255, 128, 0)').alpha(0.8);
// Array formats
color.array(); // [255, 128, 0, 0.8]
color.rgb().array(); // [255, 128, 0, 0.8]
color.hsl().array(); // [30, 100, 50, 0.8]
// Object formats
color.object(); // {r: 255, g: 128, b: 0, alpha: 0.8}
color.hsl().object(); // {h: 30, s: 100, l: 50, alpha: 0.8}
// Unit formats (0-1 range)
color.unitArray(); // [1, 0.502, 0, 0.8]
color.unitObject(); // {r: 1, g: 0.502, b: 0, alpha: 0.8}
// Other formats
color.rgbNumber(); // 16744448 (0xFF8000)
color.toJSON(); // {model: 'rgb', color: [255, 128, 0], valpha: 0.8}
color.toString(); // 'rgba(255, 128, 0, 0.8)'Control output precision with rounding methods.
/**
* Round color values to specified decimal places
* @param places - Number of decimal places (default: 0)
* @returns New ColorInstance with rounded values
*/
round(places?: number): ColorInstance;Usage Examples:
const color = Color('rgb(255.7, 128.3, 0.9)');
color.round(); // Round to integers
color.round(1); // Round to 1 decimal place
color.round(2).string(); // 'rgb(255.70, 128.30, 0.90)'All conversion methods support both getting and setting modes.
Usage Examples:
const color = Color('#FF0000');
// Get current values in different models
const rgbVersion = color.rgb(); // Convert to RGB model
const hslVersion = color.hsl(); // Convert to HSL model
// Set new values while converting
const newRgb = color.rgb(0, 255, 0); // Create new green color in RGB
const newHsl = color.hsl(240, 100, 50); // Create new blue color in HSL
// Original color unchanged (immutable)
console.log(color.hex()); // Still '#FF0000'
console.log(newRgb.hex()); // '#00FF00'
console.log(newHsl.hex()); // '#0000FF'rgb(255, 128, 0) or rgba(255, 128, 0, 0.8)hsl(30, 100%, 50%) or hsla(30, 100%, 50%, 0.8)