Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created October 1, 2021 08:01
Show Gist options
  • Save KevinGutowski/f061461dfbd67d23619df0c378fb2bd3 to your computer and use it in GitHub Desktop.
Save KevinGutowski/f061461dfbd67d23619df0c378fb2bd3 to your computer and use it in GitHub Desktop.
Create layers for each pixel with opacities
let sketch = require('sketch')
let Rectangle = sketch.Rectangle
let ShapePath = sketch.ShapePath
let Style = sketch.Style
let Group = sketch.Group
let document = sketch.getSelectedDocument()
let selection = document.selectedLayers.layers[0]
let nativeSelection = selection.sketchObject
let data = nativeSelection.image().data()
let imageRep = NSBitmapImageRep.imageRepWithData(data)
let pixelsHigh = imageRep.pixelsHigh()
let pixelsWide = imageRep.pixelsWide()
let pixelLayers = new Group({
name: 'pixel layers',
parent: selection.parent,
frame: selection.frame
})
for (let i=0; i < pixelsWide; i++) {
for (let j=0; j < pixelsHigh; j++) {
let color = imageRep.colorAtX_y(i,j)
let mscolor = MSColor.colorWithNSColor(color)
let rectangle = new ShapePath({
frame: { x: i, y: j, width: 1, height: 1 },
style: { fills: [{color: mscolor, fillType: Style.FillType.Color}] },
parent: pixelLayers,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment