# TODO: Add comment
# 
# Author: E.Korsching 30.11.2011/2024
###############################################################################



color.named <- function(numText=NULL, index=NULL, nc=6, textCol="blue", cex=0.5){
	# survey of 'named' R colors
	# construct a matrix containing all the available 'named' colors or
	#  give index numbers and only plot that colors
	# numText:1: print the index of the color in the 'colors' vector in the color field
	#         2: print the color name,  NULL: no label
	# nc: vary the width of the matrix
	# textCol: color of numbers, cex: character height
	
	#ini
	if(is.null(index)){
		n.vec <- colors()
		c.len <- length(n.vec)
		index <- 1:c.len
	}else{
		n.vec <- colors()[index]
		c.len <- length(n.vec)
	}
	nr <- ceiling(c.len/nc)
	
	#work
	plot(0, 0, type="n", axes=F, xlim=c(1,(nc+1)), ylim=c(1,nr), xlab="", ylab="")
	for(i in 1:nr){
		for(j in 1:nc){
			polygon(
					x=c(j,j+1,j+1,j),
					y=c(nr-i,nr-i,nr-i+1,nr-i+1),
					col=n.vec[(i-1)*nc+j],
					density=-1,
					border="black"
					)
			if(numText==1){ text(x=j+0.5, y=nr-i+0.5, labels=as.character(index[(i-1)*nc+j]), col=textCol, adj=0.5, cex=cex) }
			if(numText==2){ text(x=j+0.5, y=nr-i+0.5, labels=n.vec[(i-1)*nc+j], col=textCol, adj=0.5, cex=cex) }
			if((i-1)*nc+j >= c.len){ break }
		}
	}
}

#color.named(numText=1, nc=6)
#color.named(numText=2, nc=6)

#pdf(file="Rcolors0.pdf",width=7, height=11)	# also without pdf()
#a <- par()$mar
#par(mar=c(0.5,0.5,0.5,0.5))
#color.named(numText=1, nc=6)
#color.named(numText=2, nc=6)
#par(mar=a)
#dev.off()



