Skip to content

Commit

Permalink
1.0.0 Huey now works on the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrhodes committed Oct 29, 2013
1 parent fb3470b commit ea87e00
Show file tree
Hide file tree
Showing 22 changed files with 464 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
26 changes: 0 additions & 26 deletions README.md

This file was deleted.

34 changes: 34 additions & 0 deletions browser.js
@@ -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)
}
}
118 changes: 118 additions & 0 deletions build/huey.js
@@ -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)
});
;
1 change: 1 addition & 0 deletions build/huey.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cli.js
@@ -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'
)
})
17 changes: 17 additions & 0 deletions example/demo.html
@@ -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>
66 changes: 66 additions & 0 deletions example/demo.js
@@ -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)
37 changes: 37 additions & 0 deletions index.js
@@ -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)
})
}
53 changes: 53 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit ea87e00

Please sign in to comment.