.packageName <- "dichromat"

colorRampPalette<-function(palette, bias=1,method=c("spline","linear")){
	
	if (bias<=0) stop("bias must be positive")
	coord<-as.data.frame(t(col2rgb(palette))/255)
	x<-seq(0,1,length=length(palette))
	if(match.arg(method)=="spline"){
	  r<-splinefun(x,coord$red)
	  g<-splinefun(x,coord$green)
	  b<-splinefun(x,coord$blue)
	}else{
	  r<-approxfun(x,coord$red)
	  g<-approxfun(x,coord$green)
	  b<-approxfun(x,coord$blue)
	}

       function(n){
	   x<-seq(0,1,length=n)^bias
	   rgb(pmin(1,pmax(0,r(x))),pmin(1,pmax(0,g(x))),pmin(1,pmax(0,b(x))))
	}

}

colorRamp<-function(palette, bias=1,method=c("spline","linear")){
	
	coord<-as.data.frame(t(col2rgb(palette))/255)
	x<-seq(0,1,length=length(palette))
	if(match.arg(method)=="spline"){
	  r<-splinefun(x,coord$red)
	  g<-splinefun(x,coord$green)
	  b<-splinefun(x,coord$blue)
	}else{
	  r<-approxfun(x,coord$red)
	  g<-approxfun(x,coord$green)
	  b<-approxfun(x,coord$blue)
	}

        function(x,min=0,max=1){
	   if(any(x<min | x>max)) 
		stop("out of range")
	   z<-((x-min)/(max-min))^bias
	   rgb(pmin(1,pmax(0,r(z))),pmin(1,pmax(0,g(z))),pmin(1,pmax(0,b(z))))
        }

}

dichromat<- function(colours,type=c("deutan","protan")){
    data(dichromat, envir=environment())
    require("stats")
    colours<-col2rgb(colours)
    colours<-t(colours)
    colnames(colours)<-c("r","g","b")
    type<-match.arg(type)
    
    if(type=="deutan"){
        nred<-predict(redd,newdata=colours)
        ngreen<-predict(greend,newdata=colours)
        nblue<-predict(blued,newdata=colours)
    } else if (type=="protan"){
        nred<-predict(redp,newdata=colours)
        ngreen<-predict(greenp,newdata=colours)
        nblue<-predict(bluep,newdata=colours)
    }
    
    nred<-pmax(0,pmin(1,nred/255))
    ngreen<-pmax(0,pmin(1,ngreen/255))
    nblue<-pmax(0,pmin(1,nblue/255))
        rgb(nred,ngreen,nblue)
    
}
