-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
354 lines (320 loc) · 11.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html>
<head>
<title>Material color generator</title>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
input = document.getElementById("color");
input.oninput=calc;
calc();
// out link tracking
for(var i=0; i<document.links.length; i++) {
document.links[i].onclick = new Function("trackOutboundLink('"+document.links[i].href+"'); return false");
}
});
// spoiler: not yet done ;-)
if(false && window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'https://api.github.com/repos/rekire/MaterialColorGenerator/stargazers', true);
httpRequest.send(null);
httpRequest.onreadystatechange = function(){
if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
var users = JSON.parse(httpRequest.responseText);
for(var i=0; i<users.length; i++) {
// Math.floor(40*window.devicePixelRatio)
console.log(users[i].login, users[i].avatar_url+"&s=40");
}
}
};
}
users = null;
function loadImages() {
if(users === null) {
users = [new Image(), new Image(), new Image(), new Image()];
users[0].onload=calc;
users.size=0;
}
var targetSize = Math.floor(40*window.devicePixelRatio);
if(users.size < targetSize) {
users[0].src="https://avatars.githubusercontent.com/u/4619618?v=1&s=" + targetSize;
users[1].src="https://avatars.githubusercontent.com/u/0?v=1&s=" + targetSize;
//users[2].src="https://avatars.githubusercontent.com/u/0?v=1&s=40";
//users[3].src="https://avatars.githubusercontent.com/u/0?v=1&s=40";
users.size = targetSize;
}
}
// source: http://stackoverflow.com/a/19270021/995926
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len;
}
return result;
}
function calc() {
var color = document.getElementById("color").value;
if(color.length == 3) {
color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
}
var output = document.getElementById("output");
if(color.length == 6) {
var colorInt = parseInt(color, 16);
var r = (colorInt & 0xff0000) >> 16;
var g = (colorInt & 0x00ff00) >> 8;
var b = (colorInt & 0x0000ff);
var hsv = rgbToHsv(r, g, b);
var rgb = toHex(hsvToRgb(hsv[0], hsv[1], hsv[2]));
output.innerHTML ='<div style="background-color:#' + rgb + '">App color <span>#' + rgb + '</span></div>';
rgb = toHex(hsvToRgb(hsv[0], hsv[1], 1));
output.innerHTML+='<div style="background-color:#' + rgb + '">Accent <span>#' + rgb + '</span></div>';
rgb = toHex(hsvToRgb(hsv[0], hsv[1], 0.65));
output.innerHTML+='<div style="background-color:#' + rgb + '">Lighter UI element <span>#' + rgb + '</span></div>';
rgb = toHex(hsvToRgb(hsv[0], hsv[1], 0.4));
output.innerHTML+='<div style="background-color:#' + rgb + '; color:#fff">UI element <span>#' + rgb + '</span></div>';
rgb = toHex(hsvToRgb(hsv[0], hsv[1], 0.3));
output.innerHTML+='<div style="background-color:#' + rgb + '; color:#fff">Lighter background <span>#' + rgb + '</span></div>';
rgb = toHex(hsvToRgb(hsv[0], hsv[1], 0.15));
output.innerHTML+='<div style="background-color:#' + rgb + '; color:#fff">Dark background <span>#' + rgb + '</span></div>';
draw(hsv);
}
}
lastZoom = -1;
window.onresize=calc;
function draw(hsv) {
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
// source: http://html5-mobile.de/blog/retina-display-html-canvas-optimieren
if(window.devicePixelRatio && window.devicePixelRatio !== lastZoom) {
lastZoom = window.devicePixelRatio;
var devicePixelRatio = window.devicePixelRatio;
var canvasWidth = 700;
var canvasHeight = 350;
canvas.style.width = canvasWidth+"px";
canvas.style.height = canvasHeight+"px";
canvas.setAttribute("width", Math.ceil(canvasWidth*devicePixelRatio));
canvas.setAttribute("height", Math.ceil(canvasHeight*devicePixelRatio));
ctx.transform(devicePixelRatio,0,0,devicePixelRatio,0,0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
loadImages();
}
ctx.imageSmoothingEnabled = true;
// first watch
ctx.save();
ctx.beginPath();
ctx.arc(110,180,100,0,Math.PI*2,true);
ctx.clip();
ctx.beginPath();
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.3));
ctx.fillRect(14, 80, 192, 180);
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.4));
ctx.fillRect(14, 240, 192, 36);
ctx.restore();
// second watch
ctx.save();
ctx.beginPath();
ctx.arc(350,180,100,0,Math.PI*2,true);
ctx.clip();
ctx.beginPath();
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.65));
ctx.fillRect(254, 80, 192, 101);
ctx.restore();
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.4));
ctx.fillRect(254, 180, 192, 160);
// third watch
ctx.save();
ctx.beginPath();
ctx.arc(590,180,100,0,Math.PI*2,true);
ctx.clip();
ctx.beginPath();
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.4));
ctx.fillRect(494, 140, 192, 100);
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 1));
ctx.fillRect(494, 240, 192, 36);
ctx.restore();
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 0.15));
ctx.fillRect(494, 10, 192, 130);
// draw content of first watch
ctx.save();
ctx.beginPath();
ctx.arc(110,130,20,0,Math.PI*2,true);
ctx.clip();
ctx.drawImage(users[0],90,110,40,40)
ctx.restore();
ctx.font = "bold 10pt Roboto";
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.fillText("rekire", 110, 170);
ctx.font = "10pt Roboto";
ctx.fillText("This library is cool.", 110, 190);
ctx.fillText("I stared it already,", 110, 205);
ctx.fillText("you should do it too.", 110, 220);
// watch chromes
ctx.beginPath();
ctx.lineWidth=10;
ctx.strokeStyle='#555';
ctx.arc(110,180,100,0,Math.PI*2,true);
ctx.stroke();
ctx.beginPath();
ctx.arc(350,180,100,0,Math.PI*2,true);
ctx.stroke();
ctx.beginPath();
ctx.arc(590,180,100,0,Math.PI*2,true);
ctx.stroke();
ctx.beginPath();
ctx.lineWidth=0;
ctx.fillStyle = '#' + toHex(hsvToRgb(hsv[0], hsv[1], 1));
ctx.arc(300,140,20,0,Math.PI*2,true);
ctx.fill();
}
}
function toHex(rgb) {
return format((rgb[0] << 16) | (rgb[1] << 8) | rgb[2]);
}
function format(d) {
var hex = Number(d).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return hex;
}
// https://github.com/mjackson/mjijackson.github.com/blob/master/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript.html
// modified to accept h value in the range 0-359
/**
* Converts an RGB color value to HSV. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and v in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSV representation
*/
function rgbToHsv(r, g, b){
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if(max == min){
h = 0; // achromatic
}else{
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
h *= 360; // mod
return [h, s, v];
}
/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function hsvToRgb(h, s, v){
var r, g, b;
h /= 360.0; // mod
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
// tracking
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-72964219-2', 'auto');
ga('send', 'pageview');
function trackOutboundLink(url) {
console.log("click on " + url);
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = url;}
});
}
</script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<style type="text/css">
body {
font-family: 'Roboto', sans-serif;
}
h1 {
margin-top: 0;
}
#output div {
height: 60px;
line-height: 60px;
max-width: 80%;
padding: 0 16px;
vertical-align: middle;
}
#output {
float:left;
width: 275px;
}
#output div span {
float: right;
}
canvas {
clear:left;
min-width:200px;
max-width:100%;
}
</style>
</head>
<body>
<h1>Material color generator
<a aria-label="Star rekire/MaterialColorGenerator on GitHub"
data-count-aria-label="# stargazers on GitHub"
data-count-api="/repos/rekire/MaterialColorGenerator#stargazers_count"
data-count-href="/rekire/MaterialColorGenerator/stargazers" data-style="mega"
data-icon="octicon-star" href="https://github.com/rekire/MaterialColorGenerator"
class="github-button">Star</a></h1>
<p>Please enter your primary color: <input type="text" id="color" value="9c27b0" maxlength="6" size="6"/></p>
<div id="output"></div>
<canvas id="canvas" width="700" height="350" style="background-color:#efefef"></canvas>
<h2>About</h2>
<p>This is a small tool which should you help to select the right colors for your project. If you
like this tool please star it.</p>
<p>You can specify your main color and you will get the matching colors for UI Elements as described
in the <a href="https://youtu.be/LtD7eJp2ILo?t=6m1s">Building Apps with Material Design</a> talk
about Wear 2.0 on the Google I/O 2016.</p>
<p>Basically the colors are calculated based on the <a
href="https://en.wikipedia.org/wiki/HSL_and_HSV">HSB or HSV</a> color space. Here is a
screenshot taken from the Video above:</p>
<img src="watch.jpg" alt="Example usage of the colors on Android Wear 2.0" style="max-width:100%;"/>
<h2>Used third party libs</h2>
<p>A light modified <a
href="https://github.com/mjackson/mjijackson.github.com/blob/master/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript.html">color
convertation</a> implementation from <a href="https://github.com/mjackson">mjackson</a>.</p>
<h2>License</h2>
<p>This code is licensed under the <a href="http://www.rekisoft.eu/licenses/rkspl.html">Rekisoft Public License</a>.<br/>
There is no need for contribution for using the generated colors.</p>
</body>
</html>