So, I promise, this is the last Behance API demo this week. I don't know why this has been on my mind so much, but, screw it, when inspiration strikes I just go along with it. For today's demo I'm going to show something a bit different - search by color.

As you can probably guess, Behance allows us to search for projects that match a certain color (or color range). Building that into my API would be kinda boring, so I decided to build something cooler. Imagine being able to simply drop a picture onto your browser and have it return matching projects?

Here is an example. In the screen shot below, the image you see on the top left is one I dragged directly from my desktop.

And another one. (Again, the image on the top left, the kitten, came from my computer.)

Ok, so how is it done? For the most part, pretty simply. The drag and drop aspect is rather trivial and I've blogged on it before. Here is that code snippet.


$(document).ready(function() {
	
	//Set my key
	behanceAPI.setKey(behancekey);
	
	imgDom = $("#droppedImage");
	resultsDom = $("#results");
	
	$(document).on("drop", dropHandler);
	$(document).on("dragover", dragOverHandler);

});

//I simply validate the type
function validFile(s) {
	if(s.match("image/*")) return true;
	return false;
}

function dragOverHandler(e) {
	e.preventDefault();
}

function dropHandler(e) {
	e.stopPropagation();
	e.preventDefault();
	 
	if(!e.originalEvent.dataTransfer.files) return;
	var files = e.originalEvent.dataTransfer.files;
	var count = files.length;
	if(!count) return;
	 
	//Only one file allowed
	if(count > 1) {
		window.alert("You may only drop one file.");
		return;
	}

	var file = files[0];
	
	if(!validFile(file.type)) {
		window.alert("You must drop an image.");
		return;
	}
	
	var reader = new window.FileReader();
	reader.onload = function (e) {
		imgDom.attr("src",e.target.result);
		imgDom.on("load", function(e) {
			doColor();
		});
	};
	reader.readAsDataURL(file);

}

Next - I make use of ColorThief, a library I've mentioned a few times here. It has a function to return the dominant color of a photo.

function doColor() {
	var imageResults = [];
	
	console.log("do color");	
	var colorThief = new ColorThief();
	//dominantColor = getDominantColor(imgDom);
	var dom = colorThief.getColor(imgDom[0]);

This returns an array of colors. From that I can build a RGB string and pass it to a new method of my API I built to support the search API. Note that Behance supports ranges as well. I could make my demo a bit cooler if I created a range based on the color I got. ColorThief also supports returning a set of dominant colors. I could use that API and perform multiple Behance searches to return a collected set of possible matches.

	var rgb = dom[0].toString(16) + dom[1].toString(16) + dom[2].toString(16);
	
	resultsDom.html("<i>Searching Behance for stuff like this - prepare for teh awesome...</i>");
	
	//Ok, now search by color
	behanceAPI.getProjectsByColor(rgb, function(p) {
		console.log("back from the api");
		//gather up the biggest images possible (nah, just check 2 biggest
		for(var i=0;i<p.length;i++) {
			if(p[i].covers[404]) {
				imageResults.push(p[i].covers[404]);	
			} else if(p[i].covers[230]) {
				imageResults.push(p[i].covers[230]);				
			}
		}

		var html = "";
		for(var i=0;i<imageResults.length;i++) {
			html += "<img src='" + imageResults[i] + "'><br/>";
		}
		resultsDom.html(html);
	});

And that's it. I've actually put the demo online so you can see, but please remember that Behance limits API calls to 150 an hour. Based on the average traffic of my blog that limit will probably be blown away. But you may get lucky. I've attached the code as a zip as well.

Download attached file.