• vue.js3: html的十六进制和rgb颜色互相转换(vue@3.2.37)


    一,js代码:

    <template>
      <div>
        <div style="450px;margin: auto;text-align: center;font-size: 20px;color:#777777;padding-top: 10px;padding-bottom: 10px;">
          RGB转十六进制
        </div>
        <div style="450px;margin: auto;display: flex;flex-direction: row;background: rgb(248, 248, 248);">
          <div style="200px;">
    
            <div style="display: flex;flex-direction: row;margin-top: 10px;">
              <div style="color:#ff0000;100px;line-height: 30px;height:30px;text-align: right;">Red:</div>
          <el-input v-model="red" placeholder="红色" @input="colorRGBtoHex" type="number" style="100px;">
            <template #suffix><i class="el-icon-user el-input__icon"></i></template>
          </el-input>
            </div>
    
            <div style="display: flex;flex-direction: row;margin-top: 10px;">
              <div style="color:#00ff00;100px;line-height: 30px;height:30px;text-align: right;">Green:</div>
          <el-input v-model="green" placeholder="绿色" @input="colorRGBtoHex" type="number" style="100px;">
            <template #suffix><i class="el-icon-user el-input__icon"></i></template>
          </el-input>
            </div>
    
            <div style="display: flex;flex-direction: row;margin-top: 10px;margin-bottom: 10px;">
              <div style="color:#0000ff;100px;line-height: 30px;height:30px;text-align: right;">Blue:</div>
          <el-input v-model="blue" placeholder="蓝色" @input="colorRGBtoHex" type="number" style="100px;">
            <template #suffix><i class="el-icon-user el-input__icon"></i></template>
          </el-input>
            </div>
    
    
          </div>
          <div style="200px;margin-left:50px;">
            <div style="90px;text-align:left;margin-top:10px;">{{hexColor}}</div>
            <div :style="{'80px',marginTop:'10px',height:'80px',borderRadius:'10px',background:hexColor}"></div>
          </div>
        </div>
    
        <div style="450px;margin: auto;text-align: center;font-size: 20px;color:#777777;padding-top: 10px;padding-bottom: 10px;">
          十六进制转RGB
        </div>
        <div style="450px;margin: auto;display: flex;flex-direction: row;height:136px;background: rgb(248, 248, 248);">
          <div style="display: flex;flex-direction: row;margin-top: 10px;">
            <div style="color:#000000;100px;line-height: 30px;height:30px;text-align: right;">Hex:</div>
          <el-input v-model="hex" placeholder="十六进制颜色" @input="colorHextoRGB"
                    maxlength="7" minlength="4" style="100px;height:32px;">
            <template #suffix><i class="el-icon-user el-input__icon"></i></template>
          </el-input>
          </div>
          <div style="200px;margin-left:50px;">
            <div style="90px;text-align:left;margin-top:10px;">{{rgbColor}}</div>
            <div :style="{'80px',marginTop:'10px',height:'80px',borderRadius:'10px',background:rgbColor}"></div>
          </div>
        </div>
      </div>
    </template>
    
    <script>
    import {ref} from "vue"
    export default {
      name: "ColorComp",
      setup() {
        //从单一颜色值得到十六进制
        const getHex = (color) => {
          let hex = Number(color).toString(16)
          hex = hex.length == 1 ? '0' + hex : hex;
          return hex
        }
    
        //rgb颜色转十六进制
        const colorRGBtoHex = () => {
          if (red.value > 255) {
             red.value = 255;
          }
          if (red.value < 0) {
            red.value = 0;
          }
          if (green.value > 255) {
            green.value = 255;
          }
          if (green.value < 0) {
            green.value = 0;
          }
          if (blue.value > 255) {
            blue.value = 255;
          }
          if (blue.value < 0) {
            blue.value = 0;
          }
          let r = red.value;
          let g = green.value;
          let b = blue.value;
    
          var hex = '#' + getHex(r) + getHex(g) + getHex(b);
          hexColor.value = hex.toUpperCase();
        }
        //输入的红色
        const red = ref(0);
        //输入的绿色
        const green = ref(0);
        //输入的蓝色
        const blue = ref(0);
        //从rgb转换得到的十六进制颜色
        const hexColor = ref('#000000');
        //从十六进制转换得到的rgb颜色
        const rgbColor = ref('rgb(0,0,0)');
        //输入的十六进制
        const hex = ref('#000000');
    
        const isInHex = (c) => {
           let hexArr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
           for (let i = 0; i < hexArr.length; i += 1) {
            if (hexArr[i] === c) {
              return true;
            }
          }
           return false;
        }
    
        //十六进制颜色转rgb
        const colorHextoRGB = () => {
          let color = hex.value.toLowerCase();
          //判断第一个字符是否是#
          if (color[0] !== '#') {
             color = '#'+color;
             hex.value = color.toUpperCase();
          }
          //判断剩余的字符是否是十六进制
          for (let i = 1; i < color.length; i += 1) {
            let curChar = color[i];
            //console.log(curChar);
            if (isInHex(curChar) == false) {
              color = color.replace(curChar,'');
              hex.value = color.toUpperCase();
            }
          }
    
          // 如果只有三位的值,需变成六位,如:#fff => #ffffff
          if (color.length === 4) {
            var colorNew = "#";
            for (let i = 1; i < 4; i += 1) {
              colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
            }
            color = colorNew;
          }
          // 处理六位的颜色值,转为RGB
          var colorChange = [];
          for (let i = 1; i < 7; i += 2) {
            colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
          }
          let rgb =  "RGB(" + colorChange.join(",") + ")";
          rgbColor.value = rgb;
          hex.value = color.toUpperCase();
        }
    
        return {
          colorRGBtoHex,
          red,
          green,
          blue,
          hexColor,
          rgbColor,
          hex,
          colorHextoRGB,
        }
      }
    }
    </script>
    
    <style scoped>
    </style>

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/
             或: https://gitee.com/liuhongdi

    说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,查看效果

    三,查看vue框架的版本:

    liuhongdi@lhdpc:/data/vue/child$ npm list vue
    child@0.1.0 /data/vue/child
    ├─┬ @vue/cli-plugin-babel@5.0.6
    │ └─┬ @vue/babel-preset-app@5.0.6
    │   └── vue@3.2.37 deduped
    └─┬ vue@3.2.37
      └─┬ @vue/server-renderer@3.2.37
        └── vue@3.2.37 deduped
  • 相关阅读:
    Java基础知识面试题(2021年最新版,持续更新...)整理
    windows10 cmd窗口输出卡住(看这篇就够了)
    windows10 powershell窗口输出卡住(看这篇就够了)
    Golang函数相关
    内存对齐详解
    Go编程模式Pipeline
    管道符、重定向与环境变量(Linux就该这么学第三章)
    GMP模型简介
    新手必须掌握的Linux命令(Linux就该这么学第二章)
    Goland运行项目报错:CreateProcess error=216, 该版本的 %1 与你运行的 Windows 版本不兼容。请查看计算机的系统信息,然后联系软件发布者。
  • 原文地址:https://www.cnblogs.com/architectforest/p/16438699.html
Copyright © 2020-2023  润新知