A lightweight utility library for converting colors between HEX, RGB, and HSL formats. Simplify your color manipulation tasks with this easy-to-use and precise library.
Colors are an integral part of web and application development. Whether you're styling a webpage, creating graphics, or building a custom color picker, you often need to convert colors between different formats (e.g., HEX, RGB, HSL).
Ultimate Color Converter provides a robust, reliable, and precise way to perform these conversions.
- Convert HEX to RGB: Easily convert HEX color codes (e.g., #ffffff) into RGB format.
- Convert RGB to HEX: Transform RGB values into a HEX color code.
- Convert RGB to HSL: Get the HSL representation from an RGB color.
- Convert HSL to RGB: Transform HSL values into RGB format.
- Validation: Handles invalid inputs with meaningful error messages.
Install the library via npm:
npm install ultimate-color-converter
import {
hexToRgb,
rgbToHex,
rgbToHsl,
hslToRgb,
} from "ultimate-color-converter";
import { hexToRgb } from "ultimate-color-converter";
const rgb = hexToRgb("#ffffff");
console.log(rgb);
/*
Output: { r: 255, g: 255, b: 255 }
*/
import { rgbToHex } from "ultimate-color-converter";
const hex = rgbToHex({ r: 255, g: 255, b: 255 });
console.log(hex);
/*
Output: "#ffffff"
*/
import { rgbToHsl } from "ultimate-color-converter";
const hsl = rgbToHsl({ r: 255, g: 255, b: 255 });
console.log(hsl);
/*
Output: { h: 0, s: 0, l: 100 }
*/
import { hslToRgb } from "ultimate-color-converter";
const rgb = hslToRgb({ h: 0, s: 0, l: 100 });
console.log(rgb);
/*
Output: { r: 255, g: 255, b: 255 }
*/
-
Description: Converts a HEX color code to RGB format.
-
Parameters:
- hex (string): The HEX color code (e.g., #ffffff or ffffff).
-
Returns:
- RGB (object): { r: number, g: number, b: number }.
-
Throws:
- Error if the HEX code is invalid.
-
Description: Converts an RGB color to a HEX color code.
-
Parameters:
- rgb (object): { r: number, g: number, b: number }.
-
Returns:
- HEX (string): A valid HEX color code (e.g., #ffffff).
-
Throws:
- Error if RGB values are not between 0 and 255.
-
Description: Converts an RGB color to HSL format.
-
Parameters:
- rgb (object): { r: number, g: number, b: number }.
-
Returns:
-
HSL (object): { h: number, s: number, l: number }, where:
- h is the hue (0–360).
- s is the saturation percentage (0–100).
- l is the lightness percentage (0–100).
-
-
Description: Converts an HSL color to RGB format.
-
Parameters:
- hsl (object): { h: number, s: number, l: number }.
-
Returns:
- RGB (object): { r: number, g: number, b: number }.
The library validates input values and throws meaningful error messages for invalid inputs. For example:
-
hexToRgb("#zzz"); // Throws: Error: Invalid HEX color format
-
rgbToHex({ r: 300, g: 255, b: 255 }); // Throws: Error: RGB values must be between 0 and 255
- Accuracy: Precision in conversions ensures your colors are always correct.
- Efficiency: Lightweight and optimized for performance.
- Error-Resistant: Built-in validation handles invalid inputs gracefully.
- Developer-Friendly: Clear documentation and simple API make it easy to use.
We welcome contributions! If you find bugs or have ideas for new features, please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License. See the LICENSE file for more details.