'Stolen' from TinyColor, rewritten in TypeScript, and made smaller. Not all features from TinyColor have been implemented, just the ones that I find useful.
npm install @owenrees/colorconverter
yarn add @owenrees/colorconverter
pnpm
pnpm add @owenrees/colorconverter
import ColorConverter from 'colorconverter';
const color = new ColorConverter('red');
const ColorConverter = require('colorconverter');
const color = new ColorConverter('red');
Add the following script tag to your HTML:
<script src="https://unpkg.com/@owenrees/colorconverter/lib/colorconverter.umd.min.js"></script>
or point to the file locally:
<script src="path/to/colorconverter.umd.min.js"></script>
Then use the global ColorConverter
variable:
const ColorConverter = window.ColorConverter;
const color = new ColorConverter('red');
Colour comparisons and manipulations can be done using the methods on the ColorConverter
instance.
Conversions between colour spaces will never be 100% accurate, therefore are only to be used as a guide or for compatibility with other systems.
Returns a boolean indicating whether the parsed color is valid.
const color = new ColorConverter('red');
color.isValid(); // true
Returns the RGB values of the color as an object, in range 0-255.
const color = new ColorConverter('red');
color.toRgb(); // { r: 255, g: 0, b: 0 }
Return a string representation of the RGB values.
const color = new ColorConverter('red');
color.toRgbString(); // 'rgb(255, 0, 0)'
Returns the RGB / RGBA values of the color as an object, in range 0-1.
const color = new ColorConverter('red');
color.toNormalizedRgb(); // { r: 1, g: 0, b: 0 }
Return a string representation of the normalized RGB / RGBA values.
const color = new ColorConverter('red');
color.toNormalizedRgbString(); // 'rgb(1, 0, 0)'
Returns the RGB / RGBA values of the color as an object, in range 0-100.
const color = new ColorConverter('red');
color.toPercentageRgb(); // { r: 100, g: 0, b: 0 }
const color = new ColorConverter('rgba(255, 0, 0, 0.5)');
color.toPercentageRgb(); // { r: 100, g: 0, b: 0, a: 0.5 }
Return a string representation of the percentage RGB / RGBA values.
const color = new ColorConverter('red');
color.toPercentageRgbString(); // 'rgb(100%, 0%, 0%)'
const color = new ColorConverter('rgba(255, 0, 0, 0.5)');
color.toPercentageRgbString(); // 'rgba(100%, 0%, 0%, 0.5)'
Returns the HSV values of the color as an object.
const color = new ColorConverter('red');
color.toHsv(); // { h: 0, s: 100, v: 100 }
Return a string representation of the HSV values.
const color = new ColorConverter('red');
color.toHsvString(); // 'hsv(0, 100%, 100%)'
Returns the HSL values of the color as an object.
const color = new ColorConverter('red');
color.toHsl(); // { h: 0, s: 100, l: 50 }
Return a string representation of the HSL values.
const color = new ColorConverter('red');
color.toHslString(); // 'hsl(0, 100%, 50%)'
Returns the hex value of the color.
const color = new ColorConverter('red');
color.toHex(); // 'ff0000'
Return a string representation of the hex value, with hash.
const color = new ColorConverter('red');
color.toHexString(); // '#ff0000'
Returns the hex8 value of the color.
const color = new ColorConverter('red');
color.toHex8(); // 'ffff0000'
Return a string representation of the hex8 value, with hash.
const color = new ColorConverter('red');
color.toHex8String(); // '#ffff0000'
Returns the CMY values of the color as an object.
const color = new ColorConverter('red');
color.toCmy(); // { c: 0, m: 100, y: 100 }
Returns the CMYK values of the color as an object.
const color = new ColorConverter('red');
color.toCmyk(); // { c: 0, m: 100, y: 100, k: 0 }
Returns the XYZ values of the color as an object.
const color = new ColorConverter('red');
color.toXyz(); // { x: 41.24, y: 21.26, z: 1.93 }
Return a string representation of the XYZ values.
const color = new ColorConverter('red');
color.toXyzString(); // 'xyz(41.24, 21.26, 1.93)'
Returns the LAB values of the color as an object.
const color = new ColorConverter('red');
color.toLab(); // { l: 53.24, a: 80.09, b: 67.2 }
Return a string representation of the LAB values.
const color = new ColorConverter('red');
color.toLabString(); // 'lab(53.24, 80.09, 67.2)'
Returns the LCH values of the color as an object.
const color = new ColorConverter('red');
color.toLch(); // { l: 53.24, c: 103.54, h: 40.85 }
Return a string representation of the LCH values.
const color = new ColorConverter('red');
color.toLchString(); // 'lch(53.24, 103.54, 40.85)'
Returns the perceived brightness of the color, as a number between 0 and 255.
const color = new ColorConverter('red');
color.getBrightness(); // 76.245
Returns the relative luminance of the color, as a number between 0 and 1.
const color = new ColorConverter('red');
color.getLuminance(); // 0.2126
Returns a boolean indicating whether the color is dark.
const color = new ColorConverter('red');
color.isDark(); // true
Returns a boolean indicating whether the color is light.
const color = new ColorConverter('red');
color.isLight(); // false
Returns the format of the color.
const color = new ColorConverter('fff000');
color.getFormat(); // 'hex'
Returns the alpha value of the color, as a number between 0 and 1.
const color = new ColorConverter('rgba(255, 0, 0, 0.5)');
color.getAlpha(); // 0.5
Sets the alpha value of the color.
const color = new ColorConverter('red');
console.log(color.getAlpha()); // 1
color.setAlpha(0.5);
color.toRgbString(); // 'rgba(255, 0, 0, 0.5)'
Returns the name of the current color if it exists as a named CSS colour, or undefined if it doesn't.
const color = new ColorConverter('rgb(255, 0, 0)');
color.toName(); // 'red'
Returns the name of the nearest named CSS colour, or undefined if it doesn't exist.
const color = new ColorConverter('rgb(253, 10, 0)');
color.toNearestNamedColor(); // 'red'
Returns a boolean indicating whether the color is equal to the supplied color.
const color = new ColorConverter('hsl(0, 100%, 50%)');
color.equals('red'); // true
Returns a random color.
const color = ColorConverter.random();
color.toRgbString(); // 'rgb(252, 27, 65)'
Returns a clone of the current color instance.
const color = new ColorConverter('red');
const clone = color.clone();
color.equals(clone); // true
Returns a WCAG readability score for the color, based on the supplied secondary color.
const color = new ColorConverter('red');
color.readability('white'); // 3.998
Returns an object of booleans indicating whether the color is readable against the supplied secondary color.
const color = new ColorConverter('rgb(13, 114, 103)');
color.isReadable('white');
// {
// AA: {
// large: true,
// normal: true,
// small: false
// },
// AAA: {
// large: true,
// normal: false,
// small: false
// }
// }
Returns a complementary color based on the current color.
const color = new ColorConverter('red');
color.complementary()
// [
// { h: 0, s: 100, l: 50 },
// { h: 180, s: 100, l: 50 },
// ]
Returns a string representation of the complementary color based on the current color.
const color = new ColorConverter('red');
color.complementaryString();
// [
// 'hsl(0, 100%, 50%)',
// 'hsl(180, 100%, 50%)',
// ]
Returns a triad of colors based on the current color.
const color = new ColorConverter('red');
color.triadic()
// [
// { h: 0, s: 100, l: 50 },
// { h: 120, s: 100, l: 50 },
// { h: 240, s: 100, l: 50 },
// ]
Returns a string representation of the triad of colors based on the current color.
const color = new ColorConverter('red');
color.triadicString();
// [
// 'hsl(0, 100%, 50%)',
// 'hsl(120, 100%, 50%)',
// 'hsl(240, 100%, 50%)',
// ]
Returns a tetrad of colors based on the current color.
const color = new ColorConverter('red');
color.tetradic()
// [
// { h: 0, s: 100, l: 50 },
// { h: 90, s: 100, l: 50 },
// { h: 180, s: 100, l: 50 },
// { h: 270, s: 100, l: 50 },
// ]
Returns a string representation of the tetrad of colors based on the current color.
const color = new ColorConverter('red');
color.tetradicString();
// [
// 'hsl(0, 100%, 50%)',
// 'hsl(90, 100%, 50%)',
// 'hsl(180, 100%, 50%)',
// 'hsl(270, 100%, 50%)',
// ]
Returns a split complementary color based on the current color.
const color = new ColorConverter("rgb(147, 64, 191)");
color.splitComplementary(7) // default parameter is 5
// [
// { h: 279, s: 50, l: 50 },
// { h: 330, s: 50, l: 50 },
// { h: 22, s: 50, l: 50 },
// { h: 73, s: 50, l: 50 },
// { h: 125, s: 50, l: 50 },
// { h: 176, s: 50, l: 50 },
// { h: 228, s: 50, l: 50 },
// ]
Returns a string representation of the split complementary color based on the current color.
const color = new ColorConverter("rgb(147, 64, 191)");
color.splitComplementaryString(7) // default parameter is 5
// [
// 'hsl(279, 50%, 50%)',
// 'hsl(330, 50%, 50%)',
// 'hsl(22, 50%, 50%)',
// 'hsl(73, 50%, 50%)',
// 'hsl(125, 50%, 50%)',
// 'hsl(176, 50%, 50%)',
// 'hsl(228, 50%, 50%)',
// ]
Returns a monochromatic palette color based on the current color.
const color = new ColorConverter("rgb(214, 92, 92)");
color.monochromatic(5, 10, 10) // (count, lightnessStep, saturationStep), defaults are (3, 100/count, 100/count)
// [
// { h: 0, s: 20, l: 20 },
// { h: 0, s: 40, l: 40 },
// { h: 0, s: 60, l: 60 },
// { h: 0, s: 80, l: 80 },
// { h: 0, s: 100, l: 100 },
Returns a string representation of the monochromatic palette color based on the current color.
const color = new ColorConverter("rgb(214, 92, 92)");
color.monochromaticString(5, 10, 10) // (count, lightnessStep, saturationStep), defaults are (3, 100/count, 100/count)
// [
// 'hsl(0, 20%, 20%)',
// 'hsl(0, 40%, 40%)',
// 'hsl(0, 60%, 60%)',
// 'hsl(0, 80%, 80%)',
// 'hsl(0, 100%, 100%)',
// ]
TinyColor for inspiring this library.