* Returns the offset of a pixel in the bitmap buffer * @param x the x coordinate * @param y the y coordinate * @param edgeHandling (optional) define how to sum pixels from outside the border * @returns the index of the pixel or -1 if not found * @example * ```ts *
(x: number, y: number, edgeHandling?: Edge)
| 548 | * ``` |
| 549 | */ |
| 550 | getPixelIndex(x: number, y: number, edgeHandling?: Edge) { |
| 551 | let xi; |
| 552 | let yi; |
| 553 | |
| 554 | if (!edgeHandling) { |
| 555 | edgeHandling = Edge.EXTEND; |
| 556 | } |
| 557 | |
| 558 | if (typeof x !== "number" || typeof y !== "number") { |
| 559 | throw new Error("x and y must be numbers"); |
| 560 | } |
| 561 | |
| 562 | // round input |
| 563 | x = Math.round(x); |
| 564 | y = Math.round(y); |
| 565 | xi = x; |
| 566 | yi = y; |
| 567 | |
| 568 | if (edgeHandling === Edge.EXTEND) { |
| 569 | if (x < 0) xi = 0; |
| 570 | if (x >= this.bitmap.width) xi = this.bitmap.width - 1; |
| 571 | if (y < 0) yi = 0; |
| 572 | if (y >= this.bitmap.height) yi = this.bitmap.height - 1; |
| 573 | } |
| 574 | |
| 575 | if (edgeHandling === Edge.WRAP) { |
| 576 | if (x < 0) { |
| 577 | xi = this.bitmap.width + x; |
| 578 | } |
| 579 | |
| 580 | if (x >= this.bitmap.width) { |
| 581 | xi = x % this.bitmap.width; |
| 582 | } |
| 583 | |
| 584 | if (y < 0) { |
| 585 | yi = this.bitmap.height + y; |
| 586 | } |
| 587 | |
| 588 | if (y >= this.bitmap.height) { |
| 589 | yi = y % this.bitmap.height; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | let i = (this.bitmap.width * yi + xi) << 2; |
| 594 | |
| 595 | // if out of bounds index is -1 |
| 596 | if (xi < 0 || xi >= this.bitmap.width) { |
| 597 | i = -1; |
| 598 | } |
| 599 | |
| 600 | if (yi < 0 || yi >= this.bitmap.height) { |
| 601 | i = -1; |
| 602 | } |
| 603 | |
| 604 | return i; |
| 605 | } |
| 606 | |
| 607 | /** |
no outgoing calls
no test coverage detected