Vue3实现一个拾色器功能

javascript 复制代码
​
<template>
  <div class="color">
    <button v-if="hasEyeDrop" @click="nativePick">点击取色</button>
    <input v-else type="color" @input="nativePick" v-model="selectedColor" />
    <p>所选颜色: {{ selectedColor }}</p>
    <p>RGB颜色: {{ rgbColor }}</p>
  </div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';

const hasEyeDrop: boolean = 'EyeDropper' in window;
const selectedColor = ref<string>('#ffffff'); // 使用ref来创建响应式数据
let result: Record<string, any> = {}; // 用于存储选择的颜色信息

const rgbColor = computed(() => {
  const hex = selectedColor.value.replace('#', '');
  const r = parseInt(hex.substring(0, 2), 16);
  const g = parseInt(hex.substring(2, 4), 16);
  const b = parseInt(hex.substring(4, 6), 16);
  return `rgb(${r}, ${g}, ${b})`;
});

async function nativePick(e: Event): Promise<void> {
  const val = e ? (e.target as HTMLInputElement).value : null;
  if (val) {
    selectedColor.value = val; // 更新选中的颜色
    console.log('获得颜色: ' + val);
  } else {
    const eyeDropper = new (window as any).EyeDropper(); // 初始化一个EyeDropper对象
    console.log('按Esc可退出');
    try {
      const colorResult = await eyeDropper.open(); // 开始拾取颜色
      selectedColor.value = colorResult.sRGBHex; // 更新选中的颜色
      result = colorResult; // 存储颜色信息
      console.log('获得颜色: ' + colorResult.sRGBHex);

      // 使用result变量
      console.log(result);
    } catch (e) {
      console.log('用户取消了取色');
    }
  }
}
</script>


<!-- // import { ref } from 'vue';
// const hasEyeDrop: boolean = 'EyeDropper' in window;
// const selectedColor = ref<string>('#ffffff'); // 使用ref来创建响应式数据
// let result: Record<string, any> = {}; // 用于存储选择的颜色信息

// async function nativePick(e: Event): Promise<void> {
//   const val = e ? (e.target as HTMLInputElement).value : null;
//   if (val) {
//     selectedColor.value = val; // 更新选中的颜色
//     console.log('获得颜色: ' + val);
//   } else {
//     const eyeDropper = new (window as any).EyeDropper(); // 初始化一个EyeDropper对象
//     console.log('按Esc可退出');
//     try {
//       const colorResult = await eyeDropper.open(); // 开始拾取颜色
//       selectedColor.value = colorResult.sRGBHex; // 更新选中的颜色
//       result = colorResult; // 存储颜色信息
//       console.log('获得颜色: ' + colorResult.sRGBHex);
      
//       // 使用result变量
//       console.log(result);
//     } catch (e) {
//       console.log('用户取消了取色');
//     }
//   }
// }
<style>
.color {
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
}
</style> -->

效果图

相关推荐
未来之窗软件服务1 天前
一体化系统(九)智慧社区综合报表——东方仙盟练气期
大数据·前端·仙盟创梦ide·东方仙盟·东方仙盟一体化
陈天伟教授1 天前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
信看1 天前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
Tony Bai1 天前
【API 设计之道】04 字段掩码模式:让前端决定后端返回什么
前端
苏打水com1 天前
第十四篇:Day40-42 前端架构设计入门——从“功能实现”到“架构思维”(对标职场“大型项目架构”需求)
前端·架构
king王一帅1 天前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
苏打水com1 天前
第十八篇:Day52-54 前端跨端开发进阶——从“多端适配”到“跨端统一”(对标职场“全栈化”需求)
前端
Bigger1 天前
后端拒写接口?前端硬核自救:纯前端实现静态资源下载全链路解析
前端·浏览器·vite
BD_Marathon1 天前
【JavaWeb】路径问题_前端绝对路径问题
前端