The library aims to simplify and standardize color management in web development. It provides a convenient and structured tool for creating, organizing, and managing colors in projects.
Features
- Create Color Objects: Create color objects with red (r), green (g), and blue (b) components and assign unique tokens to them.
- Convert to Different Formats: Easily convert colors to different formats such as RGB and hexadecimal.
- Colors creating: Use the ColorsBuilder class to create your colors collection.
- Get Color Information: Access color properties and methods for comprehensive color information.
You can install the library using npm or yarn.
For more information on using npm check out the docs here.
npm install @njs-lib/colors --saveFor more information on using yarn check out the docs here.
yarn add @njs-lib/colorsThe Color class represents a color object with red (r), green (g), and blue (b) components.
Creates a new Color instance.
r: The red component (0-255).g: The green component (0-255).b: The blue component (0-255).token: A unique token for the color.
Gets the red component of the color (0-255).
Gets the green component of the color (0-255).
Gets the blue component of the color (0-255).
Gets the token associated with the color.
Converts the color to an RGB string representation (e.g., 'rgb(255, 0, 0)').
Converts the color to an RGBA string representation (e.g., 'rgba(255, 0, 0, 0.5)').
Converts the color to a hexadecimal string representation (e.g., '#FF0000').
Converts the color to a Human-Readable HSL string representation (e.g., 'hsl(0deg, 100%, 50%)').
Converts the color to an array of red, green, and blue components.
import { Color } from '@njs-lib/colors';
const red = new Color(255, 0, 0, 'red');
red.toHex(); // Output: #FF0000
red.toRgb(); // Output: rgb(255, 0, 0)
red.toRgba(0.2); // Output: rgba(255, 0, 0, 0.2)
red.toHsl(); // Output: hsl(0deg, 100%, 50%)
red.toArray(); // Output: [255, 0, 0]A builder class for creating colors. This class provides a fluent interface for adding colors, enabling exception skipping, creating colors based on added color definitions and destroying the instance.
Creates a new ColorBuilder instance.
Adds a color with the specified RGB values and token.
r(number): The red component (0-255).g(number): The green component (0-255).b(number): The blue component (0-255).token(string): A unique token for the color.
Enables skipping exceptions when creating colors.
Creates colors based on added color definitions.
Destroys the ColorsBuilder instance and clears all data.
import { ColorsBuilder } from '@njs-lib/colors';
const colorsBuilder = new ColorsBuilder()
.addColor(255, 255, 255, 'white')
.addColor(255, 0, 0, 'red')
.addColor(0, 255, 0, 'green')
.create();
colorsBuilder.destroy();Manages and interacts with color data.
Get the total number of colors.
Get a list of color tokens.
Get a list of color types.
Get color values in the specified format (e.g., 'toHex' or 'toRgb' or 'toHsl').
Get a color by its token (e.g., 'white' or 'black').
Set the Colors class to use default color values.
import { Colors } from '@njs-lib/colors';
Colors.getTotal(); // 2
Colors.getTokens(); // ["black", "white"]
Colors.getTypes(); // ["toHex", "toRgb", "toHsl"]
Colors.getColor('black'); // Black Color
Colors.getColor('black').toHex(); // #000000
Colors.getColor('black').toRgb(); // rgb(0, 0, 0)
Colors.getColor('black').toRgba(0.4); // rgba(0, 0, 0, 0.4)
Colors.getColor('black').toHsl(); // hsl(0deg, 0%, 0%)
Colors.getValues('toHex'); // ["#000000", "#ffffff"]
Colors.getValues('toRgb'); // ["rgb(0, 0, 0)", "rgb(255, 255, 255)"]
Colors.getValues('toHsl'); // ["hsl(0deg, 0%, 0%)", "hsl(0deg, 0%, 100%)"]
Colors.useDefault().getTotal(); // 141
Colors.useDefault().getTokens(); // ["AliceBlue", "AntiqueWhite", etc]
Colors.useDefault().getTypes(); // ["toHex", "toRgb", "toHsl"]
Colors.useDefault().getColor('AliceBlue'); // AliceBlue Color