Slowpoke - 1 year ago 83

R Question

I am working with colors in both RGB and HSV formats

`[0-255]`

`[0-1]`

Suppose I have a set of RGB color values:

`x_order<-structure(list(X1 = c(247, 202, 201), X2 = c(247, 120, 107),`

X3 = c(145, 168, 208), X4 = c(3, 79, 132), X5 = c(251, 227,

55), X6 = c(152, 221, 222), X7 = c(152, 150, 164), X8 = c(220,

68, 58), X9 = c(177, 143, 106), X10 = c(113, 204, 81)), .Names = c("X1",

"X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10"), row.names = c(NA,

-3L), class = "data.frame")

so

`> x_order`

X1 X2 X3 X4 X5 X6 X7 X8 X9 X10

1 247 247 145 3 251 152 152 220 177 113

2 202 120 168 79 227 221 150 68 143 204

3 201 107 208 132 55 222 164 58 106 81

So I check the conversion:

`library(grDevices)`

plot(1, type="n", xlab="", ylab="", xlim=c(0, 25), ylim=c(0, 1))

for (i in 1:10){

points(i*2,0.8,pch=19,col=rgb(x_order[1,i],x_order[2,i],

x_order[3,i],maxColorValue = 255), cex=5)

points(i*2,0.4,pch=19,col=hsv(rgb2hsv(x_order[,i],

maxColorValue = 255)), cex=5)

}

The result is:

How can I convert the RGB values to HSV properly and display the output?

One more thing is that I'm sure in the correct work of

`rgb2hsv`

UPD:

`> hsv(rgb2hsv(x_order[,10],maxColorValue = 255))`

[1] "#42FF00" "#0061FF" "#CC00FF"

> rgb(x_order[1,10],x_order[2,10],x_order[3,10],maxColorValue = 255)

[1] "#71CC51"

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You are passing in a vector of values to `hsv`

, which it doesn't accept in the way you are expecting. You can pass in arguments `h`

, `s`

, and `v`

separately and get the proper conversion:

```
hsvColors <- rgb2hsv(x_order)
plot(1, type="n", xlab="", ylab="", xlim=c(0, 25), ylim=c(0, 1))
for (i in 1:10){
points(i*2, 0.8, pch=19,
col=rgb(x_order[1,i],x_order[2,i], x_order[3,i], maxColorValue = 255),
cex=5)
points(i*2, 0.4, pch=19,
col = hsv(hsvColors[1, i], hsvColors[2, i], hsvColors[3, i]),
cex=5)
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**