首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用colorspace包进行错误的颜色转换

使用colorspace包进行错误的颜色转换
EN

Stack Overflow用户
提问于 2012-10-11 15:43:06
回答 1查看 340关注 0票数 2

请看下面的示例:

代码语言:javascript
复制
> library(colorspace)
> # convert color with coordinates (0.2,0.3,0.4) in the RGB space into a character string:
> ( x <- hex(RGB(0.2,0.3,0.4)) )
[1] "#7C95AA"
> # reverse conversion:
> hex2RGB(x)  ## not the original coordinates !
             R         G         B
[1,] 0.4862745 0.5843137 0.6666667
> # now do the first conversion with the rgb() function from grDevices package:
> library(grDevices)
> ( y <- rgb(0.2,0.3,0.4) )
[1] "#334C66"
> # reverse conversion:
> hex2RGB(y)  ## close to the original coordinates
       R         G   B
[1,] 0.2 0.2980392 0.4

似乎使用colorspace包中的hex()函数将RGB颜色空间中的三个坐标转换为字符串是错误的。是bug,还是我错误地使用了这个包?

EN

回答 1

Stack Overflow用户

发布于 2018-10-13 19:20:19

转换中的差异是由于使用线性化的RGB颜色模型而不是伽马校正的sRGB颜色模型造成的。后者被base R和大多数其他为图形生成颜色的软件所采用。前者与转换到其他模型的中间步骤相关,如CIE XYZ,也在colorspace包中提供,如下所示。

如果您使用的是sRGB函数,您的示例将按预期工作:

代码语言:javascript
复制
library("colorspace")
( x <- hex(sRGB(0.2, 0.3, 0.4)) )
## [1] "#334D66"
hex2RGB(x)
##        R         G   B
## [1,] 0.2 0.3019608 0.4

G坐标上差异的原因是0.3 * 255不是整数。(十六进制颜色编码整数0到255。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12834593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档