File: colors.R

package info (click to toggle)
r-cran-plot3d 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,588 kB
  • sloc: makefile: 2
file content (92 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
setalpha <- function(Col, alpha) {

  if (is.null(Col))
    return(Col)

  ii <- which (! is.na(Col))
  if (length(ii) > 0) {   
    pcol <- alpha.col (Col, alpha)
    Col[ii] <- pcol[ii] 
  }    
  Col
}


## =============================================================================
## Suitable colors
## =============================================================================

jet.col <- function (n = 100, alpha = 1) {

 # red-green-blue colors on scale of 0 to 1
  red    <- c(0,   0,   0,   255, 255, 128)
  green  <- c(0,   0,   255, 255, 0,   0  )
  blue   <- c(143, 255, 255, 0,   0,   0  )

  x.from <- c(0.0, seq(0.125, 1, by = 0.25), 1)  # scale from 0-1
  x.to   <- seq (0, 1, length.out = n)

  expand <- function(col)
    approx(x = x.from, y = col, xout = x.to)$y

  return (rgb(expand(red), expand(green), expand(blue), 
               maxColorValue = 255, alpha = alpha*255))
}

jet2.col <- function (n = 100, alpha = 1) {

  red    <- c(0,   0,   255, 255, 210)
  green  <- c(78,  255, 255, 0,   0  )
  blue   <- c(255, 255, 0,   0,   0  )

  x.from <- seq (0, 1, length.out = 5)  
  x.to   <- seq (0, 1, length.out = n)

  expand <- function(col)
    approx(x = x.from, y = col, xout = x.to)$y

  return (rgb(expand(red), expand(green), expand(blue), 
               maxColorValue = 255, alpha = alpha*255))
}

alpha.col <- function (col = "grey", alpha = 0.5) {

  RGBini <- col2rgb(col)

  return( rgb(t(RGBini), 
          maxColorValue = 255, alpha = alpha*255))
}

ramp.col <- function (col = c("grey", "black"), 
                      n = 100, alpha = 1) {

  RGBini <- col2rgb(col)

  x.from <- seq(0, 1, length.out = length(col)) 
  x.to   <- seq(0, 1, length.out = n)

  expand <- function(col)
    approx(x = x.from, y = col, xout = x.to)$y

  return (rgb(expand(RGBini["red",]), 
                 expand(RGBini["green",]), 
                 expand(RGBini["blue",]), 
                 maxColorValue = 255, alpha = alpha*255))
}

gg.col <-  function (n = 100, alpha = 1) {
 
  ramp.col(col = c("#0072B2", "#56B4E9", "#009E73", 
        "#CC79A7", "#D55E00", "#000000"), n = n, alpha = alpha)
} 

gg2.col <-  function (n = 100, alpha = 1) {
 
  ramp.col(col = c("#0072B2", "#56B4E9", "#009E73", "#F0E442", "#CC79A7", 
               "#D55E00", "#000000"), n = n, alpha = alpha)
} 

MeanColors <- function(col) {           
  rgb(t(rowMeans(col2rgb(col))), maxColorValue = 255)
}