【问题标题】:wrong color conversion with colorspace package使用颜色空间包进行错误的颜色转换
【发布时间】:2012-10-11 07:43:05
【问题描述】:

看下面的例子:

> 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还是我用错了包?

【问题讨论】:

  • 尝试伽玛校正
  • 谢谢。命令 hex2RGB(x, gamma=TRUE) 产生接近原始坐标的坐标。
  • StéphaneLaurent 或@baptiste,将其发布为答案?

标签: r colors


【解决方案1】:

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

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

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。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2018-05-14
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    相关资源
    最近更新 更多