合同小程序前端代码仓库
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.

549 lines
13 KiB

1 month ago
  1. # lime-color
  2. - 颜色转换
  3. ## 安装
  4. 在插件市场导入插件即可
  5. ## 使用
  6. ```js
  7. import {tinyColor} from '@/uni_modules/lime-color'
  8. ```
  9. ## 接受的字符串输入
  10. 字符串解析非常宽容。它的目的是使输入颜色尽可能简单。所有的逗号、百分比、括号都是可选的,大多数输入允许使用 0-1、0%-100% 或 0-n(其中 n 取决于值的 100、255 或 360)。
  11. HSL 和 HSV 都需要 0%-100% 或 0-1 作为 S/L/V 属性。H(色相)可以在 0%-100% 或 0-360 之间取值。
  12. RGB 输入需要 0-255 或 0%-100%。
  13. 以下是一些字符串输入的示例:
  14. ### Hex, 8-digit (RGBA) Hex
  15. ```ts
  16. tinyColor('#000');
  17. tinyColor('000');
  18. tinyColor('#369C');
  19. tinyColor('369C');
  20. tinyColor('#f0f0f6');
  21. tinyColor('f0f0f6');
  22. tinyColor('#f0f0f688');
  23. tinyColor('f0f0f688');
  24. ```
  25. ### RGB, RGBA
  26. ```ts
  27. tinyColor('rgb (255, 0, 0)');
  28. tinyColor('rgb 255 0 0');
  29. tinyColor('rgba (255, 0, 0, .5)');
  30. tinyColor({ r: 255, g: 0, b: 0 });
  31. ```
  32. ### HSL, HSLA
  33. ```ts
  34. tinyColor('hsl(0, 100%, 50%)');
  35. tinyColor('hsla(0, 100%, 50%, .5)');
  36. tinyColor('hsl(0, 100%, 50%)');
  37. tinyColor('hsl 0 1.0 0.5');
  38. tinyColor({ h: 0, s: 1, l: 0.5 });
  39. ```
  40. ### HSV, HSVA
  41. ```ts
  42. tinyColor('hsv(0, 100%, 100%)');
  43. tinyColor('hsva(0, 100%, 100%, .5)');
  44. tinyColor('hsv (0 100% 100%)');
  45. tinyColor('hsv 0 1 1');
  46. tinyColor({ h: 0, s: 100, v: 100 });
  47. ```
  48. ### Named
  49. ```ts
  50. tinyColor('RED');
  51. tinyColor('blanchedalmond');
  52. tinyColor('darkblue');
  53. ```
  54. ### Number
  55. ```ts
  56. tinyColor(0x0);
  57. tinyColor(0xaabbcc);
  58. ```
  59. ## 属性
  60. ### originalInput
  61. 传递到构造函数中用于创建`tinyColor`实例的原始输入。
  62. ```ts
  63. const color = tinyColor('red');
  64. color.originalInput; // "red"
  65. const color2 = tinyColor({ r: 255, g: 255, b: 255 });
  66. color2.originalInput; // "{r: 255, g: 255, b: 255}"
  67. ```
  68. ### format
  69. 返回用于创建`tinyColor`实例的格式
  70. ```ts
  71. const color = tinyColor('red');
  72. color.format; // "name"
  73. const color2 = tinyColor({ r: 255, g: 255, b: 255 });
  74. color2.format; // "rgb"
  75. ```
  76. ### isValid
  77. 一个布尔值,指示颜色是否成功被解析。注意:如果颜色无效,则在与其他方法一起使用时将表现得像“黑色”。
  78. ```ts
  79. const color1 = tinyColor('red');
  80. color1.isValid; // true
  81. color1.toHexString(); // "#ff0000"
  82. const color2 = tinyColor('not a color');
  83. color2.isValid; // false
  84. color2.toString(); // "#000000"
  85. ```
  86. ## Methods 方法
  87. ### getBrightness
  88. 返回颜色的感知亮度,范围从 0-255,这是根据 [Web内容无障碍指南(第1版)](http://www.w3.org/TR/AERT#color-contrast) 定义的。
  89. ```ts
  90. const color1 = tinyColor('#fff');
  91. color1.getBrightness(); // 255
  92. const color2 = tinyColor('#000');
  93. color2.getBrightness(); // 0
  94. ```
  95. ### isLight
  96. 返回一个布尔值,指示颜色的感知亮度是否为浅色。
  97. ```ts
  98. const color1 = tinyColor('#fff');
  99. color1.isLight(); // true
  100. const color2 = tinyColor('#000');
  101. color2.isLight(); // false
  102. ```
  103. ### isDark
  104. 返回一个布尔值,指示颜色的感知亮度是否为深色。
  105. ```ts
  106. const color1 = tinyColor('#fff');
  107. color1.isDark(); // false
  108. const color2 = tinyColor('#000');
  109. color2.isDark(); // true
  110. ```
  111. ### getLuminance
  112. 返回颜色的感知亮度(luminance),范围从 0-1,这是根据 [Web内容无障碍指南(第2版)](http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef) 定义的。
  113. ```ts
  114. const color1 = tinyColor('#fff');
  115. color1.getLuminance(); // 1
  116. const color2 = tinyColor('#000');
  117. color2.getLuminance(); // 0
  118. ```
  119. ### getAlpha
  120. 返回颜色的`alpha`(透明度)值,范围从 `0-1`
  121. ```ts
  122. const color1 = tinyColor('rgba(255, 0, 0, .5)');
  123. color1.getAlpha(); // 0.5
  124. const color2 = tinyColor('rgb(255, 0, 0)');
  125. color2.getAlpha(); // 1
  126. const color3 = tinyColor('transparent');
  127. color3.getAlpha(); // 0
  128. ```
  129. ### setAlpha
  130. 在当前颜色上设置`alpha`(透明度)值。接受的范围是 `0-1` 之间。
  131. ```ts
  132. const color = tinyColor('red');
  133. color.getAlpha(); // 1
  134. color.setAlpha(0.5);
  135. color.getAlpha(); // .5
  136. color.toRgbString(); // "rgba(255, 0, 0, .5)"
  137. ```
  138. ### onBackground
  139. 计算颜色在背景上的显示效果。当颜色完全透明(即 `getAlpha() == 0`)时,结果将是背景颜色。当颜色完全不透明(即 `getAlpha() == 1`)时,结果将是颜色本身。否则,你将得到一个计算结果。
  140. ```ts
  141. const color = tinyColor('rgba(255, 0, 0, .5)');
  142. const computedColor = color.onBackground('rgb(0, 0, 255)');
  143. computedColor.toRgbString(); // "rgb(128, 0, 128)"
  144. ```
  145. ### toHsv
  146. ```ts
  147. const color = tinyColor('red');
  148. color.toHsv(); // { h: 0, s: 1, v: 1, a: 1 }
  149. ```
  150. ### toHsvString
  151. ```ts
  152. const color = tinyColor('red');
  153. color.toHsvString(); // "hsv(0, 100%, 100%)"
  154. color.setAlpha(0.5);
  155. color.toHsvString(); // "hsva(0, 100%, 100%, 0.5)"
  156. ```
  157. ### toHsl
  158. ```ts
  159. const color = tinyColor('red');
  160. color.toHsl(); // { h: 0, s: 1, l: 0.5, a: 1 }
  161. ```
  162. ### toHslString
  163. ```ts
  164. const color = tinyColor('red');
  165. color.toHslString(); // "hsl(0, 100%, 50%)"
  166. color.setAlpha(0.5);
  167. color.toHslString(); // "hsla(0, 100%, 50%, 0.5)"
  168. ```
  169. ### toNumber
  170. ```ts
  171. tinyColor('#aabbcc').toNumber() === 0xaabbcc // true
  172. tinyColor('rgb(1, 1, 1)').toNumber() === (1 << 16) + (1 << 8) + 1 // true
  173. ```
  174. ### toHex
  175. ```ts
  176. const color = tinyColor('red');
  177. color.toHex(); // "ff0000"
  178. ```
  179. ### toHexString
  180. ```ts
  181. const color = tinyColor('red');
  182. color.toHexString(); // "#ff0000"
  183. ```
  184. ### toHex8
  185. ```ts
  186. const color = tinyColor('red');
  187. color.toHex8(); // "ff0000ff"
  188. ```
  189. ### toHex8String
  190. ```ts
  191. const color = tinyColor('red');
  192. color.toHex8String(); // "#ff0000ff"
  193. ```
  194. ### toHexShortString
  195. 根据颜色的透明度(Alpha值)返回较短的十六进制值,并且值前面带有#符号
  196. ```ts
  197. const color1 = tinyColor('#ff000000');
  198. color1.toHexShortString(); // "#ff000000"
  199. color1.toHexShortString(true); // "#f000"
  200. const color2 = tinyColor('#ff0000ff');
  201. color2.toHexShortString(); // "#ff0000"
  202. color2.toHexShortString(true); // "#f00"
  203. ```
  204. ### toRgb
  205. ```ts
  206. const color = tinyColor('red');
  207. color.toRgb(); // { r: 255, g: 0, b: 0, a: 1 }
  208. ```
  209. ### toRgbString
  210. ```ts
  211. const color = tinyColor('red');
  212. color.toRgbString(); // "rgb(255, 0, 0)"
  213. color.setAlpha(0.5);
  214. color.toRgbString(); // "rgba(255, 0, 0, 0.5)"
  215. ```
  216. ### toPercentageRgb
  217. 将当前颜色转换为百分比表示的 RGB
  218. ```ts
  219. const color = tinyColor('red');
  220. color.toPercentageRgb(); // { r: "100%", g: "0%", b: "0%", a: 1 }
  221. ```
  222. ### toPercentageRgbString
  223. ```ts
  224. const color = tinyColor('red');
  225. color.toPercentageRgbString(); // "rgb(100%, 0%, 0%)"
  226. color.setAlpha(0.5);
  227. color.toPercentageRgbString(); // "rgba(100%, 0%, 0%, 0.5)"
  228. ```
  229. ### toName
  230. ```ts
  231. const color = tinyColor('red');
  232. color.toName(); // "red"
  233. ```
  234. ### toString
  235. 根据输入格式打印成字符串。你也可以通过向函数中传入以下之一来覆盖这个行为:`"rgb", "prgb", "hex6", "hex3", "hex8", "name", "hsl", "hsv"`
  236. ```ts
  237. const color1 = tinyColor('red');
  238. color1.toString(); // "red"
  239. color1.toString('hsv'); // "hsv(0, 100%, 100%)"
  240. const color2 = tinyColor('rgb(255, 0, 0)');
  241. color2.toString(); // "rgb(255, 0, 0)"
  242. color2.setAlpha(0.5);
  243. color2.toString(); // "rgba(255, 0, 0, 0.5)"
  244. ```
  245. ### 颜色修改
  246. 这些方法操纵当前颜色,并返回它以进行链式调用。例如:
  247. ```ts
  248. tinyColor('red')
  249. .lighten()
  250. .desaturate()
  251. .toHexString(); // '#f53d3d'
  252. ```
  253. ### lighten
  254. `lighten: function(amount = 10) -> TinyColor`.根据给定的量(从0到100)调亮颜色。提供100将始终返回白色.
  255. ```ts
  256. tinyColor('#f00').lighten().toString(); // '#ff3333'
  257. tinyColor('#f00').lighten(100).toString(); // '#ffffff'
  258. ```
  259. ### brighten
  260. `brighten: function(amount = 10) -> TinyColor`. 根据给定的量(从0到100)提高颜色的亮度。
  261. ```ts
  262. tinyColor('#f00').brighten().toString(); // '#ff1919'
  263. ```
  264. ### darken
  265. `darken: function(amount = 10) -> TinyColor`. 根据给定的量(从0到100)调暗颜色。提供100将始终返回黑色.
  266. ```ts
  267. tinyColor('#f00').darken().toString(); // '#cc0000'
  268. tinyColor('#f00').darken(100).toString(); // '#000000'
  269. ```
  270. ### tint
  271. 将颜色与纯白色混合,范围从0到100。提供0将不进行任何操作,提供100将始终返回白色.
  272. ```ts
  273. tinyColor('#f00').tint().toString(); // "#ff1a1a"
  274. tinyColor('#f00').tint(100).toString(); // "#ffffff"
  275. ```
  276. ### shade
  277. 将颜色与纯黑色混合,范围从0到100。提供0将不进行任何操作,提供100将始终返回黑色。
  278. ```ts
  279. tinyColor('#f00').shade().toString(); // "#e60000"
  280. tinyColor('#f00').shade(100).toString(); // "#000000"
  281. ```
  282. ### desaturate
  283. `desaturate: function(amount = 10) -> TinyColor`. 根据给定的量(从0到100)降低颜色的饱和度。提供100将与调用`greyscale`相同。
  284. ```ts
  285. tinyColor('#f00').desaturate().toString(); // "#f20d0d"
  286. tinyColor('#f00').desaturate(100).toString(); // "#808080"
  287. ```
  288. ### saturate
  289. `saturate: function(amount = 10) -> TinyColor`. 根据给定的量(从0到100)增加颜色的饱和度。
  290. ```ts
  291. tinyColor('hsl(0, 10%, 50%)').saturate().toString(); // "hsl(0, 20%, 50%)"
  292. ```
  293. ### greyscale
  294. `greyscale: function() -> TinyColor`. 完全降低颜色的饱和度,使其变为灰度。与调用`desaturate(100)`相同。
  295. ```ts
  296. tinyColor('#f00').greyscale().toString(); // "#808080"
  297. ```
  298. ### spin
  299. `spin: function(amount = 0) -> TinyColor`. 根据给定的量(从-360到360)旋转色相。调用时使用0、360或-360将不进行任何操作(因为它将色相设置回原来的值)。
  300. ```ts
  301. tinyColor('#f00').spin(180).toString(); // "#00ffff"
  302. tinyColor('#f00').spin(-90).toString(); // "#7f00ff"
  303. tinyColor('#f00').spin(90).toString(); // "#80ff00"
  304. // spin(0) and spin(360) do nothing
  305. tinyColor('#f00').spin(0).toString(); // "#ff0000"
  306. tinyColor('#f00').spin(360).toString(); // "#ff0000"
  307. ```
  308. ### mix
  309. `mix: function(amount = 50) => TinyColor`. 将当前颜色与另一种颜色按给定量(从0到100)混合。0表示不混合(返回当前颜色)。
  310. ```ts
  311. let color1 = tinyColor('#f0f');
  312. let color2 = tinyColor('#0f0');
  313. color1.mix(color2).toHexString(); // #808080
  314. ```
  315. ### 颜色组合
  316. 组合函数除非特别说明,否则返回一个`TinyColor`对象的数组。
  317. ### analogous
  318. 生成一组与当前颜色相似的颜色。
  319. `analogous: function(results = 6, slices = 30) -> array<TinyColor>`.
  320. ```ts
  321. const colors = tinyColor('#f00').analogous();
  322. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#ff0066", "#ff0033", "#ff0000", "#ff3300", "#ff6600" ]
  323. ```
  324. ### monochromatic
  325. 生成一组与当前颜色具有相同色相和饱和度的颜色。
  326. `monochromatic: function(, results = 6) -> array<TinyColor>`.
  327. ```ts
  328. const colors = tinyColor('#f00').monochromatic();
  329. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#2a0000", "#550000", "#800000", "#aa0000", "#d40000" ]
  330. ```
  331. ### splitcomplement
  332. 生成当前颜色的分裂补色。
  333. `splitcomplement: function() -> array<TinyColor>`.
  334. ```ts
  335. const colors = tinyColor('#f00').splitcomplement();
  336. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#ccff00", "#0066ff" ]
  337. ```
  338. ### triad
  339. 生成当前颜色的三色调。
  340. `triad: function() -> array<TinyColor>`. Alias for `polyad(3)`.
  341. ```ts
  342. const colors = tinyColor('#f00').triad();
  343. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#00ff00", "#0000ff" ]
  344. ```
  345. ### tetrad
  346. 生成当前颜色的四色调。
  347. `tetrad: function() -> array<TinyColor>`. Alias for `polyad(4)`.
  348. ```ts
  349. const colors = tinyColor('#f00').tetrad();
  350. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]
  351. ```
  352. ### polyad
  353. 生成当前颜色的 n 色调。
  354. `polyad: function(number) -> array<TinyColor>`.
  355. ```ts
  356. const colors = tinyColor('#f00').polyad(4);
  357. colors.map((t):string => t.toHexString()); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]
  358. ```
  359. ### complement
  360. 计算当前颜色的补色。
  361. `complement: function() -> TinyColor`.
  362. ```ts
  363. tinyColor('#f00').complement().toHexString(); // "#00ffff"
  364. ```
  365. ## 颜色工具
  366. ### equals
  367. 判断两色是否相同
  368. ```ts
  369. let color1 = tinyColor('red');
  370. let color2 = tinyColor('#f00');
  371. color1.equals(color2); // true
  372. ```
  373. ## 常见操作
  374. ### clone
  375. `clone: function() -> TinyColor`.
  376. 使用相同的颜色实例化一个新的`TinyColor`对象。对新的对象的任何更改都不会影响旧的对象。
  377. ```ts
  378. const color1 = tinyColor('#F00');
  379. const color2 = color1.clone();
  380. color2.setAlpha(0.5);
  381. color1.toString(); // "#ff0000"
  382. color2.toString(); // "rgba(255, 0, 0, 0.5)"
  383. ```
  384. ### generate
  385. 通过一个主色生成10个等级的颜色数组,使用 Ant Design 的颜色生成算法。
  386. ```ts
  387. import {generate, LGenerateOptions} from '@/uni_modules/lime-color'
  388. // 第二个参数为选填,如果不填则默认为 'default'
  389. generate('red',{ theme: 'default'|'dark'} as LGenerateOptions)
  390. // ['#2c1113', '#450f11', '#5b0e0e', '#7e0b0b', '#ad0707', '#dc0303', '#e82d27', '#f3594f', '#f88577', '#faaca0']
  391. ```
  392. ## 打赏
  393. 如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。
  394. ![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/alipay.png)
  395. ![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/wpay.png)