How to convert color name to the corresponding hexadecimal representation?

前端 未结 5 1384
面向向阳花
面向向阳花 2020-12-19 13:56

For example:

blue 

converts to:

#0000FF

I wrote it as:

Color color = Color.FromName(\"blue\

5条回答
  •  甜味超标
    2020-12-19 14:35

    You can use gplots package:

    library(gplots)
    col2hex("blue")
    # [1] "#0000FF"
    

    https://cran.r-project.org/web/packages/gplots/index.html

    Inside gplots package the code for col2hex function is:

    col2hex <- function(cname)
    {
        colMat <- col2rgb(cname)
        rgb(
            red=colMat[1,]/255,
            green=colMat[2,]/255,
            blue=colMat[3,]/255
        )
    }
    

提交回复
热议问题