index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="jnpf-color-picker">
  3. <view class="color-box" :class="{'color-disabled':disabled}" @click="open">
  4. <view class="colorVal" :style="{backgroundColor:bgColor}">
  5. <uni-icons type="bottom" size="10" color='#c7c7c7'></uni-icons>
  6. </view>
  7. </view>
  8. <t-color-picker ref="colorPicker" :color="innerValue" @confirm="confirm" :colorFormat='colorFormat' />
  9. </view>
  10. </template>
  11. <script>
  12. import tColorPicker from './t-color-picker.vue'
  13. import conversion from '@/libs/color-typeConversion.js'
  14. export default {
  15. name: 'jnpf-color-picker',
  16. components: {
  17. tColorPicker
  18. },
  19. props: {
  20. modelValue: {
  21. default: ''
  22. },
  23. colorFormat: {
  24. type: String,
  25. default: 'hex'
  26. },
  27. disabled: {
  28. type: Boolean,
  29. default: false
  30. },
  31. },
  32. data() {
  33. return {
  34. bgColor: "#fff",
  35. hsvObj: {},
  36. hsvList: ['h', 's', 'v'],
  37. innerValue: ''
  38. };
  39. },
  40. watch: {
  41. modelValue: {
  42. handler(val) {
  43. this.innerValue = val
  44. this.setDafault()
  45. },
  46. immediate: true
  47. }
  48. },
  49. methods: {
  50. open(item) {
  51. if (this.disabled) return
  52. this.$refs.colorPicker.open();
  53. },
  54. confirm(e) {
  55. this.bgColor = e.colorVal
  56. this.$emit('update:modelValue', this.bgColor)
  57. this.$emit('change', this.bgColor)
  58. },
  59. setDafault() {
  60. if (!this.innerValue) return this.bgColor = '#fff'
  61. this.$nextTick(() => {
  62. if (this.colorFormat === 'hsv') {
  63. let color = ""
  64. var result = this.innerValue.match(/\(([^)]*)\)/)
  65. result[1].split(',').forEach((o, i) => {
  66. this.$set(this[this.colorFormat + 'Obj'], this[this.colorFormat + 'List'][i],
  67. o)
  68. })
  69. color = conversion.hsv2rgb(this[this.colorFormat + 'Obj'].h, this[this.colorFormat + 'Obj']
  70. .s,
  71. this[this.colorFormat + 'Obj'].v)
  72. this.bgColor = `rgb(${color.r},${color.g},${color.b})`
  73. } else {
  74. this.bgColor = this.innerValue
  75. }
  76. })
  77. }
  78. }
  79. };
  80. </script>
  81. <style lang="scss">
  82. .jnpf-color-picker {
  83. flex: 1;
  84. display: flex;
  85. flex-direction: row;
  86. align-items: center;
  87. justify-content: flex-end;
  88. .color-box {
  89. width: 70rpx;
  90. height: 70rpx;
  91. border: 1px solid #e6e6e6;
  92. background-color: #fff;
  93. border-radius: 10rpx;
  94. display: flex;
  95. flex-direction: column;
  96. align-items: center;
  97. justify-content: center;
  98. &.color-disabled {
  99. background-color: #E6E6E6 !important;
  100. border-color: #D9D9D9 !important;
  101. }
  102. .colorVal {
  103. width: 48rpx;
  104. height: 48rpx;
  105. border: 1px solid #999;
  106. border-radius: 6rpx;
  107. display: flex;
  108. flex-direction: column;
  109. align-items: center;
  110. justify-content: center;
  111. background-color: #fff;
  112. .colorVal-inner {
  113. width: 100%;
  114. height: 100%;
  115. color: #c7c7c7;
  116. display: flex;
  117. flex-direction: column;
  118. justify-content: center;
  119. align-items: center;
  120. text-align: center;
  121. }
  122. }
  123. }
  124. }
  125. </style>