-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·320 lines (276 loc) · 10.8 KB
/
index.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Color Palette Generator Built With D3js">
<meta name="author" content="Hause Lin">
<title>Color Palette Generator</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.css" rel="stylesheet">
<link rel="icon" href="https://hauselin.github.io/colorpalettejs/favicon.png">
<!-- Custom fonts for this template -->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
<link href="vendor/simple-line-icons/css/simple-line-icons.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="css/landing-page.css" rel="stylesheet">
<!-- d3 for color palette -->
<script src="https://d3js.org/d3.v5.min.js"></script>
<!-- css style for page -->
<style>
body {
max-width: 800px;
margin: 0 auto;
}
a {
color: #21918c;
}
p {
line-height: 1.3rem;
margin-left: 30px;
margin-right: 30px;
}
label {
width: 50px;
margin-left: 10px;
margin-right: 20px;
}
input {
width: 50px;
margin-left: 5px;
margin-right: 5px;
}
button {
margin-left: 2px;
margin-right: 0px;
background-color: #ffffff;
border: 1px solid #b8b8b8;
border-radius: 5%;
}
#preview {
width: 90%;
height: 50px;
margin-top: 10px;
margin-left: 30px;
border-radius: 8px;
}
#levels {
margin-top: 10px;
margin-left: 30px;
margin-right: 30px;
}
div.level {
width: 100%;
height: 50px;
display: block;
margin-bottom: 0.2rem;
margin-right: 30px;
}
div.level div {
display: inline-block;
margin-left: 0px;
}
/* size of color square */
div.block {
height: 100%;
width: 50px;
margin-right: 10px;
border-radius: 8px;
}
</style>
</head>
<body>
<!-- Navigation -->
<br>
<nav class="navbar static-top">
<div class="container">
<a class="navbar-brand">
<img src="https://hauselin.github.io/colorpalettejs/favicon.png" alt="viridis" width="25" style="border-radius: 8px;"> 
<strong>Perceptually uniform colormaps</strong>
</a>
<a href="https://github.com/hauselin/colorpalettejs">
<i class="fab fa-github fa-2x fa-fw" style="color:#440154"></i>
</a>
</div>
</nav>
<!-- <h1>Color Palette Generator</h1> -->
<br>
<p>
Perceptually uniform colormaps are great in many ways. They were introduced in 2015 (see <a href="https://www.youtube.com/watch?v=xAoljeRJ3lU">talk at SciPy2015</a>) and are available in <a href="http://bids.github.io/colormap/">Python's matplotlib</a>, <a href="https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html">R's viridis package</a>, <a href="https://github.com/d3/d3-scale-chromatic">JavaScript's D3</a>, and others. This site uses D3js to generate colors and is a shameless adaptation of <a href="https://gist.github.com/adam-garcia/8d8eded299ec5e16752d04281236b151">Adam Garcia's gist</a>.
</p>
<p>
Select the color palette and number of categories you'd like to use. Click <strong>Copy</strong> to copy the hex values (e.g., #440154) or <strong>Copy with quotes</strong> to copy the hex values with quotes (e.g., "#440154").
</p>
<style>
.bmc-button img {
height: 34px !important;
width: 35px !important;
margin-bottom: 0px !important;
box-shadow: none !important;
border: none !important;
vertical-align: middle !important;
margin-left: 0px;
}
.bmc-button {
padding: 7px 8px 8px 5px !important;
line-height: 30px !important;
height: 50px !important;
text-decoration: none !important;
/* display: inline-flex !important; */
color: #ffffff !important;
background-color: #3b528b !important;
border-radius: 8px !important;
border: 1px solid transparent !important;
font-size: 18px !important;
letter-spacing: 0.6px !important;
box-shadow: 0px 1px 2px rgba(190, 190, 190, 0.5) !important;
-webkit-box-shadow: 0px 1px 2px 2px rgba(190, 190, 190, 0.5) !important;
/* margin: auto !important; */
font-family: 'Cookie', cursive !important;
-webkit-box-sizing: border-box !important;
box-sizing: border-box !important;
margin-left: 30px;
}
</style>
<link href="https://fonts.googleapis.com/css?family=Cookie" rel="stylesheet"><a class="bmc-button" target="_blank" href="https://www.buymeacoffee.com/hauselin">🍵<span style="margin-left:5px;font-size:24px !important;">Buy me tea</span></a>
<label for="palette">Palette: </label>
<select name="palette" id="palette">
<option value="v">viridis</option>
<option value="i">inferno</option>
<option value="m">magma</option>
<option value="p">plasma</option>
<option value="c">cividis</option>
<!-- <option value="t">turbo</option> -->
</select>
<label for="groups">Categories: </label>
<input type="number" id="groups" value="5" min="2" max="256">
<!-- copy hex values button -->
<button onclick="copy_hex_values(vals_hex, false)">Copy</button>
<button onclick="copy_hex_values(vals_hex, true)">Copy with quotes</button>
<script>
function copy_hex_values(x, quote) {
if (quote) {
var x = x.map(i => i.toString());
x = '"' +x.join('","') + '"';
}
console.log(x)
var inp = document.createElement('input');
document.body.appendChild(inp);
inp.value = x;
inp.select();
document.execCommand('copy', false);
inp.remove();
}
</script>
<div id="preview"></div>
<div id="levels"></div>
<script>
d3.select('input').on('change', function() {
generatePalette();
});
d3.select('input').on('keyup', function() {
generatePalette();
});
d3.select('select').on('change', function() {
generatePalette();
});
function generatePalette() {
var n = parseInt(d3.select("#groups").property("value"));
if (n > 256) {
n = 256;
d3.select("#groups").property("value", 256);
}
var p = d3.select("#palette").property("value");
// Interpolation Scale Proxy
var paletteProxy = function(_l) {
switch (p) {
case "v":
return d3.interpolateViridis(_l);
case "i":
return d3.interpolateInferno(_l);
case "m":
return d3.interpolateMagma(_l);
case "p":
return d3.interpolatePlasma(_l);
case "t":
return d3.interpolateTurbo(_l);
case "c":
return d3.interpolateCividis(_l);
default:
break;
}
};
// convert rgb(x,x,x) to hex
// https://stackoverflow.com/questions/13070054/convert-rgb-strings-to-hex-in-javascript
function parseColor(color) {
var arr = []; color.replace(/[\d+\.]+/g, function (v) { arr.push(parseFloat(v)); });
return {
hex: "#" + arr.slice(0, 3).map(toHex).join(""),
opacity: arr.length == 4 ? arr[3] : 1
};
}
function toHex(int) {
var hex = int.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
// Update the preview
var gradient = "linear-gradient(to right, " +
paletteProxy(0.0) + ", " +
paletteProxy(0.5) + ", " +
paletteProxy(1.0) + "" +
")";
d3.select("#preview").style("background", gradient);
// Update the value-levels
var data = [];
d3.range(0, 1 + 1/(n - 1), 1/(n - 1))
.slice(0, n)
// .reverse()
.map(function(l) {
var r, g, b, hex;
hex = paletteProxy(l);
if (hex.slice(0, 3) == 'rgb') {
hex = parseColor(hex).hex;
}
r = parseInt(hex.slice(1, 3), 16);
g = parseInt(hex.slice(3, 5), 16);
b = parseInt(hex.slice(5, 7), 16);
var obj = {
"background": hex,
"rgb": [r, g, b]
};
data.push(obj);
});
vals_hex = data.map(i => i.background);
vals_rgb = data.map(i => i.rgb);
d3.select("#levels")
.html("")
.selectAll("div")
.data(data).enter()
.append("div")
.classed("level", true)
.append("div")
.classed("block", true)
.style("background-color", function(d) {return d.background;})
.select(function() {return this.parentNode})
.append("div")
.html(function(d){
var rgb = "rgb(" + d.rgb.join(", ") + ")";
var hex = d.background;
return hex + "<br>" + rgb}
)
.style("position", "absolute")
.style("padding-top", "0.3rem")
.style("font-size", "90%")
.style("padding-left", "1rem");
}
</script>
<!-- run script -->
<script type="text/javascript">
generatePalette();
</script>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>