This library provides classes for converting colors between different formats such as RGB, HSL, and HEX.
You can install this library via npm:
npm install @terron/colors
// Import the classes
const { RGB, HEX, HSL } = require('@terron/colors');
// Create instances of color classes
const rgbColor = new RGB(255, 0, 0);
const hslColor = new HSL(0, 100, 50);
const hexColor = new HEX('#FF0000');
// Convert between color formats
const rgbToHsl = rgbColor.toHSL(); // [0, 100, 50]
const hslToRgb = hslColor.toRGB(); // [255, 0, 0]
const hexToRgb = hexColor.toRGB(); // [255, 0, 0]
Represents a color in RGB format.
Creates an instance of RGB.
new RGB(r, g, b);
-
r
(number|Object|Array): The red component, or an object/array containing RGB values, or an array representing RGB values. -
g
(number): The green component. -
b
(number): The blue component.
-
isValid(r, g, b)
: Checks if provided RGB values are valid.
-
toHEX()
: Converts RGB values to hexadecimal format. -
toHSL()
: Converts RGB values to HSL format. -
toRGB()
: Retrieves the RGB values.
Represents a color in HSL format.
Creates an instance of HSL.
new HSL(h, s, l);
-
h
(number|Object|Array): The hue component, or an object/array containing HSL values, or an array representing HSL values. -
s
(number): The saturation component. -
l
(number): The lightness component.
-
isValid(h, s, l)
: Checks if provided HSL values are valid.
-
toHEX()
: Converts HSL values to hexadecimal format. -
toRGB()
: Converts HSL values to RGB format. -
toHSL()
: Retrieves the HSL values.
Represents a color in hexadecimal format.
Creates an instance of HEX.
new HEX(hex);
-
hex
(string): The hexadecimal color code.
-
isValid(hex)
: Checks if the provided hexadecimal color code is valid.
-
toRGB()
: Converts the hexadecimal color code to RGB format. -
toHSL()
: Converts the hexadecimal color code to HSL format. -
toHEX()
: Retrieves the hexadecimal color code.