views:

71

answers:

4

Hi everyone,

I was wondering if anyone knew of a way to recolor an image using JavaScript. Any type of access to an image's pixels would work, I suppose, as long I could read/write color values. It should work in Firefox; if it doesn't work in IE, no big deal, but it would be a benefit if it did.

Thank you for your help!

+2  A: 

In addition to alex's answer (eg: to use <canvas>, which is not yet widely supported - cough IE cough), you can use JS to change an image to another and give the illusion that it's just the same image, recolored. The other image can be readily available or can be created by the server on the fly if your server-side language supports image manipulation (eg: PHP can do it with GD).

NullUserException
Is there anyway to leverage the `data` URI scheme to do something like this?
SimpleCoder
However, I imagine it would be very processor intensive to manipulate an image in `data` URI format
SimpleCoder
@Simple Yes, very. I am trying to find a post where something like this was asked and I told explained to the OP why it couldn't be done.
NullUserException
@SimpleCoder: Almost all the browsers that support the data URI also support canvas. So you might as well use the `<canvas>` for this.
slebetman
A: 

The usual way to modify displayed images on the fly is to have duplicates of the image in each of the colours you want, and then use javascript, or CSS, to swap them out.

Andrew Cooper
+3  A: 

You could change the image using canvas.

There are dozens of examples online of changing images to grayscale. There shouldn't be any reason you can't change the colours to something different.

alex
Thank you, I will investigate this.
SimpleCoder
A: 

you could also do something like this http://jsfiddle.net/xewbP/ though not sure you could desaturate the colors like this

Gilsham