Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 6x 6x 6x 36x 36x 36x 36x 36x 5956x 1146500x 3502943x 3502943x 3502943x 3343559x 2210997x 2210997x 2210997x 2210997x 2210997x 2210997x 26600x 26600x 26600x 26600x 26600x 1265346x 1265346x 34x 6x | const Context = require('./context');
const NAMED_COLORS = require('./named_colors');
const uint32 = require('./uint32');
/**
* The Bitmap class is used for direct pixel manipulation(for example setting a pixel colour,
* transparency etc). It also provides a factory method for creating new instances of
* {@link Context}
*
* @class Bitmap
*/
class Bitmap {
/**
* Creates an instance of Bitmap.
* @param {number} w Width
* @param {number} h Height
* @param {any} options Currently unused
* @memberof Bitmap
*/
constructor(w,h, options) {
/**
* @type {number}
*/
this.width = Math.floor(w);
/**
* @type {number}
*/
this.height = Math.floor(h);
/**
* @type {ArrayBuffer}
*/
this.data = Buffer.alloc(w*h*4);
const fillval = NAMED_COLORS.transparent
for(var j=0; j<h; j++) {
for (var i = 0; i < w; i++) {
this.setPixelRGBA(i, j, fillval);
}
}
}
/**
* Calculate Index
*
* @param {number} x X position
* @param {number} y Y position
*
* @returns {number}
*
* @memberof Bitmap
*/
calculateIndex (x,y) {
x = Math.floor(x);
y = Math.floor(y);
if (x<0 || y<0 || x >= this.width || y >= this.height) return 0;
return (this.width*y+x)*4;
}
/**
* Set the RGBA(Red, Green, Blue, Alpha) values on an individual pixel level
*
* @param {number} x X axis position
* @param {number} y Y axis position
* @param {number} rgba A hex representation of the RGBA value of the pixel. See {@link NAMED_COLORS} for examples
*
* @returns {void}
*
* @memberof Bitmap
*/
setPixelRGBA(x,y,rgba) {
let i = this.calculateIndex(x, y);
const bytes = uint32.getBytesBigEndian(rgba);
this.data[i+0] = bytes[0];
this.data[i+1] = bytes[1];
this.data[i+2] = bytes[2];
this.data[i+3] = bytes[3];
}
/**
* Set the individual red, green, blue and alpha levels of an individual pixel
*
* @param {number} x X axis position
* @param {number} y Y axis position
* @param {number} r Red level
* @param {number} g Green level
* @param {number} b Blue level
* @param {number} a Alpha level
*
* @returns {void}
*
* @memberof Bitmap
*/
setPixelRGBA_i(x,y,r,g,b,a) {
let i = this.calculateIndex(x, y);
this.data[i+0] = r;
this.data[i+1] = g;
this.data[i+2] = b;
this.data[i+3] = a;
}
/**
* Get the RGBA value of an individual pixel as a hexadecimal number(See {@link NAMED_COLORS} for examples)
*
* @param {number} x X axis potiion
* @param {number} y Y axis position
*
* @returns {number}
*
* @memberof Bitmap
*/
getPixelRGBA(x,y) {
let i = this.calculateIndex(x, y);
return uint32.fromBytesBigEndian(
this.data[i+0],
this.data[i+1],
this.data[i+2],
this.data[i+3]);
}
/**
* Get Pixel RGBA Seperate
*
* @param {number} x X axis position
* @param {number} y Y axis position
*
* @ignore
*
* @returns {Array}
*
* @memberof Bitmap
*/
getPixelRGBA_separate(x,y) {
var i = this.calculateIndex(x,y);
return this.data.slice(i,i+4);
}
/**
* {@link Context} factory. Creates a new {@link Context} instance object for the current bitmap object
*
* @returns {Context}
*
* @memberof Bitmap
*/
getContext() {
return new Context(this);
}
}
module.exports = Bitmap;
|