-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalettes.R
More file actions
156 lines (141 loc) · 5.15 KB
/
palettes.R
File metadata and controls
156 lines (141 loc) · 5.15 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
## Created 10 / 01 / 2014
## Isabel Fenton
##
## Contains a range of methods for plotting colours
## a lot of this is based on colours.R
##
## functions:
## percent.col - fill a certain percentage with a given colour
## heat.sat - palette
## heat.v - palette
## log.heat - palette
## rev.log.heat - palette
## water.colors - palette
## rwb - palette
## rwbt - palette
## rainbow2 - reversed rainbow palette from purple to red
## gray.palette - set grey as a palette so it takes a single number
## matlab.like palette is in library(colorRamps)
library(colorRamps)
## basic colors for colorblind ---------------------------------------------
coloring <- c("black", "red", "blue")
## palette of black points
black.pal <- function(x) rep("black", x)
## get hex
GetColorHex <- function(color) {
c <- col2rgb(color)
sprintf("#%02X%02X%02X", c[1],c[2],c[3], c[1], c[2], c[3])
}
## percent.col; fill a certain percentage with a given colour -------------
## create a function that fills a certain percentage of levels with a given color.
## created for Andy to colour gyres
percent.col <- function(n.levels, main.col = "white", back.col = "grey", cutoff = 0.3) {
cut <- round(n.levels * cutoff, 0)
c(rep(main.col, cut), rep(back.col, n.levels - cut - 1))
}
## heat saturation colour --------------------------------------------------
heat.sat <- function (n, h = 1/6, s = s, v = 1, alpha = 1) {
if ((n <- as.integer(n[1L])) > 0) {
n
if (n > 0) hsv(h, s = seq.int(from = 1 / (2 * n), to = 1 - 1 / (2 * n), length.out = n), v, alpha)
}
else character()
}
## heat v colour -----------------------------------------------------------
heat.v <- function (n, h = 1/6, s = 1, v = v, alpha = 1) {
if ((n <- as.integer(n[1L])) > 0) {
n
if (n > 0) hsv(h, s, v = seq.int(from = 1 - 1 / (2 * n), to = 1 / (2 * n), length.out = n), alpha)
}
else character()
}
## log heat colour ---------------------------------------------------------
log.heat<- function (n, alpha = 1) {
if (n < 2) {
n <- as.integer (n * 1000 + 0.5)
}
if ((n <- as.integer(n[1L])) > 0) {
j <- n %/% 4
i <- n - j
q <- round(log(seq(10, 1, length.out = i), 10) / 6, 3)
c(heat.sat(j, alpha = alpha), hsv(q, 1, v = 1, alpha = alpha))
}
else character()
}
## reverse log heat colour -------------------------------------------------
rev.log.heat <- function (n, alpha = 1) {
if (n < 2) {
n <- as.integer (n * 1000 + 0.5)
}
if ((n <- as.integer(n[1L])) > 0) {
q <- round((1 - log(seq(1, 10, length.out = n), 10)) / 6, 4)
hsv(q, 1, v = 1, alpha = alpha)
}
else character()
}
## water colours -----------------------------------------------------------
water.colors <- function(n, alpha = 1) {
if ((n <- as.integer(n[1L])) > 0) {
j <- n %/% 4
i <- n - j * 2
q <- round(seq(0.5, 0.7, length.out = i), 4)
c(heat.sat(j, h = 0.5, alpha = alpha), hsv(q, 1, v = 1, alpha = alpha), heat.v(j, h = 0.7))
}
else character()
}
# rwb ---------------------------------------------------------------------
rwb <- function (n, alpha = 1) {
if ((n <- as.integer(n[1L])) > 0) {
# if an even number
len <- n %/% 2
if (n %% 2 == 0) {
# reverse sequence from blue to white
col <- hsv(2/3, s = rev(seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)), 1, alpha)
# sequence from white to red
col <- c(col, hsv(1, s = seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len), 1, alpha))
} else {
# reverse sequence from blue to white
col <- hsv(2/3, s = rev(seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)), 1, alpha)
# white in the middle
col <- c(col, hsv(0, 0, 1))
# sequence from white to red
col <- c(col, hsv(1, s = seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len), 1, alpha))
}
}
else character()
}
# rwbt ---------------------------------------------------------------------
rwbt <- function (n) {
if ((n <- as.integer(n[1L])) > 0) {
# if an even number
len <- n %/% 2
if (n %% 2 == 0) {
# reverse sequence from blue to white
col <- hsv(2/3, 1, 1, rev(seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)))
# sequence from white to red
col <- c(col, hsv(1, 1, 1, seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)))
} else {
# reverse sequence from blue to white
col <- hsv(2/3, 1, 1, rev(seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)))
# white in the middle
col <- c(col, hsv(0, 1, 1, 0))
# sequence from white to red
col <- c(col, hsv(1, 1, 1, seq.int(from = 1/(2 * len), to = 1 - 1/(2 * len), length.out = len)))
}
}
else character()
}
# rainbow 2 ----------------------------------------------------------------
rainbow2 <- function (n, s = 1, v = 1, alpha = 1) {
if ((n <- as.integer(n[1L])) > 0) {
hsv(h = seq.int(3/4, 0, length.out = n) %% 1, s, v, alpha)
}
else character()
}
# gray.palette ------------------------------------------------------------
gray.palette <- function(n, start = 1, end = 0) {
if ((n <- as.integer(n[1L])) > 0) {
gray(seq(start, end, length.out = n))
}
else character()
}