Last active
May 23, 2016 07:58
-
-
Save ishukshin/102f2e6865c98c07740b2841461b2b11 to your computer and use it in GitHub Desktop.
The script changes black color of every image to another desirable color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The script changes black color of every image to another desirable color | |
* requires page with jquery | |
* USAGE: execute it in browser console | |
* | |
* Замена чёрного цвета на изображениях страницы на любой другой (тёмно-синий, к примеру) | |
* Сделано для печати билетов РЖД, т.к. чёрный картридж закончился. | |
* У РЖД билеты - это html страницы с jquery по адресу вида https://pass.rzd.ru/ticket/secure/ru?STRUCTURE_ID=NN&layer_id=MM&ORDER_ID=XX | |
* выполнение скрипта в консоли браузера меняет чёрный цвет в картинках на любой другой | |
*/ | |
// var script = document.createElement( 'script' ); script.type = 'text/javascript'; script.src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js'; document.body.appendChild(script); | |
// change text color to darkblue | |
$('*').css('color', '#000040'); | |
// then modify images | |
var minsize = 10; | |
var oldcolor = {r: 0, g: 0, b: 0}; | |
var newcolor = {r: 0, g: 0, b: 64}; | |
function alterImage(canvas, image){ | |
var ctx = canvas.getContext("2d"); | |
// copy image to canvas | |
ctx.drawImage(image, 0, 0); | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
// iterate over image data. Every pixel has 4 values (rgba) | |
for (var i = 0; i < imageData.data.length; i += 4) { | |
// check if black, every value is zero | |
if ( imageData.data[i] === oldcolor.r && imageData.data[i+1] === oldcolor.g && imageData.data[i+2] === oldcolor.b ) { | |
imageData.data[i] = newcolor.r; // set R bit | |
imageData.data[i+1] = newcolor.g; // set G bit | |
imageData.data[i+2] = newcolor.b; //set B bit to 50. | |
} | |
} | |
// set change image data | |
ctx.putImageData(imageData, 0, 0); | |
} | |
// process every image | |
$('img').each(function(){ | |
// process it if image is large enough | |
if($(this).width() > minsize && $(this).height() > minsize){ | |
// create same size canvas and insert it just after the image | |
var $canvas = $('<canvas width="'+$(this).width() +'" height="' +$(this).height()+ '"></canvas>'); | |
alterImage($canvas[0], this); | |
$(this).attr('src', $canvas[0].toDataURL("image/png")); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment