aco-reader
Version:
Transform aco files to JSON format
278 lines (277 loc) • 8.13 kB
JavaScript
// // const acoDecoder = require('aco-decoder')
// //
// // acoDecoder.hex('./foo.aco', function(data) {
// // console.log(data)
// // })
//
// const fs = require('fs')
// var binary = require('binary')
//
// var filepath = './bar.aco'
//
// // //variables for logging
// // var nFiles = 0;
// // var nTotalColors = 0;
// //
// // //store palette information in this object
// // var colorTable = [];
// //
// // //read buffer
// var buf = fs.readFileSync(filepath);
// //
// // //aco1 header information mainly to get color count
// // var header = binary.parse(buf)
// // .word16be('ver')
// // .word16be('nColors')
// // .vars;
// //
// // console.log(header)
// //
// // var skipOneHeader = 4;
// //
// // //skip aco1 section
// // var skipSection1 = skipOneHeader + header.nColors * (5 * 2);
// // //skip aco2 header
// // var toSection2 = skipSection1 + skipOneHeader;
// //
// // //count palette iterations
// // var colorCount = 0;
// //
// // //parse section 2 the first time to get color info and color name field length
// // binary.parse(buf)
// // .skip(toSection2)
// // .loop(function(end, vars){
// // colorCount++;
// // if(this.eof()) {
// // end();
// // }
// // this.word16be('colorSpace')
// // .word16be('w')
// // .word16be('x')
// // .word16be('y')
// // .word16be('z')
// // .word16be('separator')
// // .word16be('lenplus1') //let's not parse any further
// // .tap(function(vars) {
// // //skipping results in an additional iteration so let's not store that one
// // //binary module assigns nulls for fields read outside the buffer so we can test the first one.
// // if (vars.colorSpace !== null) {
// // //an object for each color
// // // colorTable['color-'+colorCount] = {};
// // // color = colorTable['color-'+colorCount];
// // //store field information in those objects
// // for (var i in vars) {
// // // color[i] = vars[i];
// // // console.log(i, vars[i])
// // }
// // // console.log(vars)
// // colorTable.push(Object.assign({}, vars))
// //
// // //give these colors an index number for later use
// // // color.index = colorCount-1;
// //
// // //skip to the next color
// // this.skip(( (vars.lenplus1) * 2));
// // }
// // });
// // });
// //
// // function getColorName(color,skip) {
// //
// // var colorName = "";
// // var n = 0;
// //
// // binary.parse(buf)
// // .skip(skip)
// // .loop(function(end,vars){
// // n++;
// // if (n === (color.lenplus1 -1)) {
// // //end
// // end();
// // }
// // this.word16be('namepart')
// // .tap(function(vars) {
// // //hex representation of this part
// // var hexPart = (vars.namepart).toString(16);
// // //ascii representation of this part
// // var asciiPart = hexToAscii(hexPart);
// // //console.log(asciiPart);
// // colorName += asciiPart;
// // });
// // });
// //
// // return colorName;
// // }
// //
// // var palette = "";
// //
// // function hexToAscii(hex) {
// // var ascii = "";
// // for (var i = 0; i < hex.length; i += 2) {
// // ascii += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
// // }
// // return ascii;
// // }
// //
// // var lastNamesLength = 0;
// //
// // console.log(colorTable)
// //
// // const toHex = (color) => color.toString(16).slice(0, 2)
// // // // converts rgb to hex
// // // const rgbToHex = ({ r, g, b }) => {
// // // return `#${ ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1) }`;
// // // };
// //
// // const colors = colorTable.map((color, i) => (
// // {
// // hex: `#${toHex(color.w)}${toHex(color.x)}${toHex(color.y)}`,
// // // name: `color-${i+1}`
// //
// // }
// // ))
// //
// // console.log(colors)
// //
// // var bar = 0;
// // var foo = binary.parse(buf)
// // .skip(0x67)
// // .loop(function (end, vars) {
// // var biz = this.word16be('letter')
// // .tap((letter) => hexToAscii(letter).toString(16))
// // .vars
// // bar++
// // if(bar==40) end()
// //
// // console.log('hex', hexToAscii(biz.letter.toString(16)) );
// // console.log(biz);
// // console.log(biz.letter.toString(2));
// // return vars;
// // })
// // // .word32be('letter1')
// // // // .skip(1)
// // // .word32be('letter2')
// // // .word32be('letter3')
// // // .word32be('letter4')
// // // .word32be('letter5')
// // // .word16be('nColors')
// // .vars;
// //
// // console.log('var', foo);
// //
// // // console.log(hexToAscii((foo.letter1).toString(16)))
// // // console.log(hexToAscii((foo.letter2).toString(16)))
// //
// // //
// // //
// // // //iterate over our colorTable and store color names
// // // for (var i in colorTable) {
// // //
// // // nTotalColors++;
// // //
// // // var color = colorTable[i];
// // //
// // // //skip aco1, aco2 header, and previously iterated colors
// // // var toNextColorName = toSection2 + ( (color.index + 1) * 14 ) + lastNamesLength;
// // //
// // // //get color name
// // // // if (options.nameOverride && typeof options.nameOverride === String && options.nameOverride.length > 0) {
// // // // color.name = options.nameOverride + "-" + (parseInt(color.index,10) + 1);
// // // // } else {
// // // color.name = getColorName(color, toNextColorName);
// // // // }
// // //
// // // //the length of previous names in bytes
// // // lastNamesLength = lastNamesLength + (color.lenplus1 * 2);
// // //
// // // //write color name to the palette
// // // palette += ("@"+ color.name + ": ");
// // //
// // // //calculate color values and write them to the palette
// // // if (color.colorSpace === 0) {
// // // //RGB
// // // color.w = color.w/256;
// // // color.x = color.x/256;
// // // color.y = color.y/256;
// // //
// // // console.log(toHex(color.w))
// // // console.log(toHex(color.x))
// // // console.log(toHex(color.y))
// // // // palette += "rgb("+color.w+","+color.x+","+color.y+");\n";
// // // // palette += `${(color.w + color.x + color.y).toString(16)}, `
// // // palette += `#${toHex(color.w)}${toHex(color.x)}${toHex(color.y)}`
// // // // ["f5f9fb", "dde6f3", "34384e", "44dcf9", "6566f9", "6295fb", "e780db", "86839e"]
// // // }
// // // if (color.colorSpace === 1) {
// // // //HSB
// // // color.w = color.w/182.04;
// // // color.x = color.x/655.35;
// // // color.y = color.y/655.35;
// // //
// // // palette += "hsv("+color.w+","+color.x+"%,"+color.y+"%);\n";
// // // }
// // //
// // // }
// // //
// // // nFiles++;
// // //
// // // console.log(palette)
// // // return palette;
// //
// // // CEF5CE
// // // 96F5F3
// // // F5A3C0
// // // F1F5A3
//
//
// const header = binary.parse(buf)
// .word16be('ver')
// .word16be('numberOfColors')
// .loop(function (end, vars) {
// vars.numberOfColors--
// !vars.numberOfColors && end()
// console.log(vars)
// const color = this.word16be('color.0')
// .vars
// console.log(color)
//
// })
// // .skip(0x2)
// // .word16be('color1')
// // .word16be('color2')
// // .word16be('color3')
// // .tap()
// .vars;
//
// const HEADER_SIZE = 0x2 * 3;
// // const sectionOneSize = skipOneHeader + header.numberOfColors * (5 * 2);
//
//
// console.log('header', header)
//
// const body = binary.parse(buf)
// .skip(HEADER_SIZE)
// .word16be('color1')
// // .word16be('color2')
// // .word16be('color3')
// // .tap()
// .vars;
//
// const COLOR_SIZE = 0x2 * 5
// //
// // function readColors (buffer, n, start) {
// // const color = binary.parse(buffer)
// // .skip(start)
// // .vars
// //
// // return n ?
// // readColors(buffer, n - 1, COLOR_SIZE * n) :
// // color
// // }
// //
// // readColors(buffer, header.numberOfColors, HEADER_SIZE)
//
// const toHex = (number) => number.toString(16).slice(2)
// console.log(toHex(body.color1))
// // console.log(toHex(header.color2))
// // console.log(toHex(header.color3))