您的位置:

Hex转RGB

一、Hex转RGB Echarts

在数据可视化中,经常会用到Echarts中的颜色配置,而Echarts中的颜色配置可以采用16进制表示,也可以采用rgb表示,因此hex转rgb是非常必要的。Echarts提供了一个颜色数组,我们常用到的是"default"、"light"、"dark"三种主题,可以通过下面的代码将hex格式的颜色转成rgb格式。

function hexToRgb(hex) {    
    var r = parseInt(hex.slice(1,3), 16),    
        g = parseInt(hex.slice(3,5), 16),    
        b = parseInt(hex.slice(5,7), 16);    
    return "rgb(" + r + ", " + g + ", " + b + ")";    
}

在Echarts中使用时,可以直接调用该函数将hex颜色码转成rgb:

backgroundColor: hexToRgb('#ffffff')

这样就可以将背景色设置成白色。

二、Hex转换

1. Hex转换为十进制数字

16进制(Hexadecimal)常用于表示颜色、字符以及地址等等,在JavaScript中,我们可以通过parseInt函数将它们转成10进制数字。下面的代码是将16进制数'0x1f'转成10进制数字31:

var num = 0x1f;  
console.log(num); // 31

2. 十进制数字转换为Hex

在Web开发中,常常需要将10进制数字转换为16进制数,例如颜色值的读取、url地址、IPv6地址等等。下面的代码将数字255转换为16进制:

var hex = (255).toString(16);  
console.log(hex); // ff

三、Hex转Ascii

Ascii码是一种美国信息交换标准代码,用于把每个字母、数字、标点符号及其他符号转换成计算机可识别的数字代码。下面的代码将16进制数转换成对应的Ascii码:

function hexToAscii(hex){  
    var arr = hex.split("");  
    var ascii = "";  
    for(var i=0;i

   

调用该函数可以将16进制转换成Ascii:

var ascii = hexToAscii("68656c6c6f");  
console.log(ascii); // hello

四、Hex转QByteArray

在Qt开发中,QByteArray是一个非常常用的数据类型,用于管理一组字节数据。下面的代码将16进制数转成QByteArray:

QByteArray hexToQByteArray(QString strHex)  
{  
    QByteArray byteArray;  
    char hexdata, lowhexdata;  
    int hexdatalen = 0;  
    int len = strHex.length();  
    byteArray.resize(len / 2);  
    char *pHex = byteArray.data();  
    for (int i = 0; i < len;)  
    {  
        hexdata = strHex[i].toLatin1();  
        if (hexdata == ' ')  
        {  
            i++;  
            continue;  
        }  
        i++;  
        if (i >= len)  
            break;  
        lowhexdata = strHex[i].toLatin1();  
        hexdatalen++;  
        *pHex = (char)(hexCharToValue(hexdata) * 16 + hexCharToValue(lowhexdata));  
        pHex++;  
        i++;  
    }  
    byteArray.resize(hexdatalen);  
    return byteArray;  
}  

该函数将16进制的字符串转成了QByteArray,可以在Qt开发中使用。

五、Hex转Eth

Ethereum中经常使用16进制码表示货币的数量以及智能合约代码,因此将16进制转成Eth十分必要。下面的代码是将16进制转成Eth:

const hexToEth = (hex) => {  
  return new BigNumber(hex).dividedBy(new BigNumber(10).pow(18)).toNumber();  
}  

这样就能将16进制转成Eth了。

Hex转RGB

2023-05-21
RGB转HEX的应用

2023-05-18
java方法整理笔记(java总结)

2022-11-08
详解hex函数及其用法

2023-05-20
CSS Hex Code Color Picker

2023-05-12
印象笔记记录java学习(Java成长笔记)

2022-11-12
CSS Hex Colors

2023-05-12
CSS中的Hex颜色码是什么?

一、Hex颜色码的介绍 在前端开发中,颜色的选取是非常重要的一部分。CSS中提供了多种选取颜色的方式,其中Hex颜色码是最广泛使用的一种。Hex颜色码是指采用16进制(Hexadecimal)表示颜色

2023-12-08
java笔记,大学java笔记

2022-11-28
java笔记,尚硅谷java笔记

2022-12-01
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)