Warna
Warna is color utility library written in Javascript
to help you parse, convert, or manipulate colors.
It can works on browser or Node.js as npm modules.
Installation
You can install Warna via npm,
npm install warna
or download latest version from Github
Getting Started
Node.js
var warna = require('warna');
var hex = '#ffffff';
var color = warna.parse(hex);
console.log(color.rgb);
Browser
<script src="/path/to/warna.js"></script>
<script>
var hex = '#ffffff';
var color = warna.parse(hex);
console.log(color.rgb);
</script>
Quick Examples
Convert color to RGB or HEX.
var color = warna.parse('#000000');
console.log(color.rgb);
var color = warna.parse(0, 0, 0);
console.log(color.hex);
Get gradient color position or color slices.
var color = warna.gradient('#ffffff', '#000000').getPosition(0.5).hex;
console.log(color);
var color = warna.gradient('#ff0000', '#0000ff').getSlices(3, 'hex');
console.log(color);
Lighten or darken color.
var color = warna.lighten('#ff0000', 0.2).hex;
console.log(color);
var color = warna.darken('#ff0000', 0.2).hex;
console.log(color);
API
parse(color)
Parse color value into Warna object which is a object that contain various format of color, like RGB, HSV, HSL, HEX and Alpha value.
Argument:
color
- (mixed) Color value that will be parsed. It can be a HEX color string, RGB, HSV and HSL object.
Example:
warna.parse('#000000');
warna.parse({red: 255, green: 0, blue: 0});
warna.parse({hue: 0, saturation: 100, value: 100});
warna.parse({hue: 0, saturation: 1, luminosity: 0.5});
gradient(begin, end)
Set gradient color object which can be chained with other gradient utility function like getPosition()
or getSlices()
.
Arguments:
begin
- (mixed) Beginning color value. It can be a HEX color string, RGB object or RGB array.end
- (mixed) Ending color value. It can be a HEX color string, RGB object or RGB array.
getPosition(pos)
Get color value on gradient based on their position and return Warna object.
Argument:
pos
- (float) Float value (percent) of color position.
Example:
var gradient = new warna.Gradient('#ff0000', '#ffffff');
gradient.getPosition(0.5);
var firstColor = {red: 255, green: 0, blue: 0, alpha: 0.25};
var secondColor = {red: 255, green: 255, blue: 255, alpha: 0.75};
var gradient = new warna.Gradient(firstColor, secondColor);
gradient.getPosition(0.5);
getSlices(slice, type)
Get color slices of a gradient and return array of Warna object based on number slices you want to get. Slice should be more than 2
, if you want to get other color value other than start color and ending color.
Argument:
slice
- (integer) Integer value of number slices you want to get.type
- (string) Type of color you want to output (rgb
, hex
, hsv
, or hsl
).
Example:
warna.gradient('#ffffff', '#000000').getSlices(3, 'hex');
lighten(color, pos)
Lighten a color. Return warna object.
Arguments:
color
- (mixed) Color value that will be lighten. It can be a HEX color string, RGB object or RGB array.pos
- (float) Float value of lighten percentation. 0
will returning base color and 1
will return white #ffffff
.
Example:
warna.lighten('#ff0000', 0.2);
darken(color, pos)
Darken a color.
Arguments:
color
- (mixed) Color value that will be lighten. It can be a HEX color string, RGB object or RGB array.pos
- (float) Float value of lighten percentation. 0
will returning base color and 1
will return black #000000
.
Example:
warna.darken('#ff0000', 0.2);
Copyright and license
Copyright 2014 Alfiana E. Sibuea. Code released under the MIT license.