Last active
August 3, 2018 22:19
-
-
Save nurpax/103d675b532c3c8c9b6580900734160f to your computer and use it in GitHub Desktop.
smart flip vertical/horizontal
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
const reverseBits = (b) => { | |
const b0 = (b & 1) >> 0 | |
const b1 = (b & 2) >> 1 | |
const b2 = (b & 4) >> 2 | |
const b3 = (b & 8) >> 3 | |
const b4 = (b & 16) >> 4 | |
const b5 = (b & 32) >> 5 | |
const b6 = (b & 64) >> 6 | |
const b7 = (b & 128) >> 7 | |
return (b0 << 7) | (b1 << 6) | (b2 << 5) | (b3 << 4) | (b4 << 3) | (b5 << 2) | (b6 << 1) | (b7 << 0) | |
} | |
const findHFlippedChar = (charset, code) => { | |
const offs = code*8 | |
const origBits = charset.slice(offs, offs+8) | |
for (let ci = 0; ci < 256; ci++) { | |
const flippedBits = charset.slice(ci*8, ci*8+8).map(reverseBits) | |
let equals = true | |
for (let i = 0; i < 8; i++) { | |
if (flippedBits[i] !== origBits[i]) { | |
equals = false | |
break | |
} | |
} | |
if (equals) { | |
return ci | |
} | |
} | |
return code | |
} | |
const findVFlippedChar = (charset, code) => { | |
const offs = code*8 | |
const origBits = charset.slice(offs, offs+8) | |
for (let ci = 0; ci < 256; ci++) { | |
const flippedBits = charset.slice(ci*8, ci*8+8) | |
let equals = true | |
for (let i = 0; i < 8; i++) { | |
if (flippedBits[7-i] !== origBits[i]) { | |
equals = false | |
break | |
} | |
} | |
if (equals) { | |
return ci | |
} | |
} | |
return code | |
} | |
export const mirrorBrush = (brush, { charset, axis, smartMode }) => { | |
if (brush === null) { | |
return brush | |
} | |
if (axis === 'row') { | |
return { | |
...brush, | |
framebuf: [...brush.framebuf].reverse().map(row => { | |
return row.map(({code, color}) => { | |
return { | |
code: findVFlippedChar(charset, code), | |
color | |
} | |
}) | |
}) | |
} | |
} | |
return { | |
...brush, | |
framebuf: brush.framebuf.map(row => [...row].reverse().map(({code, color}) => { | |
return { | |
code: findHFlippedChar(charset, code), | |
color | |
} | |
})) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment