You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.2 KiB

4 years ago
  1. # color-string
  2. [![Build Status](https://travis-ci.org/Qix-/color-string.svg?branch=master)](https://travis-ci.org/Qix-/color-string)
  3. > library for parsing and generating CSS color strings.
  4. ## Install
  5. With [npm](http://npmjs.org/):
  6. ```console
  7. $ npm install color-string
  8. ```
  9. ## Usage
  10. ### Parsing
  11. ```js
  12. colorString.get('#FFF') // {model: 'rgb', value: [255, 255, 255, 1]}
  13. colorString.get('#FFFA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
  14. colorString.get('#FFFFFFAA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
  15. colorString.get('hsl(360, 100%, 50%)') // {model: 'hsl', value: [0, 100, 50, 1]}
  16. colorString.get('hwb(60, 3%, 60%)') // {model: 'hwb', value: [60, 3, 60, 1]}
  17. colorString.get.rgb('#FFF') // [255, 255, 255, 1]
  18. colorString.get.rgb('blue') // [0, 0, 255, 1]
  19. colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3]
  20. colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1]
  21. colorString.get.hsl('hsl(360, 100%, 50%)') // [0, 100, 50, 1]
  22. colorString.get.hsl('hsla(360, 60%, 50%, 0.4)') // [0, 60, 50, 0.4]
  23. colorString.get.hwb('hwb(60, 3%, 60%)') // [60, 3, 60, 1]
  24. colorString.get.hwb('hwb(60, 3%, 60%, 0.6)') // [60, 3, 60, 0.6]
  25. colorString.get.rgb('invalid color string') // null
  26. ```
  27. ### Generation
  28. ```js
  29. colorString.to.hex([255, 255, 255]) // "#FFFFFF"
  30. colorString.to.hex([0, 0, 255, 0.4]) // "#0000FF66"
  31. colorString.to.hex([0, 0, 255], 0.4) // "#0000FF66"
  32. colorString.to.rgb([255, 255, 255]) // "rgb(255, 255, 255)"
  33. colorString.to.rgb([0, 0, 255, 0.4]) // "rgba(0, 0, 255, 0.4)"
  34. colorString.to.rgb([0, 0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
  35. colorString.to.rgb.percent([0, 0, 255]) // "rgb(0%, 0%, 100%)"
  36. colorString.to.keyword([255, 255, 0]) // "yellow"
  37. colorString.to.hsl([360, 100, 100]) // "hsl(360, 100%, 100%)"
  38. colorString.to.hwb([50, 3, 15]) // "hwb(50, 3%, 15%)"
  39. // all functions also support swizzling
  40. colorString.to.rgb(0, [0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
  41. colorString.to.rgb([0, 0], [255], 0.4) // "rgba(0, 0, 255, 0.4)"
  42. colorString.to.rgb([0], 0, [255, 0.4]) // "rgba(0, 0, 255, 0.4)"
  43. ```