Farbe: A Comprehensive Color Manipulation Library for JavaScript
Project Source | NPM Package | Documentation
Introduction
Farbe (German for “color”) is a powerful and versatile JavaScript library designed for color manipulation and conversion. Whether you’re building a design tool, creating data visualizations, or developing a theme system for your application, Farbe provides all the tools you need to work with colors effectively.
This library supports all major color formats (HEX, RGB, RGBA, HSL, HSLA, HSV, CMYK) and offers a wide range of color manipulation functions, making it an essential tool for front-end developers and designers.
Key Features
- Multiple Color Format Support: Work with HEX, RGB, RGBA, HSL, HSLA, HSV, and CMYK color formats
- Seamless Conversion: Convert between any supported color formats with ease
- Color Manipulation: Lighten, darken, saturate, desaturate, and more
- Color Schemes: Generate harmonious color schemes (monochromatic, complementary, analogous, etc.)
- Color Analysis: Determine if a color is light or dark, compare colors
- Channel Manipulation: Modify individual color channels
- Color Blending: Mix, multiply, and blend colors together
- Web-safe Colors: Convert colors to web-safe equivalents
Getting Started
Installation
npm install @olton/farbe
Basic Usage
import { Farbe } from '@olton/farbe';
// Create a new color instanceconst color = new Farbe('#ff0000'); // Red
// Convert to different formatsconsole.log(color.rgb); // RGB object: {r: 255, g: 0, b: 0}console.log(color.hex); // "#ff0000"console.log(color.hsl); // HSL object: {h: 0, s: 1, l: 0.5}
// Manipulate the colorcolor.lighten(20); // Lighten by 20%console.log(color.hex); // "#ff1414"
color.toHSL(); // Convert to HSL formatcolor.saturate(10); // Increase saturation by 10%
Color Conversion
Farbe makes it easy to convert between different color formats:
const color = new Farbe('#3498db');
// Convert to different formatscolor.toRGB(); // Convert to RGBcolor.toRGBA(0.5); // Convert to RGBA with 0.5 alphacolor.toHEX(); // Convert to HEXcolor.toHSV(); // Convert to HSVcolor.toHSL(); // Convert to HSLcolor.toHSLA(0.8); // Convert to HSLA with 0.8 alphacolor.toCMYK(); // Convert to CMYK
// Get color in different formats without changing the internal representationconsole.log(color.rgb); // Get as RGBconsole.log(color.rgba); // Get as RGBAconsole.log(color.hex); // Get as HEXconsole.log(color.hsv); // Get as HSVconsole.log(color.hsl); // Get as HSLconsole.log(color.hsla); // Get as HSLAconsole.log(color.cmyk); // Get as CMYK
Color Manipulation
Farbe provides a rich set of methods to manipulate colors:
const color = new Farbe('#3498db');
// Basic manipulationscolor.darken(20); // Darken by 20%color.lighten(10); // Lighten by 10%color.saturate(15); // Increase saturation by 15%color.desaturate(10); // Decrease saturation by 10%color.grayscale(); // Convert to grayscalecolor.brighten(20); // Brighten by 20%
// Hue manipulationcolor.hueShift(30); // Shift hue by 30 degreescolor.spin(90); // Rotate hue by 90 degrees
// Channel manipulationcolor.channel('red', 128); // Set red channel to 128color.channel('saturation', 0.8); // Set saturation to 0.8color.channel('hue', 180); // Set hue to 180
Color Schemes
Generate harmonious color schemes with Farbe:
const color = new Farbe('#3498db');
// Generate different color schemesconst monoScheme = color.getScheme('mono', 'hex');const complementaryScheme = color.getScheme('complement', 'rgb');const triadicScheme = color.getScheme('triad', 'hsl');const analogousScheme = color.getScheme('analog', 'hex');const splitComplementaryScheme = color.getScheme('split', 'rgb');const tetradicScheme = color.getScheme('tetra', 'hex');const squareScheme = color.getScheme('square', 'hsl');
// Material design color paletteconst materialPalette = color.getScheme('material', 'hex');
Color Blending
Blend and mix colors together:
const color = new Farbe('#ff0000'); // Red
// Mix with blue (50% mix)color.mix('#0000ff', 50); // Results in purple (#800080)
// Add colorscolor.add('#00ff00'); // Add green
// Multiply colorscolor.multiply('#0000ff'); // Multiply with blue
Color Analysis
Analyze and compare colors:
const color = new Farbe('#3498db');
// Check if color is dark or lightif (color.isDark()) { // Use light text on dark background} else { // Use dark text on light background}
// Compare colorsconst isEqual = color.equal('#3498db'); // true
Random Colors
Generate random colors:
const color = new Farbe();
// Generate a random colorcolor.random(); // Random HEX color
// Generate a random color in a specific formatcolor.random('rgb'); // Random RGB colorcolor.random('hsv', 0.5); // Random HSV color with 0.5 alpha
Web-safe Colors
Convert colors to web-safe equivalents:
const color = new Farbe('#123456');
// Convert to web-safe colorcolor.toWebsafe(); // Results in #003366
Advanced Usage
Custom Options
You can customize the behavior of Farbe by providing options:
const color = new Farbe('#3498db', { alpha: 0.5, // Default alpha value for RGBA/HSLA angle: 45, // Default angle for hue shifting distance: 10, // Distance for color schemes step: 0.2, // Step size for color schemes tint1: 0.9, // Tint factor 1 for monochromatic schemes tint2: 0.5, // Tint factor 2 for monochromatic schemes shade1: 0.7, // Shade factor 1 for monochromatic schemes shade2: 0.4, // Shade factor 2 for monochromatic schemes});
Method Chaining
Farbe supports method chaining for a more concise and readable code:
const result = new Farbe('#3498db') .toHSL() .saturate(20) .lighten(10) .hueShift(30) .toRGB() .toString();
Conclusion
Farbe is a comprehensive color manipulation library that provides all the tools you need to work with colors in JavaScript. With its intuitive API and extensive feature set, it’s an excellent choice for any project that involves color manipulation.
Whether you’re building a design tool, creating data visualizations, or developing a theme system for your application, Farbe has you covered with its powerful color manipulation capabilities.
License
Farbe is licensed under the MIT License. See the LICENSE file for details.