Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert Only White Colors To Black With JQuery On a Page

Is it possible, if yes how, to invert only white colors on an html page to black using jquery? Not looking for inverting all colors, that works. Like this:

var css = 'html {-webkit-filter: invert(100%);' +
'-moz-filter: invert(100%);' + 
'-o-filter: invert(100%);' + 
'-ms-filter: invert(100%); }'

But I'm looking to invert only white pixels to black pixels of the whole page including images.

like image 215
jibly Avatar asked Feb 05 '15 11:02

jibly


People also ask

Does white invert to black?

You can invert colors in Photoshop to create a "negative" of an image. Inverting colors on a picture in Photoshop sets all the image's color values to their opposite value on a color wheel — white becomes black, green becomes purple, and more.

How do you invert an image in JavaScript?

To invert the colors of an image in JavaScript, we can set the style. filter property of the element to the same value. to add an image. We just set the -webkit-filter and filter properties to invert(1) to invert the colors of the image.


2 Answers

An interesting question, IMHO :)

I wouldn't entrust to css("color") because the element can inherit its color from parents. The most reliable method is getComputedStyle which returns an actual CSS property of element, doesn't matter how browser calculates it.

Pay attention that color can be in one of the tree forms: a color name like 'white', a short value like '#FFFFFF' or '#ffffff' or rgb / rgba like rgb(255, 255, 255). You should take care on all the cases!

So, the code should be like the following one:

$(body').find('*').each(function(){
    var color = window.getComputedStyle($(this).get(0), null).backgroundColor;
    if ((color.toLowerCase() == "white") ||
        (color.toLowerCase() == "#ffffff") ||
        (color.toLowerCase() == "rgb(255,255,255)")) {
            $(this).css("background-color", "black");
    }    
});

For images you can use canvas. Inside the same loop of elements:

if ($(this).attr("tagName") == "img"){
    invertImage($(this).get(0));
}

function invertImage(img){
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    var ctx = canvas.getContext("2d");

    ctx.drawImage(img, 0, 0);
    var imageData = ctx.getImageData(0, 0, img.width, img.height);    

    var data = imageData.data;
    for (var i = 0; i < data.length; i += 4) {
        if ((data[i] == 255) && (data[i+1] == 255) && (data[i+2] == 255)){
            data[i] = 0;
            data[i+1] = 0;
            data[i+2] = 0;
        }
    }

    ctx.putImageData(imageData, 0, 0);
    $(img).get(0).src = canvas.toDataURL("image/png");
}

And finally, if you want to handle also 'almost white' colors, for example '254, 252, 255' than you need to calculate the luma component as

Y = 0.299 * R + 0.587 * G + 0.114 * B

and threshold the result for something like >= 250

like image 198
Alexander Dayan Avatar answered Oct 10 '22 06:10

Alexander Dayan


You can try like this writing this in on click event

$("#your_ID").click(function () {
    $("div").each(function () {
    var color = $(this).css("color");
            if (color == "#FFFFFF") {
                $(this).css("color", "#000000");
            }
 });
});

Write your IF case like this to check all "near" white color

if(field.css('background-color') == 'rgb(255, 255, 255)') {

 }

change the RGB value to find near white. reference here

Click here for demo

like image 31
Prabhu Vignesh Rajagopal Avatar answered Oct 10 '22 06:10

Prabhu Vignesh Rajagopal