# TODO: Add comment
# 
# Author: E.Korsching  2024
###############################################################################


### create color groups for plotting


color.groups <- function(xc, ...){
	# create color groups according to input groups for e.g. clustering, tsne
	#  according to a given order by the ... elements
	# xc: colors per group, corresponds to elements(...)
	# ... : single elements, number_xc = number_elements(...)
	colargs <- list(...)
	arglen <- length(colargs)
	out <- NULL
	for(i in 1:arglen){
		cai <- length(colargs[[i]])
		out <- c(out, rep(xc[i],cai))
	}
	return(out)
}

# color.groups(xc=c("red","blue","green"), c("a1","a2","a3"), c("b1"), c("c1","c2") )


color.corresponding <- function(x, xc, ...){
	# create a color vector corresponding to an input vector x
	# x: column names
	# xc: colors per group, corresponds to elements(...)
	# ... : single elements: column name groups for a certain color
	#   number_xc = number_elements(...)
	colargs <- list(...)
	arglen <- length(colargs)
	xlen <- length(x)
	out <- vector("character",xlen)
	k <- 1
	for(i in 1:arglen){
		logi <- x %in% colargs[[i]]
		out[logi] <- xc[i]
	}
	return(out)
}

# color.corresponding(x=c("a1","b1","c1","a2","a3","c2"), xc=c("red","blue","green"), c("a1","a2","a3"), c("b1"), c("c1","c2") )


color.by.id <- function(xc, idvec){
	# create color groups according to input groups for e.g. clustering, tsne
	#  according to a given order by the idvec elements
	# xc: colors per group, corresponds to unique idvec elements
	# idvec: vector of char identifier
	xclen <- length(xc)
	ilen <- length(idvec)
	uid <- unique(idvec)
	uidlen <- length(uid)
	if(xclen!=uidlen){ stop("number of xc elements = unique elements of idvec") }
	out <- NULL
	for(i in 1:ilen){
		cai <- which(uid %in% idvec[i], arr.ind=T)
		if(length(cai)!=1){ stop("ambiguous ids") }
		out <- c(out, xc[cai])
	}
	return(out)
}

# color.by.id(xc=c("red","blue","green"), c("a1","a3","a2","a2","a2","a1") )		# unique(c("a1","a3","a2","a2","a2","a1"))  !order!



