8

I am trying to work out how to change the hue of a greyscale image using CSS...

I have two images (one colour and one greyscale) and have applied this code to both:

CSS

img { width: 10pc; float: left; }
.huerotate { -webkit-filter: hue-rotate(300deg); }

HTML:

<img alt="Test photo: Mona Lisa" src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/500px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg" class="huerotate" />

<img alt="Test photo: Hand" src="http://i821.photobucket.com/albums/zz137/ocnsamu/Capture-2.jpg" class="huerotate" />

This successfully changes the colour image, but the greyscale image remains unchanged.

Is there a way to change the hue of greyscale images, either using CSS another technology?

A demo is here: http://jsfiddle.net/ATpv8/

8
  • 2
    AFAIK, you can't do a hue rotate on grey...it's stays grey It has no 'color' as such to be rotated. You might try applying the sepia filter first and then 'rotating' that.( html5rocks.com/en/tutorials/filters/understanding-css)
    – Paulie_D
    Commented May 7, 2014 at 14:13
  • for the grey one, you need to colorize it before
    – G-Cyrillus
    Commented May 7, 2014 at 14:14
  • Thanks both, adding sepia doesn't seem to make any difference... @GCyrillus, how would I colourize it? Can this be done in the HTML/CSS? Commented May 7, 2014 at 14:21
  • 1
    Until we know what it is you are actually trying to do it's hard to help. If you only have a greyscale image you cannot add color to specific areas. You can only colorise the whole thing and then, possibly, rotate the color ..again, of the whole thing. See: jsfiddle.net/ATpv8/1
    – Paulie_D
    Commented May 7, 2014 at 14:26
  • 2
    Shall I write this up as an answer and post? Although really the credit should go to you all for helping but don't want to make more work! Happy to write @Paulie_D 's jsfiddle.net/ATpv8/1 up if there are no takers. Commented May 7, 2014 at 14:39

1 Answer 1

16

As greyscale images do not contain colour by definition, a sepia filter needs to be added first to 'colourise' the greyscale photo.

From there the hue-rotate function can be applied to give a colour tint.

.colorme {
      -webkit-filter: sepia(100%) hue-rotate(300deg); 
}

Demo here: http://jsfiddle.net/ATpv8/2/

1
  • 3
    Can't believe it could be done, but indeed I have made a light-green out of a white-transparent image with: sepia(100%) hue-rotate(64deg) saturate(9). It's all about the sepia. NOTE: the order matters! Sepia first! Commented Apr 23, 2015 at 15:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.