-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.html
191 lines (144 loc) · 5.11 KB
/
test.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
<!DOCTYPE html>
<style>
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 600;
src: local('Open Sans Semibold'), local('OpenSans-Semibold'), url(opensans.woff) format('woff');
}
html, body {
margin: 0;
width: 100%;
height: 100%;
font-family: 'Open Sans';
font-weight: 600;
}
.colors {
width: 100%;
}
#logo {
margin: 8px;
max-width: 420px;
max-height: 320px;
}
#colors-bottom {
position: fixed;
height: 40px;
bottom: 0px;
}
#links a {
display: inline-block;
padding: 3px;
border: 1px solid #888;
border-radius: 4px;
color: #444;
background-color: #eee;
text-decoration: none;
width: 24px;
margin-top: 20px;
margin-bottom: 16px;
}
#links a:hover {
background-color: #ddd;
}
</style>
<div class="colors" id="colors-top"></div>
<div id="dropZone" style="display: inline-block;">
<img src="logos/a.jpg" id="logo">
<canvas id="canvas" style="display:none"></canvas>
<div style="text-align: center"><input type="file" id="file-chooser"></div>
</div>
<div id="text" style="float: right; margin-right: 32px; width: 50%;">
<h2>Lorem Ipsum</h2>
<br>
Lorem ipsum dolor sit amet, nam et assum omnium quaestio, ius dolor insolens cu, quis ancillae ei nam. Pro in tibique epicuri omittantur. Malis iusto ei duo. Pro assum aliquip inimicus in. Cu nec summo deleniti argumentum, quo te novum alterum legendos, in deseruisse philosophia sit. Ex cum maluisset argumentum signiferumque, te sit odio dolorem cotidieque, vide eius aliquando no quo.
Vel agam eruditi dolorum at. Ut vis modus audire, debet utroque ius an. Eum equidem imperdiet vituperata ea, urbanitas tincidunt cu eos. Dolores disputationi ut eam. Graece accusam sea eu, ei brute tollit contentiones nec, ea elitr partem usu.
</div>
<br style="clear: both;">
<div style="text-align: center" id="links"></div>
<div class="colors" id="colors-bottom"></div>
<script src="js/jquery.js"></script>
<script src="js/KMeans.js"></script>
<script src="js/Octree.js"></script>
<script src="js/ColorAnalyser.js"></script>
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
var imgSrc = getURLParameter('logo');
if (imgSrc && imgSrc != 'null') {
$('#logo').attr('src', 'logos/' + imgSrc);
}
var alphabet = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i != 26; i++) {
var letter = alphabet.charAt(i);
var $link = $('<a href="?logo=' + letter + '.jpg">' + letter + '</a>');
$('#links').append($link).append(' ');
}
var analyseImage = function(img) {
if (!img) {
img = $('#logo')[0];
} else {
$('#logo').attr('src', $(img).attr('src'));
}
var canvas = $('#canvas')[0];
var analyser = new ColorAnalyser(img, canvas);
var bgColor = analyser.detectBackground();
$('body').css('background-color', 'rgb(' + bgColor[0] + ', ' + bgColor[1] + ', ' + bgColor[2] + ')');
var i;
var palette = analyser.analyseImage(6, bgColor);
//var palette = analyser.getThresholdedPalette(0.6, 128, bgColor, 32);
var colors = palette[0];
var numPixels = palette[1];
var textColor = analyser.chooseTextColor(bgColor, colors);
var colorString = 'rgb(' + textColor[0] + ', ' + textColor[1] + ', ' + textColor[2] + ')';
$('body').css('color', colorString);
$('#colors-top').empty();
$('#colors-bottom').empty();
for (i = 0; i != colors.length; i++) {
var r = colors[i][0][0];
var g = colors[i][0][1];
var b = colors[i][0][2];
var count = colors[i][1];
var colorString = "rgb(" + Math.round(r) + "," + Math.round(g) + "," + Math.round(b) + ")";
var $previewTop = $('<div style="width: 16.6666%; height: 80px; background-color: black; display: inline-block;"></div>');
$previewTop.css('background-color', colorString);
var $previewBottom = $previewTop.clone();
$previewTop.text(count);
//$preview.text(count);
$('#colors-top').append($previewTop);
$('#colors-bottom').append($previewBottom);
}
};
var handleNewImage = function(event) {
event.preventDefault( );
var supported = window.File && window.FileReader && window.FileList && window.Blob;
if (!supported) {
alert("Your browser can't do this :( Try using Safari 5.2+, Chrome, Firefox or IE 10+");
return;
}
var dropEvent = event.originalEvent;
var files = (dropEvent.target.files) || (dropEvent.dataTransfer.files);
var file = files[0];
var reader = new FileReader();
reader.onload = function (readerEvent) {
var image = new Image();
image.onload = function (imageEvent) {
console.log('image', image);
analyseImage(image);
}
image.src = readerEvent.target.result;
}
reader.onerror = function (readerEvent) {
console.log(readerEvent);
}
reader.readAsDataURL(file);
};
window.onload = function() {
$('#dropZone').on('drop', handleNewImage);
$('#file-chooser').bind('change', handleNewImage);
analyseImage();
}
</script>