Last active
March 15, 2016 16:07
-
-
Save autioch/f78e6731ace77c1e7f86 to your computer and use it in GitHub Desktop.
Sampling colors from png file.
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
'use strict'; | |
// npm install png-js https://github.com/devongovett/png.js | |
// npm install image-size https://www.npmjs.com/package/image-size | |
const options = process.argv.slice(2); | |
if (options.length < 3) { | |
throw 'Required arguments: filename, cell width, cell height. Optional 4th argument: row spacing.'; | |
} | |
const imageFilename = options[0]; | |
const cellWidth = parseInt(options[1], 10); //39 | |
const cellHeight = parseInt(options[2], 10); //6 | |
const rowSpacing = parseInt(options.length === 4 ? options[3] : 0, 10); //14 | |
const imgDims = require('image-size')(imageFilename); | |
const cellCount = Math.floor(imgDims.width / cellWidth); | |
const cellDims = { | |
width: cellWidth, | |
height: cellHeight, | |
widthHalf: Math.floor(cellWidth / 2), | |
heightHalf: Math.floor(cellHeight / 2) | |
}; | |
require('png-js').decode(imageFilename, readColors); | |
/** @description Converts colors to dictionary and writes fo tile. */ | |
function readColors(data) { | |
console.log(`Reading ${imageFilename}: ${imgDims.width}x${imgDims.height}, ${data.length / 4} pixels`); | |
const colors = []; | |
let height = 0; | |
while (height < imgDims.height) { | |
colors.push(readRow(data, height)); | |
height += cellDims.height + rowSpacing; | |
} | |
writeColors(colors); | |
} | |
/** @description Converts colors to dictionary and writes fo file. */ | |
function writeColors(colors) { | |
const outputFilename = imageFilename.split('.')[0] + '.json'; | |
const dictionary = colors.reduce(function(dictionary, colorRow, index) { | |
dictionary[index] = colorRow; | |
return dictionary; | |
}, {}); | |
console.log(`Writing ${colors.length} rows to ${outputFilename}.`); | |
require('fs').writeFile(outputFilename, JSON.stringify(dictionary, null, ' ')); | |
} | |
/** @description Reads row of colors. */ | |
function readRow(data, height) { | |
const colorRow = []; | |
let width = 0; | |
for (let i = 0; i < cellCount; i++) { | |
colorRow.push(readCell(data, height, width)); | |
width += cellDims.width; | |
} | |
return colorRow; | |
} | |
/** @description Reads centerish area of the cell. */ | |
function readCell(data, height, width) { | |
const index = (width + cellDims.widthHalf + (height + cellDims.heightHalf) * imgDims.width) * 4; | |
return '#' + rgbItemToHex(data[index]) + rgbItemToHex(data[index + 1]) + rgbItemToHex(data[index + 2]); | |
} | |
/** @description Convert numeric value to hex representation. */ | |
function rgbItemToHex(component) { | |
const hexColor = component.toString(16); | |
return (hexColor.length === 1 ? '0' + hexColor : hexColor).toUpperCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment