CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tinycolor2

Fast color parsing and manipulation library with comprehensive conversion and accessibility features

Pending
Overview
Eval results
Files

color-conversion.mddocs/

Color Format Conversion

Comprehensive conversion methods for transforming colors between different format representations including hex, RGB, HSL, HSV, and named colors.

Capabilities

Hex Format Conversion

Convert to Hex

Returns hex representation without the hash prefix.

/**
 * Returns hex representation without hash prefix
 * @param allow3Char - Allow 3-character hex shorthand when possible
 * @returns string hex value without #
 */
toHex(allow3Char?: boolean): string;

Usage Examples:

import tinycolor from "tinycolor2";

const red = tinycolor("red");
const darkGray = tinycolor("#333333");

console.log(red.toHex());           // "ff0000"
console.log(red.toHex(true));       // "f00" (3-char shorthand)
console.log(darkGray.toHex());      // "333333"
console.log(darkGray.toHex(true));  // "333" (3-char shorthand)

Convert to Hex String

Returns hex representation with the hash prefix.

/**
 * Returns hex representation with hash prefix
 * @param allow3Char - Allow 3-character hex shorthand when possible
 * @returns string hex value with #
 */
toHexString(allow3Char?: boolean): string;

Usage Examples:

const blue = tinycolor("blue");
const lightGray = tinycolor("#cccccc");

console.log(blue.toHexString());           // "#0000ff"
console.log(blue.toHexString(true));       // "#00f" (3-char shorthand)
console.log(lightGray.toHexString(true));  // "#ccc" (3-char shorthand)

// Common usage in CSS
const cssColor = blue.toHexString();

Convert to 8-Character Hex

Returns 8-character hex representation including alpha channel.

/**
 * Returns 8-character hex with alpha channel
 * @param allow4Char - Allow 4-character hex shorthand when possible
 * @returns string 8-character hex value without #
 */
toHex8(allow4Char?: boolean): string;

Usage Examples:

const semiTransparentRed = tinycolor("rgba(255, 0, 0, 0.5)");
const opaqueBlue = tinycolor("blue");

console.log(semiTransparentRed.toHex8());      // "ff000080"
console.log(semiTransparentRed.toHex8(true));  // "f008" (4-char shorthand)
console.log(opaqueBlue.toHex8());              // "0000ffff"

Convert to 8-Character Hex String

Returns 8-character hex representation with hash prefix including alpha.

/**
 * Returns 8-character hex string with hash prefix and alpha
 * @param allow4Char - Allow 4-character hex shorthand when possible
 * @returns string 8-character hex value with #
 */
toHex8String(allow4Char?: boolean): string;

Usage Examples:

const transparentGreen = tinycolor("rgba(0, 255, 0, 0.3)");

console.log(transparentGreen.toHex8String());      // "#00ff004d"
console.log(transparentGreen.toHex8String(true));  // "#0f04" (4-char shorthand)

RGB Format Conversion

Convert to RGB Object

Returns RGB representation as an object.

/**
 * Returns RGB representation as an object
 * @returns object with r, g, b (0-255) and a (0-1) properties
 */
toRgb(): {r: number, g: number, b: number, a: number};

Usage Examples:

const purple = tinycolor("purple");
const rgba = tinycolor("rgba(128, 0, 128, 0.7)");

console.log(purple.toRgb());
// { r: 128, g: 0, b: 128, a: 1 }

console.log(rgba.toRgb());
// { r: 128, g: 0, b: 128, a: 0.7 }

// Destructure for individual values
const { r, g, b, a } = purple.toRgb();

Convert to RGB String

Returns RGB/RGBA string representation.

/**
 * Returns RGB/RGBA string representation
 * @returns string RGB or RGBA format
 */
toRgbString(): string;

Usage Examples:

const orange = tinycolor("orange");
const transparentOrange = tinycolor("rgba(255, 165, 0, 0.5)");

console.log(orange.toRgbString());           // "rgb(255, 165, 0)"
console.log(transparentOrange.toRgbString()); // "rgba(255, 165, 0, 0.5)"

// Use in CSS
const element = document.querySelector(".box");
element.style.backgroundColor = orange.toRgbString();

Convert to Percentage RGB

Returns RGB representation with percentage values.

/**
 * Returns RGB representation with percentage values
 * @returns object with r, g, b as percentages (0-100) and a (0-1)
 */
toPercentageRgb(): {r: string, g: string, b: string, a: number};

Usage Examples:

const cyan = tinycolor("cyan");

console.log(cyan.toPercentageRgb());
// { r: "0%", g: "100%", b: "100%", a: 1 }

Convert to Percentage RGB String

Returns RGB string with percentage values.

/**
 * Returns RGB string with percentage values
 * @returns string RGB format with percentages
 */
toPercentageRgbString(): string;

Usage Examples:

const yellow = tinycolor("yellow");

console.log(yellow.toPercentageRgbString());
// "rgb(100%, 100%, 0%)"

HSL Format Conversion

Convert to HSL Object

Returns HSL representation as an object.

/**
 * Returns HSL representation as an object
 * @returns object with h (0-360), s (0-1), l (0-1), and a (0-1) properties
 */
toHsl(): {h: number, s: number, l: number, a: number};

Usage Examples:

const red = tinycolor("red");
const lightBlue = tinycolor("lightblue");

console.log(red.toHsl());
// { h: 0, s: 1, l: 0.5, a: 1 }

console.log(lightBlue.toHsl());
// { h: 195, s: 0.53, l: 0.79, a: 1 }

// Use for color adjustments
const { h, s, l, a } = red.toHsl();
const darkerRed = tinycolor({ h, s, l: l * 0.8, a });

Convert to HSL String

Returns HSL/HSLA string representation.

/**
 * Returns HSL/HSLA string representation
 * @returns string HSL or HSLA format
 */
toHslString(): string;

Usage Examples:

const green = tinycolor("green");
const transparentGreen = tinycolor("rgba(0, 128, 0, 0.6)");

console.log(green.toHslString());           // "hsl(120, 100%, 25%)"
console.log(transparentGreen.toHslString()); // "hsla(120, 100%, 25%, 0.6)"

HSV Format Conversion

Convert to HSV Object

Returns HSV representation as an object.

/**
 * Returns HSV representation as an object
 * @returns object with h (0-360), s (0-1), v (0-1), and a (0-1) properties
 */
toHsv(): {h: number, s: number, v: number, a: number};

Usage Examples:

const magenta = tinycolor("magenta");

console.log(magenta.toHsv());
// { h: 300, s: 1, v: 1, a: 1 }

// Create color wheel
const colors = [];
for (let h = 0; h < 360; h += 30) {
  colors.push(tinycolor({ h, s: 1, v: 1 }));
}

Convert to HSV String

Returns HSV/HSVA string representation.

/**
 * Returns HSV/HSVA string representation
 * @returns string HSV or HSVA format
 */
toHsvString(): string;

Usage Examples:

const violet = tinycolor("violet");

console.log(violet.toHsvString());
// "hsv(300, 47%, 93%)"

Named Color Conversion

Convert to Named Color

Returns the named color if one exists, otherwise returns false.

/**
 * Returns the named color if available
 * @returns string named color or false if no name exists
 */
toName(): string | false;

Usage Examples:

const red = tinycolor("#ff0000");
const customColor = tinycolor("#ff0001");

console.log(red.toName());        // "red"
console.log(customColor.toName()); // false

// Fallback to hex if no name available
function getColorName(color) {
  return color.toName() || color.toHexString();
}

Universal String Conversion

Convert to String

Returns string representation in the specified format.

/**
 * Returns string representation in specified format
 * @param format - Optional format specifier
 * @returns string representation of color
 */
toString(format?: string): string;

Usage Examples:

const color = tinycolor("red");

console.log(color.toString());        // Uses original format or default
console.log(color.toString("hex"));   // "#ff0000"
console.log(color.toString("rgb"));   // "rgb(255, 0, 0)"
console.log(color.toString("hsl"));   // "hsl(0, 100%, 50%)"
console.log(color.toString("name"));  // "red"

// Auto-format based on alpha
function smartFormat(color) {
  return color.getAlpha() < 1 
    ? color.toString("rgb")  // Use RGBA for transparency
    : color.toString("hex"); // Use hex for opaque colors
}

Legacy Format Support

Convert to IE Filter

Returns Internet Explorer filter string for gradients (legacy support).

/**
 * Returns IE filter string for gradients
 * @param secondColor - Optional second color for gradient
 * @returns string IE filter format
 */
toFilter(secondColor?: any): string;

Usage Examples:

const startColor = tinycolor("red");
const endColor = tinycolor("blue");

console.log(startColor.toFilter(endColor));
// "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ff0000ff)"

// Single color filter
console.log(startColor.toFilter());
// "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ffff0000)"

Format Comparison Examples

const color = tinycolor("rgba(255, 128, 0, 0.8)");

// All format outputs for the same color
console.log("Hex:", color.toHexString());           // "#ff8000"
console.log("Hex8:", color.toHex8String());         // "#ff8000cc"
console.log("RGB:", color.toRgbString());           // "rgba(255, 128, 0, 0.8)"
console.log("HSL:", color.toHslString());           // "hsla(30, 100%, 50%, 0.8)"
console.log("HSV:", color.toHsvString());           // "hsva(30, 100%, 100%, 0.8)"
console.log("Name:", color.toName() || "No name");  // "No name"

// Object formats
console.log("RGB Object:", color.toRgb());
// { r: 255, g: 128, b: 0, a: 0.8 }

console.log("HSL Object:", color.toHsl());
// { h: 30, s: 1, l: 0.5, a: 0.8 }

console.log("HSV Object:", color.toHsv());
// { h: 30, s: 1, v: 1, a: 0.8 }

Cloning Colors

/**
 * Creates a new tinycolor instance with the same color values
 * @returns new tinycolor instance
 */
clone(): tinycolor;

Usage Examples:

const original = tinycolor("red");
const copy = original.clone();

// Modify copy without affecting original
copy.lighten(20);

console.log(original.toHexString()); // "#ff0000" (unchanged)
console.log(copy.toHexString());     // "#ff6666" (lightened)

Install with Tessl CLI

npx tessl i tessl/npm-tinycolor2

docs

accessibility.md

color-analysis.md

color-conversion.md

color-creation.md

color-modification.md

color-schemes.md

index.md

utilities.md

tile.json