/
index.html
224 lines (199 loc) · 7.37 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>image analyser</title>
<script src="release/RGBAnalyse.js"></script>
<style>
html, body {
margin: 0,
padding: 0;
}
#images div {
min-height: 550px;
}
#images div .demo {
display: inline-block;
vertical-align: top;
margin: 1em;
}
#images div div {
display: inline-block;
padding-top: 1em;
height: 0;
overflow: visible;
min-height: 0;
}
#images div div h2 {
margin: 0;
font-family: helvetica;
font-size: 100%;
font-variant: small-caps;
}
#images div div img {
margin: 0;
display: block;
}
#images div div img.histogram.visualisation {
width: 200px;
height: 200px;
background: white;
border: 1px solid black;
}
#images div div img.spectral.histogram.visualisation {
width: 200px;
height: 120px;
background: black;
border: 1px solid black;
}
#images div div img.spectogram.visualisation {
width: 200px;
height: 20px;
background: white;
border: 1px solid black;
}
#images div div hr {
margin: 0;
border: 1px solid black;
width: 200px;
height: 10px;
}
#images div div hr + hr {
margin-top: 1px;
}
dl {
font-family: arial;
margin-left: 2em;
}
dl dt {
font-weight: bold;
font-size: 80%;
}
dl dd {
position: relative;
top: -1em;
margin-left: 12em;
margin-bottom: -1em;
}
.download {
background: #DDD;
}
a, a:visited, a:hover, a:active {
text-decoration: none;
color: rgb(20,70,225);
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>A client-side histogram library for same-origin images</h1>
<p><A href="http://github.com/Pomax/RGBAnalyse">by Pomax</a>, code freely available on <a href="https://github.com/pomax/RGBAnalyse">github</a></p>
<p><span class="download">Download the library <a href="http://pomax.github.io/RGBAnalyse/release/RGBAnalyse.js">here</a>
(7kb). Alternatively, if you use <a href="http://bower.io">bower</a> (and you hopefully do) just use
"bower install rgbanalyse".</span></p>
<p>This library sets up a global RGBAnalyse object, which has a
fairly straight-forward API:</p>
<dl>
<dt>defaults</dt><dd>a settings object with default values as analysis options.</dd>
<dt>analyse(img,[options])</dt><dd>performs image analysis on an image element, with optional tweaks.</dd>
<dt>computeHSL(r,g,b)</dt><dd>convert RGB to HSL (note this requires normalised RGB values).</dd>
<dt>analyse(img,[options])</dt><dd>convert HSL to rgb (plain {r,g,b}, using range [0,255]).</dd>
</dl>
<p>The defaults object can be set prior to running analyses, and currently supports the following properties:</p>
<dl>
<dt>neutrals</dt><dd>10 - integer from 0 to 255, indicating the how close together {r,g,b} coordinates must be to be considered neutral</dd>
<dt>smoothing</dt><dd>5 - used to smooth the hue spectrum when determining dominant hues. (value is distance from mean)</dd>
<dt>distance</dt><dd>10 - minimum distance between recording two hues to end up recorded as major hues (value can range from 0 to 628)</dd>
</dl>
<p>The analyse function returns a two property object. The first is the "analysis" property,
which houses:</p>
<dl>
<dt>rgb</dt><dd>A 3 component object with the histographical data for r, g and b.</dd>
<dt>RGB</dt><dd>A 3 component object with the histographical data for R, G,a nd B.</dd>
<dt>hsl</dt><dd>An object with the histographical data for h, and <code>.dominant</code>, a sorted array of {H,strength} major hues.</dd>
<dt>average</dt><dd>the weighted averages for r, g, b, R, G, B, and h.</dd>
<dt>maxima</dt><dd>the absolute maxima for r, g, b, R, G, B, and h.</dd>
</dl>
<p>The second is the "visualization" property, which houses image src data-uris for:</p>
<dl>
<dt>histogram</dt><dd>a 256 x 256 combined rgb histogram</dd>
<dt>spectrum.histogram</dt><dd>a 360 x 200 hues histogram</dd>
<dt>spectrum.spectogram</dt><dd>a 360 x 50 spectrogram</dd>
</dl>
<h1>Some example images</h1>
<script id="code">
function analyse(img) {
RGBAnalyse.analyse(img, { neutrals: 20, smoothing: 25, distance: 20 }, function(err, data) {
var bin = img.parentNode,
div = document.createElement("div"),
viz;
viz = new Image();
viz.src = data.visualization.histogram;
viz.setAttribute("class", "histogram visualisation");
addVisualization(div, viz, "rgb histogram");
var dominant = data.analysis.hsl.dominant;
dominant.forEach(function(color, idx) {
viz = document.createElement("hr");
var rgb = RGBAnalyse.computeRGB(color.H)
viz.style.background = "rgb("+rgb.r+","+rgb.g+","+rgb.b+")";
addVisualization(div, viz, idx===0 ? "major hues" : false);
});
viz = new Image();
viz.src = data.visualization.spectrum.spectogram;
viz.setAttribute("class", "spectogram visualisation");
addVisualization(div, viz, "spectrogram");
viz = new Image();
viz.src = data.visualization.spectrum.histogram;
viz.setAttribute("class", "spectral histogram visualisation");
addVisualization(div, viz, "spectral histogram");
bin.appendChild(div);
img.onmouseover = function() {
bin.replaceChild(data.analysis.lab, img);
};
data.analysis.lab.onmouseout = function() {
bin.replaceChild(img, data.analysis.lab);
};
});
};
</script>
<p>As a demonstrator, the following images have all been run
through the RGBAnalyser, using the following code:</p>
<pre id="codeblock"></pre>
<script>
var p = document.getElementById("codeblock");
var s = document.getElementById("code");
p.textContent = s.innerHTML;
</script>
<div id="images"></div>
<footer>All images (c) Mike "Pomax" Kamermans, hit up <a href="http://flickr.com/Pomax">Flickr</a> for additional information (and more photographs).</footer>
<script>
function addVisualization(container, element, text) {
if(text) {
var label = document.createElement("h2");
label.textContent = text;
container.appendChild(label);
}
container.appendChild(element);
}
function buildImage(idx) {
idx = (idx<10) ? '0' + idx : idx;
var img = new Image();
img.setAttribute("class", "demo");
var div = document.createElement("div");
div.appendChild(img);
img.onload = function(evt) {
var img = evt.target;
setTimeout(function() {
analyse(img);
}, 100);
};
img.src = "images/img" + idx + ".jpg";
document.getElementById("images").appendChild(div);
}
var imgcount = 17;
for(var i=1; i<=imgcount; i++) { buildImage(i); }
</script>
</body>
</html>