29e1f3563cd3fa7607f4086ffd270e6411d5adae2a5cabf8bef2e477fc0cbc9e12f0abd237f11aaef0777f41b3120f812b8af0c14961d11d50d33ebdf16151 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # color [![Build Status](https://travis-ci.org/Qix-/color.svg?branch=master)](https://travis-ci.org/Qix-/color)
  2. > JavaScript library for color conversion and manipulation with support for CSS color strings.
  3. ```js
  4. var color = Color("#7743CE");
  5. color.alpha(0.5).lighten(0.5);
  6. console.log(color.hslString()); // "hsla(262, 59%, 81%, 0.5)"
  7. ```
  8. ## Install
  9. ```console
  10. $ npm install color
  11. ```
  12. ## Usage
  13. ```js
  14. var Color = require("color")
  15. ```
  16. ### Setters
  17. ```js
  18. var color = Color("rgb(255, 255, 255)")
  19. var color = Color({r: 255, g: 255, b: 255})
  20. var color = Color().rgb(255, 255, 255)
  21. var color = Color().rgb([255, 255, 255])
  22. ```
  23. Pass any valid CSS color string into `Color()` or a hash of values. Also load in color values with `rgb()`, `hsl()`, `hsv()`, `hwb()`, and `cmyk()`.
  24. ```js
  25. color.red(120)
  26. ```
  27. Set the values for individual channels with `alpha`, `red`, `green`, `blue`, `hue`, `saturation` (hsl), `saturationv` (hsv), `lightness`, `whiteness`, `blackness`, `cyan`, `magenta`, `yellow`, `black`
  28. ### Getters
  29. ```js
  30. color.rgb() // {r: 255, g: 255, b: 255}
  31. ```
  32. Get a hash of the rgb values with `rgb()`, similarly for `hsl()`, `hsv()`, and `cmyk()`
  33. ```js
  34. color.rgbArray() // [255, 255, 255]
  35. ```
  36. Get an array of the values with `rgbArray()`, `hslArray()`, `hsvArray()`, and `cmykArray()`.
  37. ```js
  38. color.red() // 255
  39. ```
  40. Get the value for an individual channel.
  41. ### CSS Strings
  42. ```js
  43. color.hslString() // "hsl(320, 50%, 100%)"
  44. ```
  45. Different CSS String formats for the color are on `hexString`, `rgbString`, `percentString`, `hslString`, `hwbString`, and `keyword` (undefined if it's not a keyword color). `"rgba"` and `"hsla"` are used if the current alpha value of the color isn't `1`.
  46. ### Luminosity
  47. ```js
  48. color.luminosity(); // 0.412
  49. ```
  50. The [WCAG luminosity](http://www.w3.org/TR/WCAG20/#relativeluminancedef) of the color. 0 is black, 1 is white.
  51. ```js
  52. color.contrast(Color("blue")) // 12
  53. ```
  54. The [WCAG contrast ratio](http://www.w3.org/TR/WCAG20/#contrast-ratiodef) to another color, from 1 (same color) to 21 (contrast b/w white and black).
  55. ```js
  56. color.light(); // true
  57. color.dark(); // false
  58. ```
  59. Get whether the color is "light" or "dark", useful for deciding text color.
  60. ### Manipulation
  61. ```js
  62. color.negate() // rgb(0, 100, 255) -> rgb(255, 155, 0)
  63. color.lighten(0.5) // hsl(100, 50%, 50%) -> hsl(100, 50%, 75%)
  64. color.darken(0.5) // hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
  65. color.saturate(0.5) // hsl(100, 50%, 50%) -> hsl(100, 75%, 50%)
  66. color.desaturate(0.5) // hsl(100, 50%, 50%) -> hsl(100, 25%, 50%)
  67. color.greyscale() // #5CBF54 -> #969696
  68. color.whiten(0.5) // hwb(100, 50%, 50%) -> hwb(100, 75%, 50%)
  69. color.blacken(0.5) // hwb(100, 50%, 50%) -> hwb(100, 50%, 75%)
  70. color.clearer(0.5) // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
  71. color.opaquer(0.5) // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 1.0)
  72. color.rotate(180) // hsl(60, 20%, 20%) -> hsl(240, 20%, 20%)
  73. color.rotate(-90) // hsl(60, 20%, 20%) -> hsl(330, 20%, 20%)
  74. color.mix(Color("yellow")) // cyan -> rgb(128, 255, 128)
  75. color.mix(Color("yellow"), 0.3) // cyan -> rgb(77, 255, 179)
  76. // chaining
  77. color.green(100).greyscale().lighten(0.6)
  78. ```
  79. ### Clone
  80. You can can create a copy of an existing color object using `clone()`:
  81. ```js
  82. color.clone() // -> New color object
  83. ```
  84. And more to come...
  85. ## Propers
  86. The API was inspired by [color-js](https://github.com/brehaut/color-js). Manipulation functions by CSS tools like Sass, LESS, and Stylus.