t-color-picker.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <template>
  2. <view v-show="show" class="t-wrapper" @touchmove.stop.prevent="moveHandle">
  3. <view class="t-mask" :class="{active:active}" @click.stop="close"></view>
  4. <view class="t-box" :class="{active:active}">
  5. <view class="t-header">
  6. <view class="t-header-button" @click="close">取消</view>
  7. <view class="t-header-button" @click="confirm">确认</view>
  8. </view>
  9. <view class="t-color__box" :style="{ background: 'rgb(' + bgcolor.r + ',' + bgcolor.g + ',' + bgcolor.b + ')'}">
  10. <view class="t-background boxs" @touchstart="touchstart($event, 0)" @touchmove="touchmove($event, 0)" @touchend="touchend($event, 0)">
  11. <view class="t-color-mask"></view>
  12. <view class="t-pointer" :style="{ top: site[0].top - 8 + 'px', left: site[0].left - 8 + 'px' }"></view>
  13. </view>
  14. </view>
  15. <view class="t-control__box">
  16. <view class="t-control__color">
  17. <view class="t-control__color-content" :style="{ background: 'rgba(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ',' + rgba.a + ')' }"></view>
  18. </view>
  19. <view class="t-control-box__item">
  20. <view class="t-controller boxs" @touchstart="touchstart($event, 1)" @touchmove="touchmove($event, 1)" @touchend="touchend($event, 1)">
  21. <view class="t-hue">
  22. <view class="t-circle" :style="{ left: site[1].left - 12 + 'px' }"></view>
  23. </view>
  24. </view>
  25. <view class="t-controller boxs" @touchstart="touchstart($event, 2)" @touchmove="touchmove($event, 2)" @touchend="touchend($event, 2)">
  26. <view class="t-transparency">
  27. <view class="t-circle" :style="{ left: site[2].left - 12 + 'px' }"></view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="t-result__box">
  33. <view v-if="mode" class="t-result__item">
  34. <view class="t-result__box-input">{{hex}}</view>
  35. <view class="t-result__box-text">HEX</view>
  36. </view>
  37. <template v-else>
  38. <view class="t-result__item">
  39. <view class="t-result__box-input">{{rgba.r}}</view>
  40. <view class="t-result__box-text">R</view>
  41. </view>
  42. <view class="t-result__item">
  43. <view class="t-result__box-input">{{rgba.g}}</view>
  44. <view class="t-result__box-text">G</view>
  45. </view>
  46. <view class="t-result__item">
  47. <view class="t-result__box-input">{{rgba.b}}</view>
  48. <view class="t-result__box-text">B</view>
  49. </view>
  50. <view class="t-result__item">
  51. <view class="t-result__box-input">{{rgba.a}}</view>
  52. <view class="t-result__box-text">A</view>
  53. </view>
  54. </template>
  55. <view class="t-result__item t-select" @click="select">
  56. <view class="t-result__box-input">
  57. <view>切换</view>
  58. <view>模式</view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="t-alternative">
  63. <view class="t-alternative__item" v-for="(item,index) in colorList" :key="index">
  64. <view class="t-alternative__item-content" :style="{ background: 'rgba(' + item.r + ',' + item.g + ',' + item.b + ',' + item.a + ')' }"
  65. @click="selectColor(item)">
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. export default {
  74. props: {
  75. color: {
  76. type: Object,
  77. default () {
  78. return {
  79. r: 0,
  80. g: 0,
  81. b: 0,
  82. a: 0
  83. }
  84. }
  85. },
  86. spareColor: {
  87. type: Array,
  88. default () {
  89. return []
  90. }
  91. }
  92. },
  93. data() {
  94. return {
  95. show: false,
  96. active: false,
  97. // rgba 颜色
  98. rgba: {
  99. r: 0,
  100. g: 0,
  101. b: 0,
  102. a: 1
  103. },
  104. // hsb 颜色
  105. hsb: {
  106. h: 0,
  107. s: 0,
  108. b: 0
  109. },
  110. site: [{
  111. top: 0,
  112. left: 0
  113. }, {
  114. left: 0
  115. }, {
  116. left: 0
  117. }],
  118. index: 0,
  119. bgcolor: {
  120. r: 255,
  121. g: 0,
  122. b: 0,
  123. a: 1
  124. },
  125. hex: '#000000',
  126. mode: true,
  127. colorList: [{
  128. r: 244,
  129. g: 67,
  130. b: 54,
  131. a: 1
  132. }, {
  133. r: 233,
  134. g: 30,
  135. b: 99,
  136. a: 1
  137. }, {
  138. r: 156,
  139. g: 39,
  140. b: 176,
  141. a: 1
  142. }, {
  143. r: 103,
  144. g: 58,
  145. b: 183,
  146. a: 1
  147. }, {
  148. r: 63,
  149. g: 81,
  150. b: 181,
  151. a: 1
  152. }, {
  153. r: 33,
  154. g: 150,
  155. b: 243,
  156. a: 1
  157. }, {
  158. r: 3,
  159. g: 169,
  160. b: 244,
  161. a: 1
  162. }, {
  163. r: 0,
  164. g: 188,
  165. b: 212,
  166. a: 1
  167. }, {
  168. r: 0,
  169. g: 150,
  170. b: 136,
  171. a: 1
  172. }, {
  173. r: 76,
  174. g: 175,
  175. b: 80,
  176. a: 1
  177. }, {
  178. r: 139,
  179. g: 195,
  180. b: 74,
  181. a: 1
  182. }, {
  183. r: 205,
  184. g: 220,
  185. b: 57,
  186. a: 1
  187. }, {
  188. r: 255,
  189. g: 235,
  190. b: 59,
  191. a: 1
  192. }, {
  193. r: 255,
  194. g: 193,
  195. b: 7,
  196. a: 1
  197. }, {
  198. r: 255,
  199. g: 152,
  200. b: 0,
  201. a: 1
  202. }, {
  203. r: 255,
  204. g: 87,
  205. b: 34,
  206. a: 1
  207. }, {
  208. r: 121,
  209. g: 85,
  210. b: 72,
  211. a: 1
  212. }, {
  213. r: 158,
  214. g: 158,
  215. b: 158,
  216. a: 1
  217. }, {
  218. r: 0,
  219. g: 0,
  220. b: 0,
  221. a: 0.5
  222. }, {
  223. r: 0,
  224. g: 0,
  225. b: 0,
  226. a: 0
  227. }, ]
  228. };
  229. },
  230. created() {
  231. this.rgba = this.color;
  232. if (this.spareColor.length !== 0) {
  233. this.colorList = this.spareColor;
  234. }
  235. },
  236. methods: {
  237. /**
  238. * 初始化
  239. */
  240. init() {
  241. // hsb 颜色
  242. this.hsb = this.rgbToHex(this.rgba);
  243. // this.setColor();
  244. this.setValue(this.rgba);
  245. },
  246. moveHandle() {},
  247. open() {
  248. this.show = true;
  249. this.$nextTick(() => {
  250. this.init();
  251. setTimeout(() => {
  252. this.active = true;
  253. setTimeout(() => {
  254. this.getSelectorQuery();
  255. }, 350)
  256. }, 50)
  257. })
  258. },
  259. close() {
  260. this.active = false;
  261. this.$nextTick(() => {
  262. setTimeout(() => {
  263. this.show = false;
  264. }, 500)
  265. })
  266. },
  267. confirm() {
  268. this.close();
  269. this.$emit('confirm', {
  270. rgba: this.rgba,
  271. hex: this.hex
  272. })
  273. },
  274. // 选择模式
  275. select() {
  276. this.mode = !this.mode
  277. },
  278. // 常用颜色选择
  279. selectColor(item) {
  280. this.setColorBySelect(item)
  281. },
  282. touchstart(e, index) {
  283. const {
  284. pageX,
  285. pageY
  286. } = e.touches[0];
  287. this.pageX = pageX;
  288. this.pageY = pageY;
  289. this.setPosition(pageX, pageY, index);
  290. },
  291. touchmove(e, index) {
  292. const {
  293. pageX,
  294. pageY
  295. } = e.touches[0];
  296. this.moveX = pageX;
  297. this.moveY = pageY;
  298. this.setPosition(pageX, pageY, index);
  299. },
  300. touchend(e, index) {},
  301. /**
  302. * 设置位置
  303. */
  304. setPosition(x, y, index) {
  305. this.index = index;
  306. const {
  307. top,
  308. left,
  309. width,
  310. height
  311. } = this.position[index];
  312. // 设置最大最小值
  313. this.site[index].left = Math.max(0, Math.min(parseInt(x - left), width));
  314. if (index === 0) {
  315. this.site[index].top = Math.max(0, Math.min(parseInt(y - top), height));
  316. // 设置颜色
  317. this.hsb.s = parseInt((100 * this.site[index].left) / width);
  318. this.hsb.b = parseInt(100 - (100 * this.site[index].top) / height);
  319. this.setColor();
  320. this.setValue(this.rgba);
  321. } else {
  322. this.setControl(index, this.site[index].left);
  323. }
  324. },
  325. /**
  326. * 设置 rgb 颜色
  327. */
  328. setColor() {
  329. const rgb = this.HSBToRGB(this.hsb);
  330. this.rgba.r = rgb.r;
  331. this.rgba.g = rgb.g;
  332. this.rgba.b = rgb.b;
  333. },
  334. /**
  335. * 设置二进制颜色
  336. * @param {Object} rgb
  337. */
  338. setValue(rgb) {
  339. this.hex = '#' + this.rgbToHex(rgb);
  340. },
  341. setControl(index, x) {
  342. const {
  343. top,
  344. left,
  345. width,
  346. height
  347. } = this.position[index];
  348. if (index === 1) {
  349. this.hsb.h = parseInt((360 * x) / width);
  350. this.bgcolor = this.HSBToRGB({
  351. h: this.hsb.h,
  352. s: 100,
  353. b: 100
  354. });
  355. this.setColor()
  356. } else {
  357. this.rgba.a = (x / width).toFixed(1);
  358. }
  359. this.setValue(this.rgba);
  360. },
  361. /**
  362. * rgb 转 二进制 hex
  363. * @param {Object} rgb
  364. */
  365. rgbToHex(rgb) {
  366. let hex = [rgb.r.toString(16), rgb.g.toString(16), rgb.b.toString(16)];
  367. hex.map(function(str, i) {
  368. if (str.length == 1) {
  369. hex[i] = '0' + str;
  370. }
  371. });
  372. return hex.join('');
  373. },
  374. setColorBySelect(getrgb) {
  375. const {
  376. r,
  377. g,
  378. b,
  379. a
  380. } = getrgb;
  381. let rgb = {}
  382. rgb = {
  383. r: r ? parseInt(r) : 0,
  384. g: g ? parseInt(g) : 0,
  385. b: b ? parseInt(b) : 0,
  386. a: a ? a : 0,
  387. };
  388. this.rgba = rgb;
  389. this.hsb = this.rgbToHsb(rgb);
  390. this.changeViewByHsb();
  391. },
  392. changeViewByHsb() {
  393. const [a, b, c] = this.position;
  394. this.site[0].left = parseInt(this.hsb.s * a.width / 100);
  395. this.site[0].top = parseInt((100 - this.hsb.b) * a.height / 100);
  396. this.setColor(this.hsb.h);
  397. this.setValue(this.rgba);
  398. this.bgcolor = this.HSBToRGB({
  399. h: this.hsb.h,
  400. s: 100,
  401. b: 100
  402. });
  403. this.site[1].left = this.hsb.h / 360 * b.width;
  404. this.site[2].left = this.rgba.a * c.width;
  405. },
  406. /**
  407. * hsb 转 rgb
  408. * @param {Object} 颜色模式 H(hues)表示色相,S(saturation)表示饱和度,B(brightness)表示亮度
  409. */
  410. HSBToRGB(hsb) {
  411. let rgb = {};
  412. let h = Math.round(hsb.h);
  413. let s = Math.round((hsb.s * 255) / 100);
  414. let v = Math.round((hsb.b * 255) / 100);
  415. if (s == 0) {
  416. rgb.r = rgb.g = rgb.b = v;
  417. } else {
  418. let t1 = v;
  419. let t2 = ((255 - s) * v) / 255;
  420. let t3 = ((t1 - t2) * (h % 60)) / 60;
  421. if (h == 360) h = 0;
  422. if (h < 60) {
  423. rgb.r = t1;
  424. rgb.b = t2;
  425. rgb.g = t2 + t3;
  426. } else if (h < 120) {
  427. rgb.g = t1;
  428. rgb.b = t2;
  429. rgb.r = t1 - t3;
  430. } else if (h < 180) {
  431. rgb.g = t1;
  432. rgb.r = t2;
  433. rgb.b = t2 + t3;
  434. } else if (h < 240) {
  435. rgb.b = t1;
  436. rgb.r = t2;
  437. rgb.g = t1 - t3;
  438. } else if (h < 300) {
  439. rgb.b = t1;
  440. rgb.g = t2;
  441. rgb.r = t2 + t3;
  442. } else if (h < 360) {
  443. rgb.r = t1;
  444. rgb.g = t2;
  445. rgb.b = t1 - t3;
  446. } else {
  447. rgb.r = 0;
  448. rgb.g = 0;
  449. rgb.b = 0;
  450. }
  451. }
  452. return {
  453. r: Math.round(rgb.r),
  454. g: Math.round(rgb.g),
  455. b: Math.round(rgb.b)
  456. };
  457. },
  458. rgbToHsb(rgb) {
  459. let hsb = {
  460. h: 0,
  461. s: 0,
  462. b: 0
  463. };
  464. let min = Math.min(rgb.r, rgb.g, rgb.b);
  465. let max = Math.max(rgb.r, rgb.g, rgb.b);
  466. let delta = max - min;
  467. hsb.b = max;
  468. hsb.s = max != 0 ? 255 * delta / max : 0;
  469. if (hsb.s != 0) {
  470. if (rgb.r == max) hsb.h = (rgb.g - rgb.b) / delta;
  471. else if (rgb.g == max) hsb.h = 2 + (rgb.b - rgb.r) / delta;
  472. else hsb.h = 4 + (rgb.r - rgb.g) / delta;
  473. } else hsb.h = -1;
  474. hsb.h *= 60;
  475. if (hsb.h < 0) hsb.h = 0;
  476. hsb.s *= 100 / 255;
  477. hsb.b *= 100 / 255;
  478. return hsb;
  479. },
  480. getSelectorQuery() {
  481. const views = uni.createSelectorQuery().in(this);
  482. views
  483. .selectAll('.boxs')
  484. .boundingClientRect(data => {
  485. if (!data || data.length === 0) {
  486. setTimeout(() => this.getSelectorQuery(), 20)
  487. return
  488. }
  489. this.position = data;
  490. // this.site[0].top = data[0].height;
  491. // this.site[0].left = 0;
  492. // this.site[1].left = data[1].width;
  493. // this.site[2].left = data[2].width;
  494. this.setColorBySelect(this.rgba);
  495. })
  496. .exec();
  497. }
  498. },
  499. watch: {
  500. spareColor(newVal) {
  501. this.colorList = newVal;
  502. }
  503. }
  504. };
  505. </script>
  506. <style>
  507. .t-wrapper {
  508. position: fixed;
  509. top: 0;
  510. bottom: 0;
  511. left: 0;
  512. width: 100%;
  513. box-sizing: border-box;
  514. z-index: 9999;
  515. }
  516. .t-box {
  517. width: 100%;
  518. position: absolute;
  519. bottom: 0;
  520. padding: 30upx 0;
  521. padding-top: 0;
  522. background: #fff;
  523. transition: all 0.3s;
  524. transform: translateY(100%);
  525. }
  526. .t-box.active {
  527. transform: translateY(0%);
  528. }
  529. .t-header {
  530. display: flex;
  531. justify-content: space-between;
  532. width: 100%;
  533. height: 100upx;
  534. border-bottom: 1px #eee solid;
  535. box-shadow: 1px 0 2px rgba(0, 0, 0, 0.1);
  536. background: #fff;
  537. }
  538. .t-header-button {
  539. display: flex;
  540. align-items: center;
  541. width: 150upx;
  542. height: 100upx;
  543. font-size: 30upx;
  544. color: #666;
  545. padding-left: 20upx;
  546. }
  547. .t-header-button:last-child {
  548. justify-content: flex-end;
  549. padding-right: 20upx;
  550. }
  551. .t-mask {
  552. position: absolute;
  553. top: 0;
  554. left: 0;
  555. right: 0;
  556. bottom: 0;
  557. background: rgba(0, 0, 0, 0.6);
  558. z-index: -1;
  559. transition: all 0.3s;
  560. opacity: 0;
  561. }
  562. .t-mask.active {
  563. opacity: 1;
  564. }
  565. .t-color__box {
  566. position: relative;
  567. height: 400upx;
  568. background: rgb(255, 0, 0);
  569. overflow: hidden;
  570. box-sizing: border-box;
  571. margin: 0 20upx;
  572. margin-top: 20upx;
  573. box-sizing: border-box;
  574. }
  575. .t-background {
  576. position: absolute;
  577. top: 0;
  578. left: 0;
  579. right: 0;
  580. bottom: 0;
  581. background: linear-gradient(to right, #fff, rgba(255, 255, 255, 0));
  582. }
  583. .t-color-mask {
  584. position: absolute;
  585. top: 0;
  586. left: 0;
  587. right: 0;
  588. bottom: 0;
  589. width: 100%;
  590. height: 400upx;
  591. background: linear-gradient(to top, #000, rgba(0, 0, 0, 0));
  592. }
  593. .t-pointer {
  594. position: absolute;
  595. bottom: -8px;
  596. left: -8px;
  597. z-index: 2;
  598. width: 15px;
  599. height: 15px;
  600. border: 1px #fff solid;
  601. border-radius: 50%;
  602. }
  603. .t-show-color {
  604. width: 100upx;
  605. height: 50upx;
  606. }
  607. .t-control__box {
  608. margin-top: 50upx;
  609. width: 100%;
  610. display: flex;
  611. padding-left: 20upx;
  612. box-sizing: border-box;
  613. }
  614. .t-control__color {
  615. flex-shrink: 0;
  616. width: 100upx;
  617. height: 100upx;
  618. border-radius: 50%;
  619. background-color: #fff;
  620. background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee),
  621. linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee);
  622. background-size: 36upx 36upx;
  623. background-position: 0 0, 18upx 18upx;
  624. border: 1px #eee solid;
  625. overflow: hidden;
  626. }
  627. .t-control__color-content {
  628. width: 100%;
  629. height: 100%;
  630. }
  631. .t-control-box__item {
  632. display: flex;
  633. flex-direction: column;
  634. justify-content: space-between;
  635. width: 100%;
  636. padding: 0 30upx;
  637. }
  638. .t-controller {
  639. position: relative;
  640. width: 100%;
  641. height: 16px;
  642. background-color: #fff;
  643. background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee),
  644. linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee);
  645. background-size: 32upx 32upx;
  646. background-position: 0 0, 16upx 16upx;
  647. }
  648. .t-hue {
  649. width: 100%;
  650. height: 100%;
  651. background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
  652. }
  653. .t-transparency {
  654. width: 100%;
  655. height: 100%;
  656. background: linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(0, 0, 0));
  657. }
  658. .t-circle {
  659. position: absolute;
  660. /* right: -10px; */
  661. top: -2px;
  662. width: 20px;
  663. height: 20px;
  664. box-sizing: border-box;
  665. border-radius: 50%;
  666. background: #fff;
  667. box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.1);
  668. }
  669. .t-result__box {
  670. margin-top: 20upx;
  671. padding: 10upx;
  672. width: 100%;
  673. display: flex;
  674. box-sizing: border-box;
  675. }
  676. .t-result__item {
  677. display: flex;
  678. flex-direction: column;
  679. align-items: center;
  680. justify-content: center;
  681. padding: 10upx;
  682. width: 100%;
  683. box-sizing: border-box;
  684. }
  685. .t-result__box-input {
  686. padding: 10upx 0;
  687. width: 100%;
  688. font-size: 28upx;
  689. box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.1);
  690. color: #999;
  691. text-align: center;
  692. background: #fff;
  693. }
  694. .t-result__box-text {
  695. margin-top: 10upx;
  696. font-size: 28upx;
  697. line-height: 2;
  698. }
  699. .t-select {
  700. flex-shrink: 0;
  701. width: 150upx;
  702. padding: 0 30upx;
  703. }
  704. .t-select .t-result__box-input {
  705. border-radius: 10upx;
  706. border: none;
  707. color: #999;
  708. box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.1);
  709. background: #fff;
  710. }
  711. .t-select .t-result__box-input:active {
  712. box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.1);
  713. }
  714. .t-alternative {
  715. display: flex;
  716. flex-wrap: wrap;
  717. /* justify-content: space-between; */
  718. width: 100%;
  719. padding-right: 10upx;
  720. box-sizing: border-box;
  721. }
  722. .t-alternative__item {
  723. margin-left: 12upx;
  724. margin-top: 10upx;
  725. width: 50upx;
  726. height: 50upx;
  727. border-radius: 10upx;
  728. background-color: #fff;
  729. background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee),
  730. linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee);
  731. background-size: 36upx 36upx;
  732. background-position: 0 0, 18upx 18upx;
  733. border: 1px #eee solid;
  734. overflow: hidden;
  735. }
  736. .t-alternative__item-content {
  737. width: 50upx;
  738. height: 50upx;
  739. background: rgba(255, 0, 0, 0.5);
  740. }
  741. .t-alternative__item:active {
  742. transition: all 0.3s;
  743. transform: scale(1.1);
  744. }
  745. </style>