Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb3470b
commit ea87e00
Showing
22 changed files
with
464 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var shared = require('./shared') | ||
|
||
var canvas = null | ||
var supported = (function(document) { | ||
canvas = document.createElement('canvas') | ||
return !!(canvas.getContext && canvas.getContext('2d')) | ||
})(document) | ||
|
||
module.exports = function(path, callback) { | ||
var error = shared.invalid( | ||
path, callback, supported | ||
) | ||
|
||
if (error) { | ||
if (!callback) { | ||
throw error | ||
} | ||
callback(error) | ||
return | ||
} | ||
|
||
var image = new Image | ||
if (!/^data/.test(path)) { | ||
image.crossOrigin = true | ||
} | ||
|
||
image.src = path | ||
image.onload = function() { | ||
var huey = shared.helper(canvas) | ||
var data = huey.getImageData(image) | ||
var color = huey.getMostFrequentColor(data) | ||
callback(null, color) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.huey=e():"undefined"!=typeof global?global.huey=e():"undefined"!=typeof self&&(self.huey=e())}(function(){var define,module,exports; | ||
return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var shared = require('./shared') | ||
|
||
var canvas = null | ||
var supported = (function(document) { | ||
canvas = document.createElement('canvas') | ||
return !!(canvas.getContext && canvas.getContext('2d')) | ||
})(document) | ||
|
||
module.exports = function(path, callback) { | ||
var error = shared.invalid( | ||
path, callback, supported | ||
) | ||
|
||
if (error) { | ||
if (!callback) { | ||
throw error | ||
} | ||
callback(error) | ||
return | ||
} | ||
|
||
var image = new Image | ||
if (!/^data/.test(path)) { | ||
image.crossOrigin = true | ||
} | ||
|
||
image.src = path | ||
image.onload = function() { | ||
var huey = shared.helper(canvas) | ||
var data = huey.getImageData(image) | ||
var color = huey.getMostFrequentColor(data) | ||
callback(null, color) | ||
} | ||
} | ||
|
||
},{"./shared":2}],2:[function(require,module,exports){ | ||
var is = function(variable, type) { | ||
return variable && typeof variable === type | ||
} | ||
|
||
exports.invalid = function(path, callback, supported) { | ||
var message = | ||
(supported === false) ? | ||
'Your browser doesn’t support <canvas>' : | ||
(path === undefined) ? | ||
'Please provide an image path.' : | ||
(is(callback, 'function') === false) ? | ||
'Please provide a callback function.' : | ||
null | ||
|
||
return (message ? | ||
new Error(message) : | ||
false | ||
) | ||
} | ||
|
||
exports.helper = function(canvas) { | ||
return { | ||
getImageData: function(image) { | ||
var context = canvas.getContext('2d') | ||
canvas.width = image.width | ||
canvas.height = image.height | ||
context.drawImage(image, 0, 0) | ||
var raw = context.getImageData( | ||
0, 0, image.width, image.height | ||
) | ||
return raw.data | ||
}, | ||
getMostFrequentColor: function(data) { | ||
var dataLength = data.length | ||
var colorCount = {} | ||
var highestColorCount = 0 | ||
var jump = ( | ||
dataLength < 1e4 ? 24 : | ||
dataLength < 1e5 ? 56 : | ||
dataLength < 1e6 ? 512 : | ||
dataLength < 1e7 ? 768 : | ||
dataLength < 1e8 ? 10240 : | ||
12288 | ||
) | ||
var mostFrequentColor, rgb, min, max | ||
for (var i = 0; i < dataLength; i += jump) { | ||
rgb = [data[i], data[i + 1], data[i + 2]] | ||
min = Math.min(rgb[0], Math.min(rgb[1], rgb[2])) | ||
max = Math.max(rgb[0], Math.max(rgb[1], rgb[2])) | ||
if (max - min < 24) { | ||
continue | ||
} | ||
rgb = rgb.join(',') | ||
colorCount[rgb] = ( | ||
colorCount.hasOwnProperty(rgb) ? | ||
++colorCount[rgb] : 1 | ||
) | ||
} | ||
for (rgb in colorCount) { | ||
if (colorCount[rgb] < highestColorCount) { | ||
continue | ||
} | ||
highestColorCount = colorCount[rgb] | ||
mostFrequentColor = rgb | ||
} | ||
if (!mostFrequentColor) { | ||
return null | ||
} | ||
return mostFrequentColor.split(',') | ||
.map(function(value) { | ||
return parseInt(value, 10) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
},{}]},{},[1]) | ||
(1) | ||
}); | ||
; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env node | ||
|
||
var huey = require('./') | ||
|
||
huey(process.argv[2], function(error, color) { | ||
if (error) { | ||
process.stderr.write(error) | ||
return | ||
} | ||
process.stdout.write( | ||
'rgb(' + color.join(', ') + ')\n' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html lang='en-US'> | ||
<head> | ||
<meta charset='UTF-8'> | ||
<title>huey</title> | ||
<style> | ||
html { height: 100% } | ||
body { -webkit-font-smoothing: antialiased; background: #16c no-repeat 50%; background-size: cover; color: #ece9d9; font-family: sans-serif; font-weight: bold; margin: 0 } | ||
#message { border-radius: 4px; display: inline-block; font-weight: bold; height: 24px; left: 50%; line-height: 24px; margin: -24px 0 0 -175px; padding: 1em; position: absolute; text-align: center; top: 45%; vertical-align: middle; width: 320px; z-index: 0} | ||
</style> | ||
</head> | ||
<body> | ||
<p id='message'>Drag an image from your computer into this window (nothing will be uploaded)</p> | ||
<script src="../build/huey.min.js"></script> | ||
<script src="demo.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(function(document) { | ||
var loading = null | ||
var body = document.body | ||
var message = document.getElementById('message') | ||
var caption = document.getElementById('caption') | ||
var input = document.createElement('input') | ||
input.type = 'file' | ||
|
||
message.onclick = function() { | ||
input.click() | ||
} | ||
|
||
document.ondragover = | ||
document.ondragend = function (e) { | ||
e.preventDefault() | ||
} | ||
input.onchange = | ||
document.ondrop = function (e) { | ||
e.preventDefault() | ||
var file = ( | ||
e.dataTransfer ? | ||
e.dataTransfer.files[0] : | ||
e.target.files[0] | ||
) | ||
if (!file) { | ||
return | ||
} | ||
message.innerHTML = 'Calculating…' | ||
var reader = new FileReader() | ||
reader.onload = function (e) { | ||
var url = e.target.result | ||
var start = new Date() | ||
|
||
huey(url, function(error, color) { | ||
var isGreyscale = !color | ||
color = color || [128,128,128] | ||
var total = color[0] + color[1] + color[2] | ||
var end = new Date() | ||
var time = (end.getTime() - start.getTime()) | ||
|
||
body.style.cssText = [ | ||
'background-color: rgb(' + color + ')', | ||
'background-image: url(' + url + ')' | ||
].join('; ') | ||
|
||
message.style.cssText = [ | ||
'background-color: rgb(' + color + ')', | ||
'color: ' + (total > 500 ? '#222' : '#fff') | ||
].join('; ') | ||
|
||
message.innerHTML = ( | ||
isGreyscale ? | ||
'Greyscale' : | ||
'rgb(' + color.join(',') + ')' | ||
) | ||
|
||
message.setAttribute('title', 'Calculated in ' + ( | ||
time >= 1000 ? | ||
(time / 1000).toFixed(2) + 's' : | ||
time + 'ms') | ||
) | ||
}) | ||
}; | ||
reader.readAsDataURL(file) | ||
} | ||
})(document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var fs = require('fs') | ||
var Canvas = require('canvas') | ||
var shared = require('./shared') | ||
|
||
module.exports = function(path, callback) { | ||
var error = shared.invalid( | ||
path, callback | ||
) | ||
|
||
if (error) { | ||
if (!callback) { | ||
throw error | ||
} | ||
callback(error) | ||
return | ||
} | ||
|
||
fs.readFile(path, function(error, file) { | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
var image = new Canvas.Image | ||
image.src = file | ||
|
||
var canvas = new Canvas( | ||
image.width, image.height | ||
) | ||
|
||
var huey = shared.helper(canvas) | ||
var data = huey.getImageData(image) | ||
var color = huey.getMostFrequentColor(data) | ||
|
||
callback(null, color) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "huey", | ||
"version": "1.0.0", | ||
"description": "A little utility that finds the dominant colour of an image and returns it as an RGB array.", | ||
"main": "index.js", | ||
"browser": "browser.js", | ||
"bin": "cli.js", | ||
"browserify": { | ||
"transform": ["brfs"] | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": { | ||
"canvas": "~1.1.1" | ||
}, | ||
"devDependencies": { | ||
"tape": "~2.1.0", | ||
"brfs": "~0.0.8", | ||
"browserify": "~2.35.0", | ||
"uglify-js": "~2.4.1" | ||
}, | ||
"scripts": { | ||
"test": "tape ./test/server.js", | ||
"postinstall": "./node_modules/.bin/browserify ./browser.js -s huey -o ./build/huey.js && ./node_modules/.bin/uglifyjs ./build/huey.js -mco ./build/huey.min.js" | ||
}, | ||
"testling": { | ||
"files": "./test/browser.js", | ||
"browsers": { | ||
"ie": [8, 9, 10], | ||
"chrome": [25], | ||
"firefox": [4, 19], | ||
"safari": [5.1, 6], | ||
"opera": [10, 12], | ||
"iphone": [6], | ||
"android": [4.2] | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:michaelrhodes/huey.git" | ||
}, | ||
"keywords": [ | ||
"dominant", | ||
"colour", | ||
"color" | ||
], | ||
"author": "Michael Rhodes", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/michaelrhodes/huey/issues" | ||
} | ||
} |
Oops, something went wrong.