In the browser, the parser is exposed as a global named onecolor.
In node.js, it is returned directly with a require of the module
(after npm install onecolor):
var color = require('onecolor');console.warn(color('rgba(100%, 0%, 0%, .5)').alpha(0.4).cssa());
rgba(255,0,0,0.4)
All of the above return color instances in the relevant color space
with the channel values (0..1) as instance variables:
var myColor = color('#a9d91d');myColor instanceof color.RGB; // truemyColor.red(); // 0.6627450980392157
You can also parse named CSS colors (works out of the box in node.js,
but the requires the slightly bigger one-color-all.js build in the
browser):
Color instances have getters/setters for all channels in all supported
colorspaces (red(), green(), blue(), hue(), saturation(), lightness(),
value(), alpha(), etc.). Thus you don't need to think about which colorspace
you're in. All the necessary conversions happen automatically:
color('#ff0000') // Red in RGB .green(1) // Set green to the max value, producing yellow (still RGB) .hue(0.5, true) // Add 180 degrees to the hue, implicitly converting to HSV .hex(); // Dump as RGB hex syntax: '#2222ff'
When called without any arguments, they return the current value of
the channel (0..1):
When called with a single numerical argument (0..1), a new color
object is returned with that channel replaced:
var myColor = color('#00ddff');myColor.red(0.5).red(); // .5// ... but as the objects are immutable, the original object retains its value:myColor.red(); // 0
When called with a single numerical argument (0..1) and true as
the second argument, a new value is returned with that channel
adjusted:
color('#ff0000') // Red .red(-0.1, true) // Adjust red channel by -0.1 .hex(); // '#e60000'
Alpha channel
All color instances have an alpha channel (0..1), defaulting to 1
(opaque). You can simply ignore it if you don't need it.
It's preserved when converting between colorspaces:
Getters -- return the value of the channel (converts to other colorspaces as needed):
var myColor = color('#bda65b');myColor.red();myColor.green();myColor.blue();myColor.hue();myColor.saturation();myColor.value();myColor.lightness();myColor.alpha();myColor.cyan(); // one-color-all.js and node.js onlymyColor.magenta(); // one-color-all.js and node.js onlymyColor.yellow(); // one-color-all.js and node.js onlymyColor.black(); // one-color-all.js and node.js only
Setters -- return new color instances with one channel changed:
color.red(<number>)color.green(<number>)color.blue(<number>)color.hue(<number>)color.saturation(<number>)color.value(<number>)color.lightness(<number>)color.alpha(<number>)color.cyan(<number>) // one-color-all.js and node.js onlycolor.magenta(<number>) // one-color-all.js and node.js onlycolor.yellow(<number>) // one-color-all.js and node.js onlycolor.black(
Adjusters -- return new color instances with the channel adjusted by
the specified delta (0..1):
color.red(<number>, true)color.green(<number>, true)color.blue(<number>, true)color.hue(<number>, true)color.saturation(<number>, true)color.value(<number>, true)color.lightness(<number>, true)color.alpha(<number>, true)color.cyan(<number>, true) // one-color-all.js and node.js onlycolor.magenta(<number>, true) // one-color-all.js and node.js onlycolor.yellow(<number>, true) // one-color-all.js and node.js only
Comparison with other color objects, returns true or false (epsilon defaults to 1e-9):
color.equals(otherColor[, <epsilon>])
Mostly for internal (and plugin) use:
"Low level" constructors, accept 3 or 4 numerical arguments (0..1):
new onecolor.CMYK(<cyan>, <magenta>, <yellow>, <black>[, <alpha>])
All color instances have rgb(), hsv(), and hsl() methods for
explicitly converting to another color space. Like the setter and
adjuster methods they return a new color object representing the same
color in another color space.
If for some reason you need to get all the channel values in a
specific colorspace, do an explicit conversion first to cut down on
the number of implicit conversions: