绘制一个彩色十六进制编码矩阵

时间:2022-09-18 00:10:18

I'm trying to plot a box which fade between three colours. It should be red on top left, blue on top right and white at the bottom.

我想画一个在三种颜色中褪色的盒子。左上方应该是红色,右上方应该是蓝色,下方应该是白色。

Here's what I've tried:

这是我试过:

##Change from red to blue as move left to right, fade to white as move from top to bottom

data<-matrix(1:(255*255),ncol=255,nrow=255)

rb<-colorRampPalette(c("red","blue"))(255)

colsMat<-matrix(nrow=255,ncol=255)

for(col in rb){
  idx<-which(rb==col)
  colsMat[idx,]<-colorRampPalette(c(col,"blue"))(255)
}

colsVec<-as.vector(colsMat)

image(data,col=colsVec,xaxt="n",yaxt="n")

I can get it to go from red to blue (but need to add the white) like this:

我可以让它从红色变为蓝色(但需要添加白色)如下:

data<-matrix(1:255,ncol=255,nrow=255)
image(data,col=rb,xaxt="n",yaxt="n")

1 个解决方案

#1


3  

rb<-colorRampPalette(c("red","blue"))(255)
trans<-sapply(seq(from=0,to=1,length.out=255),function(op) rgb(1,1,1,op))

image(matrix(1:255,ncol=255,nrow=255),col=rb,xaxt="n",yaxt="n")
par(new=T)
image(t(matrix(255:1,ncol=255,nrow=255)),col=trans,xaxt="n",yaxt="n")

#1


3  

rb<-colorRampPalette(c("red","blue"))(255)
trans<-sapply(seq(from=0,to=1,length.out=255),function(op) rgb(1,1,1,op))

image(matrix(1:255,ncol=255,nrow=255),col=rb,xaxt="n",yaxt="n")
par(new=T)
image(t(matrix(255:1,ncol=255,nrow=255)),col=trans,xaxt="n",yaxt="n")