Three ways to create a gray effect on website images

Three ways to create a gray effect on website images
I’ve always preferred grayscale images because I think they look more artistic. Many image editors like Photoshop can easily convert your color images to grayscale. There are even options to adjust color depth and hue. Unfortunately, this effect is not easy to achieve on the web due to differences in browsers.
1. CSS Filter
Using the CSS filter property is probably the easiest way to convert an image to grayscale. Historically, Internet Explorer had a proprietary CSS property called filter to apply custom effects including grayscale.
Now, the filter property is part of the CSS3 specification and is supported in some browsers, including Firefox, Chrome and Safari. Previously, we also mentioned Webkit filters, which not only turn images to gray but also to sepia and blur effects.
Adding the following CSS style can turn the image into gray

Copy code
The code is as follows:

img {
-webkit-filter: grayscale(1); /* Webkit */
filter: gray; /* IE6-9 */
filter: grayscale(1); /* W3C */
}

Supports IE6-9 and Webkit browsers (Chrome 18+, Safari 6.0+, and Opera 15+)
(Note: This code has no effect on Firefox.)
2. Javascript
The second method is to use JavaScript. Technically, all browsers that support JavaScript should support JavaScript, including IE6 and below.

Copy code
The code is as follows:

var imgObj = document.getElementById('js-image');
function gray(imgObj) {
var canvas = document.createElement('canvas');
var canvasContext = canvas.getContext('2d');
var imgW = imgObj.width;
var imgH = imgObj.height;
canvas.width = imgW;
canvas.height = imgH;
canvasContext.drawImage(imgObj, 0, 0);
var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
imgObj.src = gray(imgObj);

3. SVG
The third method is from SVG Filter. You need to create an SVG file, write the following code in it, and save it as ***.svg

Copy code
The code is as follows:

<svg xmlns="http://www.w3.org/2000/svg">
<filter id="grayscale">
<feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"/>
</filter>
</svg>

Then using the filter properties, we can connect to the SVG file by the ID of the element in the SVG file

Copy code
The code is as follows:

img {
filter: url('img/gray.svg#grayscale');
}

You can also put it in a CSS file, for example:

Copy code
The code is as follows:

img {
filter: url('url("data:image/svg+xml;utf8,<svg%20xmlns='http://www.w3.org/2000/svg'><filter%20id='grayscale'><feColorMatrix%20type='matrix'%20values='0.3333%200.3333%200.3333%200.3333%200%200%200.3333%200.3333%200.3333%200.3333%200%200%200%200%200%200%201%200'/></filter></svg>#grayscale");')
}

In summary, in order to support grayscale effects across browsers, we can use the above methods together with the following code snippet to achieve it. This code will support Firefox 3.5+, Opera 15+, Safari, Chrome, and IE

Copy code
The code is as follows:

img {
-webkit-filter: grayscale(100%);
-webkit-filter: grayscale(1);
filter: grayscale(100%);
filter: url('../img/gray.svg#grayscale');
filter: gray;
}

We can leverage the above code with the JavaScript approach and just provide the CSS filter as a fallback in case JavaScript is disabled. This idea can be easily implemented with the help of Modernizr.

Copy code
The code is as follows:

.no-js img {
-webkit-filter: grayscale(100%);
-webkit-filter: grayscale(1);
filter: grayscale(100%);
filter: url('../img/gray.svg#grayscale');
filter: gray;
}

OK, you can see the cool effect on your browser! !

<<:  Simple example of HTML text formatting (detailed explanation)

>>:  How to implement Hover drop-down menu with CSS

Recommend

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

Solution to MySQL garbled code problem under Linux

The project interacts with the server, accesses t...

Examples of using html unordered list tags and ordered list tags

1. Upper and lower list tags: <dl>..</dl...

MySQL gets the current date and time function

Get the current date + time (date + time) functio...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

Implementation steps of vue-element-admin to build a backend management system

Recently, when I was working on a conference heal...

Detailed explanation of Mysql communication protocol

1.Mysql connection method To understand the MySQL...

Six ways to increase your website speed

1. Replace your .js library file address with the...

Summary of how to add root permissions to users in Linux

1. Add a user . First, use the adduser command to...

Detailed explanation of setting Context Path in Web application

URL: http://hostname.com/contextPath/servletPath/...

Computed properties and listeners details

Table of contents 1. Calculated properties 1.1 Ba...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

Solution to overflow:hidden failure in CSS

Cause of failure Today, when I was writing a caro...