npm install real-colors
or
yarn add real-colors
import { createColorInstance } from 'real-colors';
// You may pass in a color map to createColorInstance
const Color = createColorInstance({ myColor: '#ff00ff' });
// Sample usage
const color = Color.named('myColor');
console.log(color.hexString); // #FF00FF
Creates a color from a hex string.
Creates a color from RGB values.
Creates a color from HSL values.
Creates a random color.
Creates a seeded random color.
Creates a color from a named color. There is a set of named colors available by default, but you can add your own by passing in a color map to createColorInstance
.
Resolves a color from a color or string. If a string is passed in, it will be parsed as a hex
, rgb
, rgba
, hsl
, or hsla
color. If a color is passed in, it will be returned as is. If neither a string nor a color is passed in, an undefined behavior will occur.
Gets the hex string of the color.
Gets the hex string of the color. This will include alpha value if it is not 1.
Gets the hex string of the color without the pound sign.
Gets the hex string of the color without the alpha value. This is equivalent to Color.hexString
.
Gets the RGB values of the color.
Gets the RGBA values of the color.
Gets the RGB string of the color in the form of rgb(r, g, b)
.
Gets the RGBA string of the color in the form of rgba(r, g, b, a)
.
Gets the HSL values of the color.
Gets the HSLA values of the color.
Gets the HSL string of the color in the form of hsl(h, s%, l%)
.
Gets the HSLA string of the color in the form of hsla(h, s%, l%, a)
.
Gets the Kelvin temperature of the color.
Gets the contrast ratio of the color relative to some arbitrary reference.
Sets the opacity of the color. The value may be in range 0-1 (float) or 0-100 (integer).
Lightens the color by the given amount. The value may be in range 0-1 (float) or 0-100 (integer).
Darkens the color by the given amount. The value may be in range 0-1 (float) or 0-100 (integer).
Saturates the color by the given amount. The value may be in range 0-1 (float) or 0-100 (integer).
Desaturates the color by the given amount. The value may be in range 0-1 (float) or 0-100 (integer).
Rotates the color by the given amount. The value may be in range 0-360 (integer).
Shifts the RGB values of the color by the given amount. The value may be in range 0-255 (integer).
Not implemented yet
Gets the complement of the color.
Gets the monochromatic colors of the color (10 values);
Gets the triadic colors of the color.
Gets the grayscale color of the color.
Generates a new colors based on a 3x3 matrix transformation. The formula is:
[ r' ] [ a b c ] [ r ]
[ g' ] = [ d e f ] * [ g ]
[ b' ] [ g h i ] [ b ]
The matrix is represented as an array of 9 numbers. The result is a new color with the same alpha value as the original color. All new values are clamped to the range 0-255 (integer).
Clamps a color value to the range 0-255 (integer).
Inverts a Color. The value may be in range 0-1 (float).
Applies a linear transformation to a Color.
Applies a linear contrast transformation to a Color.
Applies a linear brightness transformation to a Color.
Applies a sepia transformation to a color;
Checks if a foreground color is accessible on a background color. Returns true if the contrast ratio is greater than 4.5, false otherwise.