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