Guest User

login screen message

a guest
Apr 15th, 2021
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Jimp = require('jimp');
  2. const robot = require("robotjs");
  3. const textract = require('textract');
  4.  
  5. export enum Status {
  6.     LOGGED_OUT,
  7.     LOGGED_IN,
  8. }
  9.  
  10. export enum MenuMessage {
  11.     WELCOME,
  12.     INVALID_CREDENTIALS,
  13.     SUSPECTED_STOLEN,
  14.     DISABLED,
  15.     CONNECTING,
  16.     ENTER_CREDENTIALS,
  17.     UNRESOLVED,
  18. }
  19.  
  20. async function resolveMenuMessage(clientRect: { x: number, y: number, w: number, h: number }) : Promise<MenuMessage> {
  21.     const ii = robot.screen.capture(clientRect.x, clientRect.y, clientRect.x + clientRect.w, clientRect.y + clientRect.w);
  22.     const image = await fixColors(ii);
  23.     const { rgbaArray } = getColor('#fefe00');
  24.     let minX = image.bitmap.width;
  25.     let maxX = 0;
  26.     let minY = image.bitmap.height;
  27.     let maxY = 0;
  28.     image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
  29.         const {r:r2, g:g2, b:b2} = Jimp.intToRGBA(image.getPixelColor(x, y));
  30.         const delta = deltaE(rgbaArray,[r2,g2,b2]);
  31.         if(delta<3) {
  32.             minX = Math.min(x, minX);
  33.             maxX = Math.max(x, maxX);
  34.             minY = Math.min(y, minY);
  35.             maxY = Math.max(y, maxY);
  36.             // sets yellow to black
  37.             image.bitmap.data[idx + 2] = 255;
  38.             image.bitmap.data[idx + 1] = 255;
  39.             image.bitmap.data[idx + 0] = 255;
  40.             image.bitmap.data[idx + 3] = 255;
  41.         } else {
  42.             // sets black to yellow
  43.             image.bitmap.data[idx + 2] = 0;
  44.             image.bitmap.data[idx + 1] = 0;
  45.             image.bitmap.data[idx + 0] = 0;
  46.             image.bitmap.data[idx + 3] = 255;
  47.         }
  48.     });
  49.     image.crop(minX, minY, maxX-minX, maxY-minY);
  50.     image.scale(2);
  51.     const buffer = await image.getBufferAsync('image/png')
  52.     return new Promise((resolve, reject) => {
  53.         textract.fromBufferWithMime('image/png', buffer, (e, t) => {
  54.             if(t) {
  55.                 t = t.toLowerCase();
  56.                 if(t.includes('disable')) {
  57.                     return resolve(MenuMessage.DISABLED);
  58.                 } else if (t.includes('welcome')) {
  59.                     return resolve(MenuMessage.WELCOME)
  60.                 } else if (t.includes('suspect')) {
  61.                     return resolve(MenuMessage.SUSPECTED_STOLEN)
  62.                 } else if (t.includes('credentials')) {
  63.                     return resolve(MenuMessage.INVALID_CREDENTIALS)
  64.                 } else if (t.includes('connecting')) {
  65.                     return resolve(MenuMessage.CONNECTING)
  66.                 } else if (t.includes('enter')) {
  67.                     return resolve(MenuMessage.ENTER_CREDENTIALS)
  68.                 }
  69.             }
  70.             return resolve(MenuMessage.UNRESOLVED)
  71.         })
  72.     })
  73. }
  74.  
  75.  
  76.  
  77. async function fixColors(robotScreenPic) : Promise<any> {
  78.     return new Promise((resolve, reject) => {
  79.         new Jimp(robotScreenPic.width, robotScreenPic.height, (err, image) => {
  80.             if(err) return reject(err);
  81.             let pos = 0;
  82.             image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
  83.                 image.bitmap.data[idx + 2] = robotScreenPic.image.readUInt8(pos++);
  84.                 image.bitmap.data[idx + 1] = robotScreenPic.image.readUInt8(pos++);
  85.                 image.bitmap.data[idx + 0] = robotScreenPic.image.readUInt8(pos++);
  86.                 image.bitmap.data[idx + 3] = robotScreenPic.image.readUInt8(pos++);
  87.             });
  88.             return resolve(image);
  89.         });
  90.     });
  91. }
  92.  
  93. function getColor(css) {
  94.     const hex = Jimp.cssColorToHex(css);
  95.     const rgba =    Jimp.intToRGBA(hex);
  96.     return {
  97.         hex,
  98.         rgba,
  99.         rgbaArray: [rgba.r, rgba.g, rgba.b, rgba.a]
  100.     }
  101. }
  102. function deltaE(rgbA, rgbB) {
  103.     let labA = rgb2lab(rgbA);
  104.     let labB = rgb2lab(rgbB);
  105.     let deltaL = labA[0] - labB[0];
  106.     let deltaA = labA[1] - labB[1];
  107.     let deltaB = labA[2] - labB[2];
  108.     let c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
  109.     let c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
  110.     let deltaC = c1 - c2;
  111.     let deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
  112.     deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
  113.     let sc = 1.0 + 0.045 * c1;
  114.     let sh = 1.0 + 0.015 * c1;
  115.     let deltaLKlsl = deltaL / (1.0);
  116.     let deltaCkcsc = deltaC / (sc);
  117.     let deltaHkhsh = deltaH / (sh);
  118.     let i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
  119.     return i < 0 ? 0 : Math.sqrt(i);
  120. }
  121.  
  122. function rgb2lab(rgb){
  123.     let r = rgb[0] / 255, g = rgb[1] / 255, b = rgb[2] / 255, x, y, z;
  124.     r = (r > 0.04045) ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
  125.     g = (g > 0.04045) ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
  126.     b = (b > 0.04045) ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
  127.     x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
  128.     y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.00000;
  129.     z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
  130.     x = (x > 0.008856) ? Math.pow(x, 1/3) : (7.787 * x) + 16/116;
  131.     y = (y > 0.008856) ? Math.pow(y, 1/3) : (7.787 * y) + 16/116;
  132.     z = (z > 0.008856) ? Math.pow(z, 1/3) : (7.787 * z) + 16/116;
  133.     return [(116 * y) - 16, 500 * (x - y), 200 * (y - z)]
  134. }
RAW Paste Data