项目作者: t28hub

项目描述 :
A fluent color conversion and manipulation library.
高级语言: JavaScript
项目地址: git://github.com/t28hub/kolour.git
创建时间: 2016-01-14T04:08:02Z
项目社区:https://github.com/t28hub/kolour

开源协议:MIT License

下载


Kolour

npm version
Build Status
Dependency Status

Kolour is a fluent color conversion and manipulation library.

Installation

Using bower:

  1. $ bower install kolour

Using npm:

  1. $ npm install kolour

Supported Color Spaces

Kolour supports the following color spaces.

Example

Here is an example using the Kolour.

  1. const rgb = kolour('black');
  2. rgb.r(40).g(60).b(80);
  3. rgb.hex(); // #283C50
  4. rgb.css(); // rgb(40, 60, 80)
  5. const hsl = color.hsl();
  6. hsl.h(120).s(40).l(80);
  7. hsl.css(); // hsl(120, 40%, 80%)
  8. hsl.alpha(0.5);
  9. hsl.css(); // hsl(120, 40%, 80%, 0.5)

API

Creation

Kolour provides a simple way to create a color and you only have to call the method.

  1. const white = kolour('#FFFFFF'); // Create a color from a hex string
  2. const green = kolour('green'); // Create a color from a color name
  3. const hsl = kolour('hsl(0, 100%, 50%)') // Create a color from a css color
  4. const cmy = kolour({c: 1, m: 0, y: 0, a: 1}); // Create a color from an object
  5. const cyan = kolour(0xFF00FFFF); // Create a color from a 32-bit ARGB number
  6. const cloned = kolour(black); // Create a color with another instance
  7. const invalid = kolour(null); // Create an invalid color
  8. const random = kolour.random(); // Create a random RGB color

If you need to create an instance of a specific color space, you can use a method that has the same name as a color space.

  1. const cmy = kolour.cmy(0.2, 0.4, 0.6);
  2. const cmyk = kolour.cmyk(0.2, 0.4, 0.6, 0.8);
  3. const hsl = kolour.hsl(120, 0.4, 0.6);
  4. const hsv = kolour.hsv(120, 0.4, 0.6);
  5. const hwb = kolour.hwb(120, 0.4, 0.6);
  6. const rgb = kolour.rgb(80, 120, 160);

Accessor

Kolour provides two types of accessor for each component.

  1. const rgb = kolour('rgb(60, 120, 180)');
  2. rgb.r(); // Get red value (shorthand method)
  3. rgb.red(); // Get red value
  4. rgb.r(80); // Set red value (shorthand method)
  5. rgb.red(80); // Set red value

In addition, ‘setter’ returns self so you can set values fluently as below.

  1. const rgb = kolour('rgb(60, 120, 180)');
  2. rgb.r(40).g(80).b(120).a(0.5); // {r: 40, g: 80, b: 120, a: 0.5}
  3. rgb.red(40).green(80).blue(120).alpha(0.5); // {r: 40, g: 80, b: 120, a: 0.5}

Conversion

Kolour provides the following color conversion methods.

  1. color.cmy(); // Convert to a CMY color
  2. color.cmyk(); // Convert to a CMYK color
  3. color.hsl(); // Convert to a HSL color
  4. color.hsv(); // Convert to a HSV color
  5. color.hwb(); // Convert to a HWB color
  6. color.rgb(); // Convert to a RGB color
  7. color.hex(); // Convert to a hex string

Manipulation

kolour provides the following manipulation methods and they are related to less and sass.

  1. color.lighten(0.1); // Increase the lightness
  2. color.darken(0.1); // Decrease the lightness
  3. color.grayscale(); // Convert to a grayscale color
  4. color.whiten(0.1); // Increase the whiteness
  5. color.blacken(0.1); // Increase the blackness
  6. color.invert(); // Convert to a negative color
  7. color.negate(); // Convert to a negative color
  8. color.rotate(120); // Rotate he hue
  9. color.spin(120); // Rotate he hue
  10. color.complement(); // Convert to a complementary color
  11. color.fadein(0.1); // Increase the alpha
  12. color.fadeout(0.1); // Decrease the alpha
  13. color.mix(other, 0.5); // Mix with a color
  14. color.tint(0.5); // Mix with a white color
  15. color.shade(0.5); // Mix with a black color

Others

Kolour provides other methods as below.

  1. color.css(); // Return a css color styled string
  2. color.isValid(); // Return true when the color is valid
  3. color.equals(other); // Return true when the specified color is same color

License

MIT