|
1 | 1 | <template>
|
2 | 2 | <div ref="pickerAreaRef" class="spectrum-map" :style="pickerStyle">
|
3 |
| - <div class="spectrum"></div> |
| 3 | + <div class="spectrum" @click="spectrumClick"></div> |
4 | 4 | <div class="picker-cursor" :style="pointerStyle"></div>
|
5 | 5 | </div>
|
6 | 6 | </template>
|
7 | 7 |
|
8 | 8 | <script lang="ts" setup>
|
9 |
| - import { ref, reactive, onMounted, computed } from 'vue' |
10 |
| - import { clamp, isDefined, usePointerSwipe } from '@vueuse/core' |
11 |
| - import {Color} from '@/utils/color/color' |
| 9 | +import { ref, reactive, onMounted, computed } from "vue" |
| 10 | +import { clamp, isDefined, usePointerSwipe } from "@vueuse/core" |
| 11 | +import { Color } from "@/utils/color/color" |
12 | 12 |
|
13 |
| - const props = defineProps<{ |
14 |
| - red: number |
15 |
| - green: number |
16 |
| - blue: number |
17 |
| - alpha: number |
18 |
| - hue: number |
19 |
| - saturation: number |
20 |
| - value: number |
21 |
| - updateColor: Function |
22 |
| - }>() |
| 13 | +const props = defineProps<{ |
| 14 | + red: number; |
| 15 | + green: number; |
| 16 | + blue: number; |
| 17 | + alpha: number; |
| 18 | + hue: number; |
| 19 | + saturation: number; |
| 20 | + value: number; |
| 21 | + updateColor: Function; |
| 22 | +}>() |
23 | 23 |
|
24 |
| - const pickerAreaRef = ref<HTMLDivElement>() |
| 24 | +const pickerAreaRef = ref<HTMLDivElement>() |
25 | 25 |
|
26 |
| - const state = reactive({ |
27 |
| - width: 0, |
28 |
| - height: 0, |
29 |
| - }) |
| 26 | +const state = reactive({ |
| 27 | + width: 0, |
| 28 | + height: 0 |
| 29 | +}) |
30 | 30 |
|
31 |
| - const offsetLeft = computed(() => { |
32 |
| - return Math.round(props.saturation * state.width - 8) |
33 |
| - }) |
| 31 | +const offsetLeft = computed(() => { |
| 32 | + return Math.round(props.saturation * state.width - 8) |
| 33 | +}) |
34 | 34 |
|
35 |
| - const offsetTop = computed(() => |
36 |
| - Math.max(Math.round(state.height - props.value * state.height - 14), -6), |
37 |
| - ) |
| 35 | +const offsetTop = computed(() => |
| 36 | + Math.max(Math.round(state.height - props.value * state.height - 14), -6) |
| 37 | +) |
38 | 38 |
|
39 |
| - const pointerStyle = computed(() => { |
40 |
| - return { |
41 |
| - backgroundColor: `rgb(${props.red}, ${props.green}, ${props.blue})`, |
42 |
| - left: `${offsetLeft.value}px`, |
43 |
| - top: `${offsetTop.value}px`, |
44 |
| - } |
45 |
| - }) |
| 39 | +const pointerStyle = computed(() => { |
| 40 | + return { |
| 41 | + backgroundColor: `rgb(${props.red}, ${props.green}, ${props.blue})`, |
| 42 | + left: `${offsetLeft.value}px`, |
| 43 | + top: `${offsetTop.value}px` |
| 44 | + } |
| 45 | +}) |
46 | 46 |
|
47 |
| - const pickerStyle = computed(() => { |
48 |
| - const color = new Color(`hsva(${props.hue},1,1,1)`) |
49 |
| - // const color = new Color(new HSVA(props.hue, 1, 1, 1)) |
50 |
| - // const { r, g, b } = color.rgba |
51 |
| - return { |
52 |
| - backgroundColor: color.rgb, |
53 |
| - } |
54 |
| - }) |
| 47 | +const pickerStyle = computed(() => { |
| 48 | + const color = new Color(`hsva(${props.hue},1,1,1)`) |
| 49 | + // const color = new Color(new HSVA(props.hue, 1, 1, 1)) |
| 50 | + // const { r, g, b } = color.rgba |
| 51 | + return { |
| 52 | + backgroundColor: color.rgb |
| 53 | + } |
| 54 | +}) |
55 | 55 |
|
56 |
| - onMounted(() => { |
57 |
| - if (isDefined(pickerAreaRef)) { |
58 |
| - state.width = pickerAreaRef.value.clientWidth |
59 |
| - state.height = pickerAreaRef.value.clientHeight |
60 |
| - } |
61 |
| - }) |
| 56 | +onMounted(() => { |
| 57 | + if (isDefined(pickerAreaRef)) { |
| 58 | + state.width = pickerAreaRef.value.clientWidth |
| 59 | + state.height = pickerAreaRef.value.clientHeight |
| 60 | + } |
| 61 | +}) |
62 | 62 |
|
63 |
| - let rect: DOMRect | undefined |
| 63 | +let rect: DOMRect | undefined |
64 | 64 |
|
65 |
| - const getColor = () => { |
66 |
| - if (!isDefined(rect)) return |
67 |
| - let x = clamp(posEnd.x - rect.x, 0, state.width) |
68 |
| - let y = clamp(posEnd.y - rect.y, 0, state.height) |
69 |
| - const saturation = x / state.width |
70 |
| - const value = 1 - y / state.height |
71 |
| - const color = new Color(`hsva(${props.hue},${saturation},${value},${props.alpha})`) |
72 |
| - // const color = new Color(new HSVA(props.hue, saturation, value, props.alpha)) |
73 |
| - return { |
74 |
| - ...color.getRgba(), |
75 |
| - saturation, |
76 |
| - value, |
77 |
| - } |
| 65 | +const getColor = () => { |
| 66 | + if (!isDefined(rect)) return |
| 67 | + let x = clamp(posEnd.x - rect.x, 0, state.width) |
| 68 | + let y = clamp(posEnd.y - rect.y, 0, state.height) |
| 69 | + const saturation = x / state.width |
| 70 | + const value = 1 - y / state.height |
| 71 | + const color = new Color( |
| 72 | + `hsva(${props.hue},${saturation},${value},${props.alpha})` |
| 73 | + ) |
| 74 | + // const color = new Color(new HSVA(props.hue, saturation, value, props.alpha)) |
| 75 | + return { |
| 76 | + ...color.getRgba(), |
| 77 | + saturation, |
| 78 | + value |
78 | 79 | }
|
| 80 | +} |
| 81 | +
|
| 82 | +const spectrumClick = (obj: any) => { |
| 83 | + props.updateColor(getColor(), "onChange") |
| 84 | +} |
79 | 85 |
|
80 |
| - const { posEnd } = usePointerSwipe(pickerAreaRef, { |
81 |
| - threshold: 0, |
82 |
| - onSwipeStart() { |
83 |
| - rect = pickerAreaRef.value?.getBoundingClientRect() |
84 |
| - props.updateColor(getColor(), 'onStartChange') |
85 |
| - }, |
86 |
| - onSwipe() { |
87 |
| - props.updateColor(getColor(), 'onChange') |
88 |
| - }, |
89 |
| - onSwipeEnd() { |
90 |
| - props.updateColor(getColor(), 'onEndChange') |
91 |
| - }, |
92 |
| - }) |
| 86 | +const { posEnd } = usePointerSwipe(pickerAreaRef, { |
| 87 | + threshold: 0, |
| 88 | + onSwipeStart() { |
| 89 | + rect = pickerAreaRef.value?.getBoundingClientRect() |
| 90 | + props.updateColor(getColor(), "onStartChange") |
| 91 | + }, |
| 92 | + onSwipe() { |
| 93 | + props.updateColor(getColor(), "onChange") |
| 94 | + }, |
| 95 | + onSwipeEnd() { |
| 96 | + props.updateColor(getColor(), "onEndChange") |
| 97 | + } |
| 98 | +}) |
93 | 99 | </script>
|
0 commit comments