What is onecolor?
The onecolor npm package is a versatile color manipulation library that allows you to work with colors in various formats, convert between them, and perform operations like blending and adjusting color properties.
What are onecolor's main functionalities?
Color Creation
You can create a color object from a string representing a color in various formats (hex, RGB, HSL, etc.). The created color object can then be used for further manipulations.
const onecolor = require('onecolor');
const color = onecolor('#3498db');
console.log(color.css()); // Outputs: 'rgb(52, 152, 219)'
Color Conversion
The package allows you to convert colors between different formats such as HEX, RGB, and HSL.
const onecolor = require('onecolor');
const color = onecolor('#3498db');
console.log(color.hex()); // Outputs: '#3498db'
console.log(color.rgb()); // Outputs: 'rgb(52, 152, 219)'
console.log(color.hsl()); // Outputs: 'hsl(204, 0.68, 0.53)'
Color Manipulation
You can manipulate color properties like lightness, saturation, and hue. This example demonstrates adjusting the lightness of a color.
const onecolor = require('onecolor');
const color = onecolor('#3498db').lightness(0.5);
console.log(color.css()); // Outputs: 'rgb(26, 76, 109)'
Color Blending
The package supports blending two colors together. This example shows how to blend two colors with equal weight.
const onecolor = require('onecolor');
const color1 = onecolor('#3498db');
const color2 = onecolor('#e74c3c');
const blendedColor = color1.mix(color2, 0.5);
console.log(blendedColor.css()); // Outputs: 'rgb(153, 101, 123)'
Other packages similar to onecolor
chroma-js
Chroma.js is a powerful and flexible color manipulation library that supports a wide range of color spaces and provides advanced color manipulation features. Compared to onecolor, chroma-js offers more extensive functionality and better performance for complex color operations.
color
The color package is another popular library for color manipulation and conversion. It provides a simple API for working with colors and supports various color models. While it offers similar functionalities to onecolor, it is known for its ease of use and comprehensive documentation.
tinycolor2
TinyColor is a small, fast library for color manipulation and conversion. It is designed to be lightweight and easy to use, making it a good choice for projects where performance and simplicity are important. Compared to onecolor, TinyColor is more focused on providing a minimalistic API with essential color manipulation features.
one.color
one.color is a JavaScript color calculation toolkit.
Works in node.js and in the browser.
The color object automatically installs implicit conversions to different color spaces when needed, and adds get/set/adjust methods of any color channel in any available color space.
All implicit conversions are done using floating point, so no precision is lost due to rounding errors when converting between color spaces.
Features
- RGB, HSV, HSL and CMYK color space implementations
- Alpha channel
- Extensible architecture - implement your own color spaces easily
- Chainable color manipulation
- Seamless conversion between color spaces on demand
- Outputs as Hex, RGB or HSV in css syntax with or without alpha channel
Usage Example
one.color.(RGB|HSL|HSV|CMYK) objects are designed to be immutable; all the conversion, set, and adjust methods return new objects.
one.color.(RGB|HSL|HSV|CMYK) objects automatically get the set and adjust methods from all other installed colorspaces, so although you can use the explicit conversion methods such as one.color.RGB.hsl(), one.color.RGB.cmyk():
Example::
new one.color.RGB(.4, .3, .9).
lightness(+.2, true). // Implicit conversion to HSL
red(-.1). // Implicit conversion back to RGB
hex(); // "#00a6f2"
Documentation
Base class overview::
one.color = function( one.color.* | Array | String | Int ), // Parser
one.color.installColorSpace: function ( str space, [str channelNames], obj methods )
one.color.RGB = function ( [0;1], [0;1], [0;1], [0;1] ); // Constructor
one.color.HSV = function ( [0;1], [0;1], [0;1], [0;1] ); // Constructor
one.color.HSL = function ( [0;1], [0;1], [0;1], [0;1] ); // Constructor
one.color.CMYK = function ( [0;1], [0;1], [0;1], [0;1] ); // Constructor
Methods on an instance of one.color.( RGB | HSL | HSV | CMYK )::
toString() → "[one.color.RGB: Red=0 Green=0 Blue=0 Alpha=1]" // For debugging
hex() → "#RRGGBB"
css() → "rgb([0;255], [0;255], [0;255])"
cssa() → "rgb([0;255], [0;255], [0;255], [0;1])"
toJSON() → ["RGB", [0;1], [0;1], [0;1], [0;1]] // As understood by one.color(Array)
equals(otherColor, ε = 10^-9) → Boolean
[colorSpace]() → one.color[colorSpace] // Conversion method
[channelName]() → Number [0;1] // Get, implicit color space conversion
[channelName]([0;1]) → new one.color[colorSpace], channelName = [0;1] // Set, implicit color space conversion
[channelName](±[0;1], true) → new one.color[colorSpace], channelName ±= [0;1] // Adjust, implicit color space conversion
The API is documented in the source code and can be built using JSDoc <http://code.google.com/p/jsdoc-toolkit/>
_.
Building
The Makefile uses AssetGraph <https://github.com/One-com/assetgraph>
_ for resolving the JavaScript dependencies.
If you aren't up for a complete installation, take a look at the pre-built packages:
one-color.js <https://raw.github.com/One-com/one-color/master/one-color.js>
_one-color-debug.js <https://raw.github.com/One-com/one-color/master/one-color-debug.js>
_
License
one.color is licensed under a standard 3-clause BSD license -- see the LICENSE-file for details.