043d9af624d82203f2df5cfe0f815d8a3c22390f3ab9b1302ca7757b6fabe3fb69feb396c9422eec2914b8a00ccd8a13e403987a10cbb95eb767752979c35e 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. [ENGLISH](./README_EN.md)
  2. <h1 align="center">Color Extension</h1>
  3. <p align="center">
  4. <a href="https://travis-ci.com/DataV-Team/color">
  5. <img src="https://img.shields.io/travis/com/DataV-Team/color.svg" alt="Travis CI" />
  6. </a>
  7. <a href="https://github.com/DataV-Team/color/blob/master/LICENSE">
  8. <img src="https://img.shields.io/github/license/DataV-Team/color.svg" alt="LICENSE" />
  9. </a>
  10. <a href="https://www.npmjs.com/package/@jiaminghi/color">
  11. <img src="https://img.shields.io/npm/v/@jiaminghi/color.svg" alt="NPM" />
  12. </a>
  13. </p>
  14. ### 这是一个提供如下几个方法的颜色插件。
  15. - **[isHex](#isHex)**
  16. 判断一个颜色是否为 Hex 类型
  17. - **[isRgb](#isRgb)**
  18. 判断一个颜色是否为 Rgb 类型
  19. - **[isRgba](#isRgba)**
  20. 判断一个颜色是否为 Rgba 类型
  21. - **[isRgbOrRgba](#isRgbOrRgba)**
  22. 判断一个颜色是否为 Rgb 或 Rgba 类型
  23. - **[darken](#darken)**
  24. 加深颜色
  25. - **[lighten](#lighten)**
  26. 提亮颜色
  27. - **[fade](#fade)**
  28. 调节颜色透明度
  29. - **[toHex](#toHex)**
  30. 转换成 Hex 颜色
  31. - **[toRgb](#toRgb)**
  32. 将颜色转为 Rgb 或 Rgba 颜色
  33. - **[getOpacity](#getOpacity)**
  34. 获取颜色透明度
  35. - **[getRgbValue](#getRgbValue)**
  36. 获取颜色的 Rgb 值
  37. - **[getRgbaValue](#getRgbaValue)**
  38. 获取颜色的 Rgba 值
  39. - **[getColorFromRgbValue](#getColorFromRgbValue)**
  40. 从 Rgb 或 Rgba 值获取颜色
  41. ### 附录
  42. - **[颜色关键字](#颜色关键字)**
  43. 内置颜色关键字
  44. ### npm 安装
  45. ```shell
  46. $ npm install @jiaminghi/color
  47. ```
  48. ### 使用
  49. ```javascript
  50. import { toHex } from '@jiaminghi/color'
  51. // do something
  52. ```
  53. ### 快速体验
  54. ```html
  55. <!--调试版-->
  56. <script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
  57. <!--压缩版-->
  58. <script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
  59. <script>
  60. const { darken, lighten } = window.Color
  61. // do something
  62. </script>
  63. ```
  64. ---
  65. <h3 align="center">示例</h3>
  66. #### isHex
  67. ```typescript
  68. /**
  69. * @description 判断一个颜色是否为hex类型
  70. * @param {string} color 可能是hex颜色的字符串
  71. * @return {boolean} 判断结果
  72. */
  73. type isHex = (color: string) => boolean
  74. isHex('#000') // true
  75. isHex('#333333') // true
  76. isHex('Not A Color') // false
  77. isHex('rgb(0,0,0)') // false
  78. isHex('rgba(0,0,0,1)') // false
  79. ```
  80. #### isRgb
  81. ```typescript
  82. /**
  83. * @description 判断一个颜色是否为rgb类型
  84. * @param {string} color 可能是rgb颜色的字符串
  85. * @return {boolean} 判断结果
  86. */
  87. type isRgb = (color: string) => boolean
  88. isRgb('rgb(0,0,0)') // true
  89. isRgb('RGB(0,0,0)') // true
  90. isRgb('Not A Color') // false
  91. isRgb('#000') // false
  92. isRgb('#000000') // false
  93. ```
  94. #### isRgba
  95. ```typescript
  96. /**
  97. * @description 判断一个颜色是否为rgba类型
  98. * @param {string} color 可能是rgba颜色的字符串
  99. * @return {boolean} 判断结果
  100. */
  101. type isRgba = (color: string) => boolean
  102. isRgba('rgba(0,0,0,1)') // true
  103. isRgba('rgb(0,0,0)') // false
  104. isRgba('Not A Color') // false
  105. isRgba('#000') // false
  106. isRgba('#000000') // false
  107. ```
  108. #### isRgbOrRgba
  109. ```typescript
  110. /**
  111. * @description 判断一个颜色是否为rgb或rgba类型
  112. * @param {string} color 可能是rgb或rgba颜色的字符串
  113. * @return {boolean} 判断结果
  114. */
  115. type isRgbOrRgba = (color: string) => boolean
  116. isRgbOrRgba('rgb(0,0,0)') // true
  117. isRgbOrRgba('RGB(0,0,0)') // true
  118. isRgbOrRgba('rgba(0,0,0,1)') // true
  119. isRgbOrRgba('#000') // false
  120. isRgbOrRgba('Not A Color') // false
  121. ```
  122. #### darken
  123. ```typescript
  124. /**
  125. * @description 加深颜色
  126. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  127. * @param {number} percent 加深的百分比 (1-100)
  128. * @return {string} Rgba颜色 (无效输入将抛出异常)
  129. */
  130. type darken = (color: string, percent: number) => string
  131. const before = '#3080E8'
  132. const after = darken(color, 20)
  133. // after = 'rgba(0,77,181,1)'
  134. ```
  135. <p align="center">
  136. <img width="180px" src="./exampleImgs/1.jpg" />
  137. </p>
  138. #### lighten
  139. ```typescript
  140. /**
  141. * @description 提亮颜色
  142. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  143. * @param {number} percent 提亮的百分比 (1-100)
  144. * @return {string} Rgba颜色 (无效输入将抛出异常)
  145. */
  146. type lighten = (color: string, percent: number) => string
  147. const before = '#3080E8'
  148. const after = lighten(color, 20)
  149. // after = 'rgba(99,179,255,1)'
  150. ```
  151. <p align="center">
  152. <img width="180px" src="./exampleImgs/2.jpg" />
  153. </p>
  154. #### fade
  155. ```typescript
  156. /**
  157. * @description 调节颜色透明度
  158. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  159. * @param {number} Percent 透明度百分比
  160. * @return {string} Rgba颜色 (无效输入将抛出异常)
  161. */
  162. type fade = (color: string, percent: number) => string
  163. const before = '#3080E8'
  164. const after = lighten(color, 20)
  165. // after = 'rgba(48,128,232,0.2)'
  166. ```
  167. <p align="center">
  168. <img width="180px" src="./exampleImgs/3.jpg" />
  169. </p>
  170. #### toHex
  171. ```typescript
  172. /**
  173. * @description 转换成Hex颜色
  174. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  175. * @return {string} Hex颜色 (无效输入将抛出异常)
  176. */
  177. type toHex = (color: string) => string
  178. const before = 'rgb(48,128,232)'
  179. const after = toHex(before)
  180. // after = '#3080e8'
  181. ```
  182. #### toRgb
  183. ```typescript
  184. /**
  185. * @description 将颜色转为Rgb或Rgba颜色
  186. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  187. * @param {number} opacity 颜色的透明度 (输入该参数将生成Rgba颜色)
  188. * @return {string} Rgb或Rgba颜色 (无效输入将抛出异常)
  189. */
  190. type toRgb = (color: string, opacity: number) => string
  191. const before = '#3080E8'
  192. const after1 = toRgb(before)
  193. // after1 = 'rgb(48,128,232)'
  194. const after2 = toRgb(before, 0.2)
  195. // after2 = 'rgba(48,128,232,0.2)'
  196. ```
  197. #### getOpacity
  198. ```typescript
  199. /**
  200. * @description 获取颜色透明度
  201. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  202. * @return {number} 颜色透明度 (无效输入将抛出异常)
  203. */
  204. type getOpacity = (color: string) => number
  205. const color1 = '#3080E8'
  206. const color2 = 'rgba(48,128,232,0.2)'
  207. const opacity1 = getOpacity(color1)
  208. // opacity1 = 1
  209. const opacity2 = getOpacity(color2)
  210. // opacity2 = 0.2
  211. ```
  212. #### getRgbValue
  213. ```typescript
  214. /**
  215. * @description 获取颜色的Rgb值
  216. * @param {string} color Hex|Rgb|Rgba颜色或颜色关键字
  217. * @return {RgbValue} Rgb值 (无效输入将抛出异常)
  218. */
  219. type RgbValue = [number, number, number]
  220. type getRgbValue = (color: string) => RgbValue
  221. const color = '#3080E8'
  222. const rgbValue = getRgbValue(color)
  223. // rgbValue = [48, 128, 232]
  224. ```
  225. #### getRgbaValue
  226. ```typescript
  227. /**
  228. * @description 获取颜色的Rgba值
  229. * @param {String} color Hex|Rgb|Rgba颜色或颜色关键字
  230. * @return {RgbaValue|null} Rgba值 (无效输入将抛出异常)
  231. */
  232. type RgbaValue = [number, number, number, number]
  233. type getRgbaValue = (color: string) => RgbaValue
  234. const color1 = '#3080E8'
  235. const color2 = 'rgba(48,128,232,0.2)'
  236. const rgbaValue1 = getRgbaValue(color1)
  237. // rgbaValue1 = [48, 128, 232, 1]
  238. const rgbaValue2 = getRgbaValue(color2)
  239. // rgbaValue2 = [48, 128, 232, 0.2]
  240. ```
  241. #### getColorFromRgbValue
  242. ```typescript
  243. /**
  244. * @description 从Rgb或Rgba值获取颜色
  245. * @param {Value} value Rgb或Rgba颜色的值
  246. * @return {string} Rgb颜色或Rgba颜色 (无效输入将抛出异常)
  247. */
  248. type RgbValue = [number, number, number]
  249. type RgbaValue = [number, number, number, number]
  250. type Value = RgbValue | RgbaValue
  251. type getColorFromRgbValue = (value: Value) => string
  252. const value1 = [48, 128, 232]
  253. const value2 = [48, 128, 232, 0.2]
  254. const color1 = getColorFromRgbValue(value1)
  255. // color1 = 'rgb(48,128,232)'
  256. const color2 = getColorFromRgbValue(value2)
  257. // color2 = 'rgba(48,128,232,0.2)'
  258. ```
  259. #### 颜色关键字
  260. <p align="center">
  261. <img width="750px" src="./exampleImgs/keywords.png" />
  262. </p>