Skip to content

Commit

Permalink
WIP More pixelpic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Soleone committed Sep 3, 2018
1 parent de4a8ee commit 0b4b41f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
10 changes: 10 additions & 0 deletions src/components/Board.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
</b-col>
</b-row>

<b-row>
<b-col>
<copy-button title="Hex number:" :value="cellsToHex" class="mt-3">
</copy-button>
</b-col>
</b-row>

<b-form class="mt-3">
<b-row>
<b-col>
Expand Down Expand Up @@ -133,6 +140,9 @@ export default {
cellsToBinaryString () {
return cellsToBinaryString(this.rows)
},
cellsToHex () {
return cellsToHex(this.rows)
},
rows () {
return this.cells
},
Expand Down
54 changes: 23 additions & 31 deletions src/pixel_pic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ function resizeCells (cells, targetRowSize, targetColumnSize) {
return newCells
}

function cellsToHex (cells) {
const binaryString = cellsToBinaryString(cells)
const binaryNumber = binaryStringToBinaryNumber(binaryString)
return decimalToHex(binaryNumber)
}

// Input: 2-dimensional Array of cells
// Output: e.g."3x3:101101101"
function cellsToBinaryString (cells) {
const rowSize = cells.length
const columnSize = cells[0].length
const binaryRows = cellsToBinaryRows(cells).map(rows => rows.join('')).join('')
return `${rowSize}${DIMENSION_DELIMITER}${columnSize}${DATA_DELIMITER}${binaryRows}`
}

// Input: e.g."3x3:101101101"
// Output: 2-dimensional Array of cells
function binaryStringToCells (binaryString) {
Expand All @@ -56,18 +71,9 @@ function binaryStringToCells (binaryString) {
})
}

// Input: 2-dimensional Array of cells
// Output: e.g."3x3:101101101"
function cellsToBinaryString (cells) {
const rowSize = cells.length
const columnSize = cells[0].length
const binaryRows = cellsToBinaryRows(cells).map(rows => rows.join('')).join('')
return `${rowSize}${DIMENSION_DELIMITER}${columnSize}${DATA_DELIMITER}${binaryRows}`
}

function binaryStringToBinaryNumber (binaryString) {
const [dimensions, binaryData] = binaryString.split(':')
const [x, y] = dimensions.split('x')
const [x, y] = dimensions.split(DIMENSION_DELIMITER)

const xBinary = (parseInt(x)).toString(2).padStart(5, '0')
const yBinary = (parseInt(y)).toString(2).padStart(5, '0')
Expand All @@ -79,15 +85,16 @@ function binaryStringToBinaryNumber (binaryString) {
function binaryNumberToBinaryString (binaryNumber) {
const decimalBinaryNumber = binaryExponentialToBinary(new Decimal(binaryNumber).toBinary(130))
const binaryNumberAsString = decimalBinaryNumber.padStart(128, 0)
const xSize = parseInt(binaryNumberAsString.slice(-5), 2)
const ySize = parseInt(binaryNumberAsString.slice(-10, -5), 2)
const pixelCount = (xSize * ySize) || 1
const x = parseInt(binaryNumberAsString.slice(-5), 2)
const y = parseInt(binaryNumberAsString.slice(-10, -5), 2)
const pixelCount = (x * y) || 1
const binaryDataPadded = binaryNumberAsString.slice(0, -10)
const binaryData = binaryDataPadded.slice(-pixelCount)
return `${xSize}x${ySize}:${binaryData}`
return `${x}${DIMENSION_DELIMITER}${y}${DATA_DELIMITER}${binaryData}`
}

function decimalToHex (decimal, pad = 32) {
// TODO: Fix toHex
const [, hexdata] = new Decimal(decimal).toHex().split('x')
const hexdataPadded = hexdata.padStart(pad, '0')
const hexdataReversed = chunk(hexdataPadded, 2).reverse().join('')
Expand Down Expand Up @@ -123,27 +130,12 @@ function cellsToBinaryRows (cells) {
})
}

function binaryExponentialToBinary (binaryExponential) {
binaryExponential = binaryExponential.slice(2)
let exponent = binaryExponential.match(/p\+\d+/)[0].slice(2)
let base = binaryExponential.slice(0, binaryExponential.indexOf('p'))

if (base.indexOf('.') > -1) {
const baseNoDecimal = base.replace('.', '')
const zeroCount = exponent - baseNoDecimal.length + 1
const zeros = zeroCount > 0 ? '0'.repeat(zeroCount) : ''
return `${baseNoDecimal}${zeros}`
} else {
const zeros = '0'.repeat(exponent)
return `${base}${zeros}`
}
}

export {
createCells,
resizeCells,
binaryStringToCells,
cellsToHex,
cellsToBinaryString,
binaryStringToCells,
binaryStringToBinaryNumber,
binaryNumberToBinaryString,
decimalToHex,
Expand Down
40 changes: 39 additions & 1 deletion test/unit/specs/pixel_pic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
binaryStringToBinaryNumber,
binaryNumberToBinaryString,
decimalToHex,
hexToDecimal
hexToDecimal,
cellsToHex,
createCells
} from '../../../src/pixel_pic'

describe('binaryStringToBinaryNumber', () => {
Expand Down Expand Up @@ -129,4 +131,40 @@ describe('hexToDecimal', () => {
const decimal = hexToDecimal('0xffffffffffffffff000000000000000000')
expect(decimal.toString()).toBe('18446744073709551615')
})

it('returns a string of max int64 for all ff hex pairs', () => {
const decimal = hexToDecimal('0x5eaffffffffffffffffffffffff00000000')
expect(decimal.toString()).toBe('1298074214633706907132624082304330')
})
})

describe('cellsToHex', () => {
it('returns 33 as hex for 1x1:0', () => {
const cells = createCells(1, 1)
expect(cellsToHex(cells)).toBe('0x21000000000000000000000000000000')
})

it('returns 1057 as hex for 1x1:1', () => {
const cells = createCells(1, 1, () => {
return { filled: true }
})
expect(cellsToHex(cells)).toBe('0x21040000000000000000000000000000')
})

it('returns 1298074214633706907132624082304330 as hex for 10x10:1', () => {
const cells = createCells(10, 10, () => {
return { filled: true }
})
expect(cellsToHex(cells)).toBe('0x5peaffffffffffffffffffffffff00000000')
})

it('returns different hex for differently filled cells', () => {
const cells = createCells(10, 10, () => {
return { filled: true }
})
const differentCells = cells.slice()
differentCells[9][9].filled = false

expect(cellsToHex(cells)).not.toBe(cellsToHex(differentCells))
})
})

0 comments on commit 0b4b41f

Please sign in to comment.