JS练习:选择颜色,得到其HEX 和RGB

JS部分:

function  getRGB(){
  var panelCOLOR=$('#color').val().toUpperCase();
  $('#hex').val(panelCOLOR);
  var hex = [panelCOLOR.substr(1,2),panelCOLOR.substr(3,2),panelCOLOR.substr(5,2)];
  var rgb = [undefined,undefined,undefined];
  var hexABC = ['A','B','C','D','E','F'];
  var hexNum = [10,11,12,13,14,15];
  var i = -1;
  for(var x of hex){
    console.log('hex'+hex);
    i=i+1;
    var m  = x.substr(0,1);
    var n = x.substr(1,1);
    console.log('i'+i);
    if(isNaN(x) == false){
      rgb[i] = parseInt(m)*16+parseInt(n);
      var rgbColor = 'rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';
    }else{
      if(hexABC.indexOf(m)>-1){
        var p = hexABC.indexOf(m);
        m = hexNum[p];
      }
      if(hexABC.indexOf(n)>-1){
        var q = hexABC.indexOf(n);
        n = hexNum[q];
      }
      rgb[i] = parseInt(m)*16+parseInt(n);
      rgbColor = 'rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';
    }
  }
   $('#rgb').val(rgbColor);
}

html 部分:

<table class="table table-bordered">
  <caption class="text-center">click the button,get HEX and RGB the color you choosed!</caption>
  <thead>
   <tr>
    <th class="text-center">HEX</th>
    <th class="text-center">RGB</th>
    <th class="text-center">COLOR</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td><input id="hex" type="text" value="" readonly></td>
    <td><input id="rgb" type="text" value="" readonly></td>
    <td><input id="color" type="color" value=""></td>
   </tr>
   <tr>
    <td class="text-center" colspan="3">
     <button onClick=getRGB()>click me</button>
    </td>
   </tr>
  </tbody>
 </table>

~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值