Example #1
0
    sizeOf(imageSrc, function (err, dimensions) {
        if (err) {
            throw err;
        }

        // Go through every value in pixels
        c = 0;
        for (i = 0; i < pixels.length; i += 1) {
            // Create a pixel
            if (c === 0) {
                arr = [];
            }

            arr.push(pixels[i]);

            c += 1;

            if (c === 4) {
                imgArr.push(arr);
                c = 0;
            }
        }

        // Create the rows
        c = 0;
        h = 0;
        for (i = 0; i < imgArr.length; i += 1) {
            pixel = imgArr[i];
            if (pixel[3] === 255) {
                color = rgb2hex('rgb(' + pixel[0] +',' + pixel[1] +',' + pixel[2] +')').hex;
                cssStr += '        ' + c + 'em ' + h + 'em 0 ' + color + ',\n';
            }

            c += 1;

            if (c === dimensions.width) {
                c = 0;
                h += 1;

                if (h < dimensions.height) {
                    cssStr += '\n        /* Line ' + h +' */\n';
                }
            }
        }

        // End the css file
        cssStr = cssStr.slice(0, cssStr.length - 2);
        cssStr += '\n}';

        // Save the css file
        fs.writeFile(output, cssStr, function(err) {
            if (err) {
                throw err;
            }

            console.log('Success!');
        });
    });
Example #2
0
let parse = function (cssPropertyValue, cssProperty) {
    if (!cssPropertyValue || !cssPropertyValue.value) {
        return null
    }

    let parsedValue = {
        property: cssProperty,
        value: cssPropertyValue.value.toLowerCase().trim()
    }

    if (parsedValue.value.indexOf('rgb') === 0) {
        /**
         * remove whitespaces in rgb values
         */
        parsedValue.value = parsedValue.value.replace(/\s/g, '')

        /**
         * parse color values
         */
        let color = parsedValue.value
        parsedValue.parsed = rgb2hex(parsedValue.value)
        parsedValue.parsed.type = 'color'
        parsedValue.parsed[/[rgba]+/g.exec(color)[0]] = color
    } else if (parsedValue.property === 'font-family') {
        let font = cssValue(cssPropertyValue.value)
        let string = parsedValue.value
        let value = cssPropertyValue.value.split(/,/).map(sanitize.css)

        parsedValue.value = sanitize.css(font[0].value || font[0].string)
        parsedValue.parsed = { value, type: 'font', string }
    } else {
        /**
         * parse other css properties
         */
        try {
            parsedValue.parsed = cssValue(cssPropertyValue.value)

            if (parsedValue.parsed.length === 1) {
                parsedValue.parsed = parsedValue.parsed[0]
            }

            if (parsedValue.parsed.type && parsedValue.parsed.type === 'number' && parsedValue.parsed.unit === '') {
                parsedValue.value = parsedValue.parsed.value
            }
        } catch (e) {
            // TODO improve css-parse lib to handle properties like
            // `-webkit-animation-timing-function :  cubic-bezier(0.25, 0.1, 0.25, 1)
        }
    }

    return parsedValue
}
Example #3
0
  function create (event) {
    if (event.forEach) return event.forEach(create)

    if (event.type === 'header') {
      layout = event.layout.type
      createPage(event)
    }

    if (layout === 'portrait') rotate(event, 1, opt.size[1])

    if (event.type === 'style') {
      style = xtend(event.args)
    }

    if (event.layout || event.type === 'style') {
      restore = true
      doc.save()
    }
    if (event.layout) {
      if (event.layout.font) {
        doc.font(event.layout.font.name)
        doc.fontSize(event.layout.font.size)
      }
      if (event.layout.rotate) {
        doc.rotate(event.layout.rotate.deg, {origin: [event.args.x, event.args.y]})
      }
    }

    if (event.args && event.args.stroke) {
      color = event.args.stroke === 'transparent'
        ? 'transparent'
        : rgb2hex(event.args.stroke).hex
    }

    if (style) applyStyles(style)

    if (color && color !== 'transparent') {
      strokeColor(color)
    }

    var f = doc[type(event)]
    if (f && args(event).length) {
      f.apply(doc, args(event))
      doc.stroke()
      if (restore) {
        doc.restore()
        restore = false
      }
    }
  }
Example #4
0
getPixels(image, function(err, pixels) {
    if (err) return console.log('Bad pixels path');
    console.log('processing ' + image + ' -> SVG');

    var bar = new ProgressBar(':percent', { total: (pixels.data.buffer.byteLength / 4) });
    var shape = pixels.shape.slice();
    var width = shape[0];
    var height = shape[1];

    svgStream.write('<?xml version="1.0" encoding="utf-8" ?>\n'+
                    '<svg baseProfile="full" ' +
                    'version="1.1" ' +
                    'height="'+height+'px" ' +
                    'width="'+width+'px" ' +
                    'xmlns="http://www.w3.org/2000/svg" ' +
                    'xmlns:ev="http://www.w3.org/2001/xml-events" ' +
                    'xmlns:xlink="http://www.w3.org/1999/xlink">\n');

    var c = 0;
    for (var i = 0; i < height; i += 1) {
        for (var j = 0; j < width; j += 1) {
            var r = pixels.data[c];
            var g = pixels.data[c+1];
            var b = pixels.data[c+2];
            var a = pixels.data[c+3];

            // If colour data is found, draw a rect.
            if (r !== 0 && g !== 0 && b !== 0) {
                var rgbStr = 'rgb('+r+','+g+','+b+')';
                var hexObj = rgb2hex(rgbStr);
                var rectStr = '<rect fill="'+ hexObj.hex +'" height="1px" width="1px" x="' + j + '" y="'+ i +'" />\n';
                svgStream.write(rectStr);
            }

            bar.tick();
            c += 4;
        }
    }

    svgStream.end('</svg>', function () {
        console.log('done!');
    });

});
Example #5
0
// Dependancies
var fs = require('fs')
  , rgb2hex = require('rgb2hex')
  ;

// Variables
var rgbArr = require('./data')
	,	fname = 'grandmaster.smd'

rgb2hex(rgbArr, function(buffer) {
  fs.writeFile(fname, buffer);
})