123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <template>
- <div class="color-picker">
- <div class="picker-saturation-wrap">
- <Saturation :value="color" :hue="hue" @colorChange="value => changeColor(value)" />
- </div>
- <div class="picker-controls">
- <div class="picker-color-wrap">
- <div class="picker-current-color" :style="{ background: currentColor }"></div>
- <Checkboard />
- </div>
- <div class="picker-sliders">
- <div class="picker-hue-wrap">
- <Hue :value="color" :hue="hue" @colorChange="value => changeColor(value)" />
- </div>
- <div class="picker-alpha-wrap">
- <Alpha :value="color" @colorChange="value => changeColor(value)" />
- </div>
- </div>
- </div>
- <div class="picker-field">
- <EditableInput class="input" :value="color" @colorChange="value => changeColor(value)" />
- <div class="straw" @click="openEyeDropper()"><IconNeedle /></div>
- <div class="transparent" @click="selectPresetColor('#00000000')">
- <Checkboard />
- </div>
- </div>
- <div class="picker-presets">
- <div
- class="picker-presets-color"
- v-for="c in themeColors"
- :key="c"
- :style="{ background: c }"
- @click="selectPresetColor(c)"
- ></div>
- </div>
- <div class="picker-gradient-presets">
- <div
- class="picker-gradient-col"
- v-for="(col, index) in presetColors"
- :key="index"
- >
- <div class="picker-gradient-color"
- v-for="c in col"
- :key="c"
- :style="{ background: c }"
- @click="selectPresetColor(c)"
- ></div>
- </div>
- </div>
- <div class="picker-presets">
- <div
- v-for="c in standardColors"
- :key="c"
- class="picker-presets-color"
- :style="{ background: c }"
- @click="selectPresetColor(c)"
- ></div>
- </div>
- <div class="recent-colors-title" v-if="recentColors.length">最近使用:</div>
- <div class="picker-presets">
- <div
- v-for="c in recentColors"
- :key="c"
- class="picker-presets-color alpha"
- @click="selectPresetColor(c)"
- >
- <div class="picker-presets-color-content" :style="{ background: c }"></div>
- </div>
- </div>
- </div>
- </template>
- <script lang="ts" setup>
- import { computed, onMounted, ref, watch } from 'vue'
- import tinycolor, { type ColorFormats } from 'tinycolor2'
- import { debounce } from 'lodash'
- import { toCanvas } from 'html-to-image'
- import message from '@/utils/message'
- import Alpha from './Alpha.vue'
- import Checkboard from './Checkboard.vue'
- import Hue from './Hue.vue'
- import Saturation from './Saturation.vue'
- import EditableInput from './EditableInput.vue'
- const props = withDefaults(defineProps<{
- modelValue?: string
- }>(), {
- modelValue: '#e86b99',
- })
- const emit = defineEmits<{
- (event: 'update:modelValue', payload: string): void
- }>()
- const RECENT_COLORS = 'RECENT_COLORS'
- const presetColorConfig = [
- ['#7f7f7f', '#f2f2f2'],
- ['#0d0d0d', '#808080'],
- ['#1c1a10', '#ddd8c3'],
- ['#0e243d', '#c6d9f0'],
- ['#233f5e', '#dae5f0'],
- ['#632623', '#f2dbdb'],
- ['#4d602c', '#eaf1de'],
- ['#3f3150', '#e6e0ec'],
- ['#1e5867', '#d9eef3'],
- ['#99490f', '#fee9da'],
- ]
- const gradient = (startColor: string, endColor: string, step: number) => {
- const _startColor = tinycolor(startColor).toRgb()
- const _endColor = tinycolor(endColor).toRgb()
- const rStep = (_endColor.r - _startColor.r) / step
- const gStep = (_endColor.g - _startColor.g) / step
- const bStep = (_endColor.b - _startColor.b) / step
- const gradientColorArr = []
- for (let i = 0; i < step; i++) {
- const gradientColor = tinycolor({
- r: _startColor.r + rStep * i,
- g: _startColor.g + gStep * i,
- b: _startColor.b + bStep * i,
- }).toRgbString()
- gradientColorArr.push(gradientColor)
- }
- return gradientColorArr
- }
- const getPresetColors = () => {
- const presetColors = []
- for (const color of presetColorConfig) {
- presetColors.push(gradient(color[1], color[0], 5))
- }
- return presetColors
- }
- const themeColors = ['#000000', '#ffffff', '#eeece1', '#1e497b', '#4e81bb', '#e2534d', '#9aba60', '#8165a0', '#47acc5', '#f9974c']
- const standardColors = ['#c21401', '#ff1e02', '#ffc12a', '#ffff3a', '#90cf5b', '#00af57', '#00afee', '#0071be', '#00215f', '#72349d']
- const hue = ref(-1)
- const recentColors = ref<string[]>([])
- const color = computed({
- get() {
- return tinycolor(props.modelValue).toRgb()
- },
- set(rgba: ColorFormats.RGBA) {
- const rgbaString = `rgba(${[rgba.r, rgba.g, rgba.b, rgba.a].join(',')})`
- emit('update:modelValue', rgbaString)
- },
- })
- const presetColors = getPresetColors()
- const currentColor = computed(() => {
- return `rgba(${[color.value.r, color.value.g, color.value.b, color.value.a].join(',')})`
- })
- const selectPresetColor = (colorString: string) => {
- hue.value = tinycolor(colorString).toHsl().h
- emit('update:modelValue', colorString)
- }
- // 每次选择非预设颜色时,需要将该颜色加入到最近使用列表中
- const updateRecentColorsCache = debounce(function() {
- const _color = tinycolor(color.value).toRgbString()
- if (!recentColors.value.includes(_color)) {
- recentColors.value = [_color, ...recentColors.value]
- const maxLength = 10
- if (recentColors.value.length > maxLength) {
- recentColors.value = recentColors.value.slice(0, maxLength)
- }
- }
- }, 300, { trailing: true })
- onMounted(() => {
- const recentColorsCache = localStorage.getItem(RECENT_COLORS)
- if (recentColorsCache) recentColors.value = JSON.parse(recentColorsCache)
- })
- watch(recentColors, () => {
- const recentColorsCache = JSON.stringify(recentColors.value)
- localStorage.setItem(RECENT_COLORS, recentColorsCache)
- })
- const changeColor = (value: ColorFormats.RGBA | ColorFormats.HSLA | ColorFormats.HSVA) => {
- if ('h' in value) {
- hue.value = value.h
- color.value = tinycolor(value).toRgb()
- }
- else {
- hue.value = tinycolor(value).toHsl().h
- color.value = value
- }
- updateRecentColorsCache()
- }
- // 打开取色吸管
- // 检查环境是否支持原生取色吸管,支持则使用原生吸管,否则使用自定义吸管
- const openEyeDropper = () => {
- const isSupportedEyeDropper = 'EyeDropper' in window
- if (isSupportedEyeDropper) browserEyeDropper()
- else customEyeDropper()
- }
- // 原生取色吸管
- const browserEyeDropper = () => {
- message.success('按 ESC 键关闭取色吸管', { duration: 0 })
- // eslint-disable-next-line
- const eyeDropper = new (window as any).EyeDropper()
- eyeDropper.open().then((result: { sRGBHex: string }) => {
- const tColor = tinycolor(result.sRGBHex)
- hue.value = tColor.toHsl().h
- color.value = tColor.toRgb()
- message.closeAll()
- updateRecentColorsCache()
- }).catch(() => {
- message.closeAll()
- })
- }
- // 基于 Canvas 的自定义取色吸管
- const customEyeDropper = () => {
- const targetRef: HTMLElement | null = document.querySelector('.canvas')
- if (!targetRef) return
- const maskRef = document.createElement('div')
- maskRef.style.cssText = 'position: fixed; top: 0; left: 0; bottom: 0; right: 0; z-index: 9999; cursor: wait;'
- document.body.appendChild(maskRef)
- const colorBlockRef = document.createElement('div')
- colorBlockRef.style.cssText = 'position: absolute; top: -100px; left: -100px; width: 16px; height: 16px; border: 1px solid #000; z-index: 999'
- maskRef.appendChild(colorBlockRef)
- const { left, top, width, height } = targetRef.getBoundingClientRect()
- const filter = (node: HTMLElement) => {
- if (node.tagName && node.tagName.toUpperCase() === 'FOREIGNOBJECT') return false
- if (node.classList && node.classList.contains('operate')) return false
- return true
- }
- toCanvas(targetRef, { filter, fontEmbedCSS: '', width, height, canvasWidth: width, canvasHeight: height, pixelRatio: 1 }).then(canvasRef => {
- canvasRef.style.cssText = `position: absolute; top: ${top}px; left: ${left}px; cursor: crosshair;`
- maskRef.style.cursor = 'default'
- maskRef.appendChild(canvasRef)
- const ctx = canvasRef.getContext('2d')
- if (!ctx) return
- let currentColor = ''
- const handleMousemove = (e: MouseEvent) => {
- const x = e.x
- const y = e.y
- const mouseX = x - left
- const mouseY = y - top
- const [r, g, b, a] = ctx.getImageData(mouseX, mouseY, 1, 1).data
- currentColor = `rgba(${r}, ${g}, ${b}, ${(a / 255).toFixed(2)})`
- colorBlockRef.style.left = x + 10 + 'px'
- colorBlockRef.style.top = y + 10 + 'px'
- colorBlockRef.style.backgroundColor = currentColor
- }
- const handleMouseleave = () => {
- currentColor = ''
- colorBlockRef.style.left = '-100px'
- colorBlockRef.style.top = '-100px'
- colorBlockRef.style.backgroundColor = ''
- }
- const handleMousedown = (e: MouseEvent) => {
- if (currentColor && e.button === 0) {
- const tColor = tinycolor(currentColor)
- hue.value = tColor.toHsl().h
- color.value = tColor.toRgb()
- updateRecentColorsCache()
- }
- document.body.removeChild(maskRef)
-
- canvasRef.removeEventListener('mousemove', handleMousemove)
- canvasRef.removeEventListener('mouseleave', handleMouseleave)
- window.removeEventListener('mousedown', handleMousedown)
- }
- canvasRef.addEventListener('mousemove', handleMousemove)
- canvasRef.addEventListener('mouseleave', handleMouseleave)
- window.addEventListener('mousedown', handleMousedown)
- }).catch(() => {
- message.error('取色吸管初始化失败')
- document.body.removeChild(maskRef)
- })
- }
- </script>
- <style lang="scss" scoped>
- .color-picker {
- position: relative;
- width: 240px;
- background: #fff;
- user-select: none;
- margin-bottom: -10px;
- }
- .picker-saturation-wrap {
- width: 100%;
- padding-bottom: 50%;
- position: relative;
- overflow: hidden;
- }
- .picker-controls {
- display: flex;
- }
- .picker-sliders {
- padding: 4px 0;
- flex: 1;
- }
- .picker-hue-wrap {
- position: relative;
- height: 10px;
- }
- .picker-alpha-wrap {
- position: relative;
- height: 10px;
- margin-top: 4px;
- overflow: hidden;
- }
- .picker-color-wrap {
- width: 24px;
- height: 24px;
- position: relative;
- margin-top: 4px;
- margin-right: 4px;
- outline: 1px dashed rgba($color: #666, $alpha: .12);
- .checkerboard {
- background-size: auto;
- }
- }
- .picker-current-color {
- @include absolute-0();
- z-index: 2;
- }
- .picker-field {
- display: flex;
- margin-bottom: 8px;
- .transparent {
- width: 24px;
- height: 24px;
- margin-top: 4px;
- margin-left: 8px;
- position: relative;
- cursor: pointer;
- &::after {
- content: '';
- width: 26px;
- height: 2px;
- position: absolute;
- top: 11px;
- left: -1px;
- transform: rotate(-45deg);
- background-color: #f00;
- }
- .checkerboard {
- background-size: auto;
- }
- }
- .straw {
- width: 24px;
- height: 24px;
- margin-top: 4px;
- margin-left: 8px;
- display: flex;
- justify-content: center;
- align-items: center;
- font-size: 20px;
- background-color: #f5f5f5;
- outline: 1px solid #f1f1f1;
- cursor: pointer;
- }
- .input {
- flex: 1;
- }
- }
- .picker-presets {
- @include flex-grid-layout();
- }
- .picker-presets-color {
- @include flex-grid-layout-children(10, 7%);
- height: 0;
- padding-bottom: 7%;
- flex-shrink: 0;
- position: relative;
- cursor: pointer;
- &.alpha {
- background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAADBJREFUOE9jfPbs2X8GPEBSUhKfNAPjqAHDIgz+//+PNx08f/4cfzoYNYCBceiHAQC5flV5JzgrxQAAAABJRU5ErkJggg==);
- }
- }
- .picker-presets-color-content {
- @include absolute-0();
- }
- .picker-gradient-presets {
- @include flex-grid-layout();
- }
- .picker-gradient-col {
- @include flex-grid-layout-children(10, 7%);
- display: flex;
- flex-direction: column;
- }
- .picker-gradient-color {
- width: 100%;
- height: 16px;
- position: relative;
- cursor: pointer;
- }
- .recent-colors-title {
- font-size: 12px;
- margin-bottom: 4px;
- }
- </style>
|