[R] bizarre color space conversion problem
Sarah Goslee
@@r@h@go@|ee @end|ng |rom gm@||@com
Thu Jul 18 17:50:17 CEST 2019
I didn't include enough detail, despite the length of my original
email. The problem is in the conversion from RGB to Lab. I converted
it back to RGB because most of us are more familiar with that. Below
are the intermediate steps.
And to make it even more bizarre, it fails with eight or more colors,
but works correctly with seven or fewer.
Ivan's suggestion works fine; the Lab to RGB conversion is okay. I
tried to figure out how colorspaces() works, but couldn't get it to
agree with convertColors(), although they're both reversible using the
same function.
> white <- c(x = 0.953205712549377, 1, y = 1.08538438164692)
> grDevices::convertColor(c(1, 0, 0), from = "sRGB", to = "Lab")
L a b
[1,] 53.48418 80.01027 67.38407
>
> grDevices::colorspaces[['Lab']][['fromXYZ']](c(1, 0, 0), white)
L a b
0.000 439.086 0.000
#-#-#
Here's a more complete example.
red.in <- rep("red", 8)
red.rgb <- t(col2rgb(red.in, alpha = 0)/255)
red.rgb
red.lab <- convertColor(red.rgb, from = "sRGB", to = "Lab")
red.lab
convertColor(red.lab, from = "Lab", to = "sRGB")
#
> red.in <- rep("red", 8)
>
> red.rgb <- t(col2rgb(red.in, alpha = 0)/255)
> red.rgb
red green blue
[1,] 1 0 0
[2,] 1 0 0
[3,] 1 0 0
[4,] 1 0 0
[5,] 1 0 0
[6,] 1 0 0
[7,] 1 0 0
[8,] 1 0 0
>
> red.lab <- convertColor(red.rgb, from = "sRGB", to = "Lab")
> red.lab
L a b
[1,] 97.1495 -21.36677 94.42044
[2,] 97.1495 -21.36677 94.42044
[3,] 97.1495 -21.36677 94.42044
[4,] 97.1495 -21.36677 94.42044
[5,] 0.0000 0.00000 0.00000
[6,] 0.0000 0.00000 0.00000
[7,] 0.0000 0.00000 0.00000
[8,] 0.0000 0.00000 0.00000
>
> convertColor(red.lab, from = "Lab", to = "sRGB")
[,1] [,2] [,3]
[1,] 1 0.0000 1
[2,] 1 0.0000 1
[3,] 1 0.0000 1
[4,] 1 0.0000 1
[5,] 0 0.5472 0
[6,] 0 0.5472 0
[7,] 0 0.5472 0
[8,] 0 0.5472 0
#
red.in <- rep("red", 7)
red.rgb <- t(col2rgb(red.in, alpha = 0)/255)
red.rgb
red.lab <- convertColor(red.rgb, from = "sRGB", to = "Lab")
red.lab
convertColor(red.lab, from = "Lab", to = "sRGB")
> red.in <- rep("red", 7)
>
> red.rgb <- t(col2rgb(red.in, alpha = 0)/255)
> red.rgb
red green blue
[1,] 1 0 0
[2,] 1 0 0
[3,] 1 0 0
[4,] 1 0 0
[5,] 1 0 0
[6,] 1 0 0
[7,] 1 0 0
>
> red.lab <- convertColor(red.rgb, from = "sRGB", to = "Lab")
> red.lab
L a b
[1,] 53.48418 80.01027 67.38407
[2,] 53.48418 80.01027 67.38407
[3,] 53.48418 80.01027 67.38407
[4,] 53.48418 80.01027 67.38407
[5,] 53.48418 80.01027 67.38407
[6,] 53.48418 80.01027 67.38407
[7,] 53.48418 80.01027 67.38407
>
> convertColor(red.lab, from = "Lab", to = "sRGB")
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 1 0 0
[3,] 1 0 0
[4,] 1 0 0
[5,] 1 0 0
[6,] 1 0 0
[7,] 1 0 0
On Thu, Jul 18, 2019 at 11:02 AM Ivan Krylov <krylov.r00t using gmail.com> wrote:
>
> This is, indeed, bizzare!
>
> On Thu, 18 Jul 2019 10:35:57 -0400
> Sarah Goslee <sarah.goslee using gmail.com> wrote:
>
> > I've gotten as far as locating the problem in this line from
> > grDevices::convertColor()
> >
> > xyz <- from$toXYZ(color, from.ref.white)
>
> So if you manually feed the arguments:
>
> Lab <- structure(c(53.484183785248, 53.484183785248, 53.484183785248,
> 53.484183785248, 53.484183785248, 53.484183785248, 53.484183785248,
> 53.484183785248, 53.484183785248, 53.484183785248, 53.484183785248,
> 53.484183785248, 53.484183785248, 53.484183785248, 53.484183785248,
> 53.484183785248, 53.484183785248, 53.484183785248, 53.484183785248,
> 53.484183785248, 80.0102716478952, 80.0102716478952, 80.0102716478952,
> 80.0102716478952, 80.0102716478952, 80.0102716478952, 80.0102716478952,
> 80.0102716478952, 80.0102716478952, 80.0102716478952, 80.0102716478952,
> 80.0102716478952, 80.0102716478952, 80.0102716478952, 80.0102716478952,
> 80.0102716478952, 80.0102716478952, 80.0102716478952, 80.0102716478952,
> 80.0102716478952, 67.3840746825475, 67.3840746825475, 67.3840746825475,
> 67.3840746825475, 67.3840746825475, 67.3840746825475, 67.3840746825475,
> 67.3840746825475, 67.3840746825475, 67.3840746825475, 67.3840746825475,
> 67.3840746825475, 67.3840746825475, 67.3840746825475, 67.3840746825475,
> 67.3840746825475, 67.3840746825475, 67.3840746825475, 67.3840746825475,
> 67.3840746825475), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("L",
> "a", "b")))
> white <- c(x = 0.953205712549377, 1, y = 1.08538438164692)
>
> to the function:
>
> grDevices::colorspaces[['Lab']][['toXYZ']](Lab, white)
>
> Do you get the same result on all three computers?
>
> If you debugonce(grDevices::colorspaces[['Lab']][['toXYZ']]), then
> try to convertColor(red.lab, from = "Lab", to = "sRGB"), are the
> arguments Lab and white the same on all three computers?
>
> --
> Best regards,
> Ivan
More information about the R-help
mailing list