Fast color parsing and manipulation library with comprehensive conversion and accessibility features
—
Core color creation functionality that accepts various input formats and creates tinycolor instances for manipulation and analysis.
Creates a tinycolor instance from various color input formats.
/**
* Creates a tinycolor instance from various input formats
* @param color - Color input (string, object, or tinycolor instance)
* @param opts - Optional configuration object
* @returns tinycolor instance
*/
function tinycolor(color, opts);Parameters:
color: Can be hex string, RGB/RGBA/HSL/HSLA/HSV/HSVA string, named color, color object, or existing tinycolor instanceopts: Optional object with format (string) and gradientType (string) propertiesUsage Examples:
import tinycolor from "tinycolor2";
// String formats
const red = tinycolor("red");
const hex = tinycolor("#ff0000");
const hex3 = tinycolor("#f00");
const hex8 = tinycolor("#ff0000ff");
const rgb = tinycolor("rgb(255, 0, 0)");
const rgba = tinycolor("rgba(255, 0, 0, 0.5)");
const hsl = tinycolor("hsl(0, 100%, 50%)");
const hsla = tinycolor("hsla(0, 100%, 50%, 0.8)");
// Object formats
const rgbObj = tinycolor({ r: 255, g: 0, b: 0 });
const rgbaObj = tinycolor({ r: 255, g: 0, b: 0, a: 0.5 });
const hslObj = tinycolor({ h: 0, s: 1, l: 0.5 });
const hslaObj = tinycolor({ h: 0, s: 1, l: 0.5, a: 0.8 });
const hsvObj = tinycolor({ h: 0, s: 1, v: 1 });
// With options
const color = tinycolor("#ff0000", { format: "hex" });
// From existing tinycolor (creates copy)
const copy = tinycolor(red);Creates a tinycolor instance from ratio-based color values (0-1 range).
/**
* Creates a tinycolor from ratio values (0-1 range)
* @param color - Color object with ratio values
* @param opts - Optional configuration object
* @returns tinycolor instance
*/
tinycolor.fromRatio(color, opts);Usage Examples:
// RGB ratios (0-1 instead of 0-255)
const red = tinycolor.fromRatio({ r: 1, g: 0, b: 0 });
const halfRed = tinycolor.fromRatio({ r: 0.5, g: 0, b: 0 });
// HSL ratios
const blue = tinycolor.fromRatio({ h: 0.67, s: 1, l: 0.5 });
// With alpha
const transparentRed = tinycolor.fromRatio({ r: 1, g: 0, b: 0, a: 0.5 });Generates a random color.
/**
* Generates a random color
* @returns tinycolor instance with random color
*/
tinycolor.random();Usage Examples:
const randomColor = tinycolor.random();
console.log(randomColor.toHexString()); // e.g., "#a3f7b2"
// Generate multiple random colors
const palette = Array.from({ length: 5 }, () => tinycolor.random());#rgb (e.g., #f00)#rrggbb (e.g., #ff0000)#rgba (e.g., #f00f)#rrggbbaa (e.g., #ff0000ff)rgb, rrggbb, rgba, rrggbbaargb(r, g, b) where r,g,b are 0-255rgba(r, g, b, a) where r,g,b are 0-255 and a is 0-1hsl(h, s%, l%) where h is 0-360, s and l are percentageshsla(h, s%, l%, a) where h is 0-360, s and l are percentages, a is 0-1hsv(h, s%, v%) where h is 0-360, s and v are percentageshsva(h, s%, v%, a) where h is 0-360, s and v are percentages, a is 0-1{r: number, g: number, b: number, a?: number}{h: number, s: number, l: number, a?: number}{h: number, s: number, v: number, a?: number}Supports all 147 CSS named colors including:
red, green, blue, yellow, black, white, transparentaliceblue, antiquewhite, aqua, aquamarine, etc.isValid() returning falseisValid() returning falseAll created colors can be validated using the isValid() method:
const valid = tinycolor("red");
const invalid = tinycolor("not-a-color");
console.log(valid.isValid()); // true
console.log(invalid.isValid()); // falseInstall with Tessl CLI
npx tessl i tessl/npm-tinycolor2