Skip to content

Commit 74f655d

Browse files
committed
refactor(color-picker): use rgba background
1 parent e4d2d3d commit 74f655d

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

examples/sites/demos/pc/app/color-picker/alpha.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ test('测试Alpha', async ({ page }) => {
99
await page.getByText('用户选择了取消').click()
1010
await page.locator('.tiny-color-picker__inner').click()
1111
await page.getByRole('button', { name: '选择' }).click()
12-
// format not implment if enable alpha will use rgba
13-
await page.getByText('rgba(128, 64, 64, 1)').click()
12+
// default is hex
13+
await page.getByText('#804040FF').click()
1414
})

packages/renderless/src/color-picker/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IColorSelectPanelRef } from '@/types'
2-
import { parseHSV } from './utils/hsv-to-rgb'
2+
import type { Color } from './utils/color'
33

44
export const updateModelValue = (val, emit) => {
55
emit('update:modelValue', val)
@@ -11,13 +11,9 @@ export const toggleVisible = (isShow: IColorSelectPanelRef<boolean>) => {
1111
}
1212
}
1313

14-
export const useEvent = (state, emit, changeVisible) => {
14+
export const useEvent = (state, emit, changeVisible, color: Color) => {
1515
const onConfirm = (val) => {
16-
state.hex = val
17-
if (val.includes('hsv') || val.includes('hsva')) {
18-
const { r, g, b } = parseHSV(val)
19-
state.hex = `rgb(${r}, ${g}, ${b})`
20-
}
16+
color.fromString(val)
2117
updateModelValue(val, emit)
2218
emit('confirm', val)
2319
changeVisible(false)

packages/renderless/src/color-picker/utils/color.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ export class Color {
340340
v: this._value
341341
})
342342
}
343+
toRgba() {
344+
return {
345+
...hsv2rgb({
346+
h: this._hue,
347+
s: this._sat,
348+
v: this._value
349+
}),
350+
a: this._alpha / 100
351+
}
352+
}
343353
onChange() {
344354
const { _hue, _sat, _value, _alpha, format, enableAlpha } = this
345355
if (!enableAlpha) {

packages/renderless/src/color-picker/vue.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export const renderless = (props, ctx: ISharedRenderlessParamHooks, { emit }: IS
5050
ctx.watch(
5151
() => props.modelValue,
5252
() => {
53-
if (color.value !== props.modelValue && props.modelValue) {
54-
color.fromString(props.modelValue)
55-
}
53+
color.fromString(props.modelValue)
54+
const { r, g, b, a } = color.toRgba()
55+
state.hex = `rgba(${r}, ${g}, ${b}, ${a})`
5656
}
5757
)
5858
const changeVisible = toggleVisible(isShow)
59-
const { onConfirm, onCancel } = useEvent(state, emit, changeVisible)
59+
const { onConfirm, onCancel } = useEvent(state, emit, changeVisible, color)
6060
const api = {
6161
state,
6262
changeVisible,

0 commit comments

Comments
 (0)