将十六进制转换为RGBA

编程入门 行业动态 更新时间:2024-10-21 19:53:57
本文介绍了将十六进制转换为RGBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的小提琴 - jsbin/pitu/1/edit

我想尝试一个简单的hex到rgba转换。我曾经使用渲染颜色使用rgb作为默认,所以当使用farbtastic颜色选择器我通过抓取十六进制值生成的背景颜色(默认为rgb =简单转换)将十六进制值转换为rgb

I wanted to try an easy hex to rgba conversion. Ever browser I've used renders colors using rgb as default so when using the farbtastic color picker I'm converting the hex value to rgb by grabbing the background color the hex value generates (as rgb by default = simple conversion)

我试图将)符号替换为,1)没有工作,所以我去看看如何转换rgb到rgba将工作,我仍然有麻烦。

I tried replacing the ) symbol to , 1), but that didn't work so I went to just see how converting rgb to rgba would work, and I'm still having trouble.

jquery

$('.torgb').val($('#color').css('background-color')); $('.torgba').val().replace(/rgb/g,"rgba");

目标

The goal

推荐答案

//If you write your own code, remember hex color shortcuts (eg., #fff, #000) function hexToRgbA(hex){ var c; if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){ c= hex.substring(1).split(''); if(c.length== 3){ c= [c[0], c[0], c[1], c[1], c[2], c[2]]; } c= '0x'+c.join(''); return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)'; } throw new Error('Bad Hex'); } hexToRgbA('#fbafff') /* returned value: (String) rgba(251,175,255,1) */

更多推荐

将十六进制转换为RGBA

本文发布于:2023-10-27 15:51:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   将十六进制   RGBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!