[R] bizarre color space conversion problem

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Jul 18 19:05:39 CEST 2019


On Thu, 18 Jul 2019 11:50:17 -0400
Sarah Goslee <sarah.goslee using gmail.com> wrote:

> The problem is in the conversion from RGB to Lab.

Hmm. Assuming defaults and skipping all checks, convertColor(red.rgb,
from = "sRGB", to = "Lab") amounts to the following:

red.rgb <- t(col2rgb(rep('red',8), alpha = 0)/255)
# let's hope this implementation detail wasn't changed in 3.6.1
white.point <- grDevices:::c2to3(grDevices:::white.points[,'D65'])
(red.xyz <- grDevices::colorspaces$sRGB$toXYZ(red.rgb, white.point))[1,]
# [1] 0.4168213 0.2149235 0.0195385
(red.lab <- grDevices::colorspaces$Lab$fromXYZ(red.xyz,white.point))[1,] #        L        a        b
# 53.48418 80.01027 67.38407
convertColor(red.rgb, from = "sRGB", to = "Lab")[1,]
#        L        a        b
# 53.48418 80.01027 67.38407

Does the manual way result in a different output? If so, do the
differences appear in red.xyz or red.lab?

grDevices::colorspaces$sRGB$toXYZ is pretty simple, just a matrix
product.  (I hope there are no differences in
as.list(environment(grDevices::colorspaces$sRGB$toXYZ))[c('dogamma','M')]
on different computers - that would be strange.)
grDevices::colorspaces$Lab$fromXYZ looks more complicated but is just a
bunch of vectorised element-wise algebra, so tracing the intermediate
results should also be possible.

> And to make it even more bizarre, it fails with eight or more colors,
> but works correctly with seven or fewer.

This is just a wild guess, but could /usr/lib64/R/lib/libRblas.so per
chance be a symlink to an OpenBLAS copy? Could any of the computers
exhibiting the bizarre behaviour be equipped with an AVX-512-capable
CPU?

-- 
Best regards,
Ivan



More information about the R-help mailing list