diff --git a/src/components/ColorPicker/Alpha.vue b/src/components/ColorPicker/Alpha.vue index 737bd3b2..36dd42ae 100644 --- a/src/components/ColorPicker/Alpha.vue +++ b/src/components/ColorPicker/Alpha.vue @@ -28,13 +28,13 @@ export default defineComponent({ Checkboard, }, props: { - modelValue: { + value: { type: Object as PropType, required: true, }, }, setup(props, { emit }) { - const color = computed(() => props.modelValue) + const color = computed(() => props.value) const gradientColor = computed(() => { const rgbaStr = [color.value.r, color.value.g, color.value.b].join(',') return `linear-gradient(to right, rgba(${rgbaStr}, 0) 0%, rgba(${rgbaStr}, 1) 100%)` @@ -54,7 +54,7 @@ export default defineComponent({ else a = Math.round(left * 100 / containerWidth) / 100 if(color.value.a !== a) { - emit('update:modelValue', { + emit('change', { r: color.value.r, g: color.value.g, b: color.value.b, diff --git a/src/components/ColorPicker/EditableInput.vue b/src/components/ColorPicker/EditableInput.vue index 858350c1..5ac96b17 100644 --- a/src/components/ColorPicker/EditableInput.vue +++ b/src/components/ColorPicker/EditableInput.vue @@ -15,7 +15,7 @@ import tinycolor, { ColorFormats } from 'tinycolor2' export default defineComponent({ name: 'editable-input', props: { - modelValue: { + value: { type: Object as PropType, required: true, }, @@ -23,14 +23,14 @@ export default defineComponent({ setup(props, { emit }) { const val = computed(() => { let _hex = '' - if(props.modelValue.a < 1) _hex = tinycolor(props.modelValue).toHex8String().toUpperCase() - else _hex = tinycolor(props.modelValue).toHexString().toUpperCase() + if(props.value.a < 1) _hex = tinycolor(props.value).toHex8String().toUpperCase() + else _hex = tinycolor(props.value).toHexString().toUpperCase() return _hex.replace('#', '') }) const handleInput = (e: InputEvent) => { const value = (e.target as HTMLInputElement).value - if(value.length >= 6) emit('update:modelValue', tinycolor(value).toRgb()) + if(value.length >= 6) emit('change', tinycolor(value).toRgb()) } return { diff --git a/src/components/ColorPicker/Hue.vue b/src/components/ColorPicker/Hue.vue index 2b074d39..1ad3304e 100644 --- a/src/components/ColorPicker/Hue.vue +++ b/src/components/ColorPicker/Hue.vue @@ -22,25 +22,33 @@ import tinycolor, { ColorFormats } from 'tinycolor2' export default defineComponent({ name: 'hue', props: { - modelValue: { + value: { type: Object as PropType, required: true, }, + hue: { + type: Number, + required: true, + }, }, setup(props, { emit }) { const oldHue = ref(0) const pullDirection = ref('') - const color = computed(() => tinycolor(props.modelValue).toHsl()) + const color = computed(() => { + const hsla = tinycolor(props.value).toHsl() + if(hsla.s === 0) hsla.h = props.hue + return hsla + }) const pointerLeft = computed(() => { if(color.value.h === 0 && pullDirection.value === 'right') return '100%' return color.value.h * 100 / 360 + '%' }) - watch(() => props.modelValue, () => { - const hsl = tinycolor(props.modelValue).toHsl() - const h = hsl.h + watch(() => props.value, () => { + const hsla = tinycolor(props.value).toHsl() + const h = hsla.s === 0 ? props.hue : hsla.h if(h !== 0 && h - oldHue.value > 0) pullDirection.value = 'right' if(h !== 0 && h - oldHue.value < 0) pullDirection.value = 'left' oldHue.value = h @@ -63,14 +71,12 @@ export default defineComponent({ h = (360 * percent / 100) } if(color.value.h !== h) { - const rgba = tinycolor({ + emit('change', { h, l: color.value.l, s: color.value.s, a: color.value.a, - }).toRgb() - - emit('update:modelValue', rgba) + }) } } diff --git a/src/components/ColorPicker/Saturation.vue b/src/components/ColorPicker/Saturation.vue index 93f5abdb..79cf0e06 100644 --- a/src/components/ColorPicker/Saturation.vue +++ b/src/components/ColorPicker/Saturation.vue @@ -27,20 +27,28 @@ import clamp from 'lodash/clamp' export default defineComponent({ name: 'saturation', props: { - modelValue: { + value: { type: Object as PropType, required: true, }, + hue: { + type: Number, + required: true, + }, }, setup(props, { emit }) { - const color = computed(() => tinycolor(props.modelValue).toHsv()) + const color = computed(() => { + const hsva = tinycolor(props.value).toHsv() + if(hsva.s === 0) hsva.h = props.hue + return hsva + }) const bgColor = computed(() => `hsl(${color.value.h}, 100%, 50%)`) const pointerTop = computed(() => (-(color.value.v * 100) + 1) + 100 + '%') const pointerLeft = computed(() => color.value.s * 100 + '%') const emitChangeEvent = throttle(function(param) { - emit('update:modelValue', param) + emit('change', param) }, 20, { leading: true, trailing: false }) const saturationRef = ref(null) @@ -57,14 +65,12 @@ export default defineComponent({ const saturation = left / containerWidth const bright = clamp(-(top / containerHeight) + 1, 0, 1) - const rgba = tinycolor({ + emitChangeEvent({ h: color.value.h, s: saturation, v: bright, a: color.value.a, - }).toRgb() - - emitChangeEvent(rgba) + }) } diff --git a/src/components/ColorPicker/index.vue b/src/components/ColorPicker/index.vue index 76f0e1f5..4b8a7450 100644 --- a/src/components/ColorPicker/index.vue +++ b/src/components/ColorPicker/index.vue @@ -1,7 +1,7 @@