Is there a simple way to display a color bitmap in grayscale with just HTML/CSS
?
It doesn't need to be IE-compatible (and I imagine it won't be) -- if it works in FF3 and/or Sf3, that's good enough for me.
I know I can do it with both SVG
and Canvas, but that seems like a lot of work right now.
Is there a truly lazy person's way to do this?
For Firefox you don't need to create a filter.svg file, you can use data URI scheme.
Taking up the css code of the first answer gives:
Take care to replace "utf-8" string by your file encoding.
This method should be faster than the other because the browser will not need to do a second HTTP request.
Doesn't look like it's possible (yet), even with CSS3 or proprietary
-webkit-
or-moz-
CSS properties.However, I did find this post from last June that used SVG filters on HTML. Not available in any current browser (the demo hinted at a custom WebKit build), but very impressive as a proof of concept.
A new way to do this has been available for some time now on modern browsers.
background-blend-mode allows you to get some interesting effects, and one of them is grayscale conversion
The value luminosity , set on a white background, allows it. (hover to see it in gray)
The luminosity is taken from the image, the color is taken from the background. Since it is always white, there is no color.
But it allows much more.
You can animate the effect setting 3 layers. The first one will be the image, and the second will be a white-black gradient. If you apply a multiply blend mode on this, you will get a white result as before on the white part, but the original image on the black part (multiply by white gives white, multiplying by black has no effect.)
On the white part of the gradient, you get the same effect as before. On the black part of the gradient, you are blending the image over itself, and the result is the unmodified image.
Now, all that is needed is to move the gradient to get this effect dynamic: (hover to see it in color)
reference
compatibility matrix
Following on from brillout.com's answer, and also Roman Nurik's answer, and relaxing somewhat the the 'no SVG' requirement, you can desaturate images in Firefox using only a single SVG file and some CSS.
Your SVG file will look like this:
Save that as resources.svg, it can be reused from now on for any image you want to change to greyscale.
In your CSS you reference the filter using the Firefox specific
filter
property:Add the MS proprietary ones too if you feel like it, apply that class to any image you want to convert to greyscale (works in Firefox >3.5, IE8).
edit: Here's a nice blog post which describes using the new CSS3
filter
property in SalmanPK's answer in concert with the SVG approach described here. Using that approach you'd end up with something like:Further browser support info here.
If you, or someone else facing a similar problem in future are open to PHP. (I know you said HTML/CSS, but maybe you are already using PHP in the backend) Here is a PHP solution:
I got it from the PHP GD library and added some variable to automate the process...