Skip to content

Instantly share code, notes, and snippets.

@petebarber
Last active August 29, 2015 14:20

Revisions

  1. petebarber revised this gist May 3, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion grass.swift
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,6 @@ private func drawGrassIntoBitmap(bitmap: NSBitmapImageRep)

    path.stroke()
    path.fill()

    }

    let SIZE = CGSize(width: 800, height: 400)
  2. petebarber revised this gist May 3, 2015. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions grass.swift
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,6 @@ private func saveAsPNGWithName(fileName: String, bitMap: NSBitmapImageRep) -> Bo
    let props: [NSObject:AnyObject] = [:]
    let imageData = bitMap.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: props)

    let myPath = NSFileManager.defaultManager().currentDirectoryPath

    return imageData!.writeToFile(fileName, atomically: false)
    }

    @@ -35,8 +33,6 @@ private func drawGrassIntoBitmap(bitmap: NSBitmapImageRep)

    let SIZE = CGSize(width: 800, height: 400)

    println("\(Process.arguments[0])")

    if Process.arguments.count != 2
    {
    println("usage: grass <file>")
    @@ -46,4 +42,4 @@ if Process.arguments.count != 2
    let grass = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(SIZE.width), pixelsHigh: Int(SIZE.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)

    drawGrassIntoBitmap(grass!)
    saveAsPNGWithName(Process.arguments[1], grass!)
    saveAsPNGWithName(Process.arguments[1], grass!)
  3. petebarber created this gist May 2, 2015.
    49 changes: 49 additions & 0 deletions grass.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    import Cocoa

    private func saveAsPNGWithName(fileName: String, bitMap: NSBitmapImageRep) -> Bool
    {
    let props: [NSObject:AnyObject] = [:]
    let imageData = bitMap.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: props)

    let myPath = NSFileManager.defaultManager().currentDirectoryPath

    return imageData!.writeToFile(fileName, atomically: false)
    }

    private func drawGrassIntoBitmap(bitmap: NSBitmapImageRep)
    {
    var ctx = NSGraphicsContext(bitmapImageRep: bitmap)

    NSGraphicsContext.setCurrentContext(ctx)

    NSColor(red: 124 / 255, green: 252 / 255, blue: 0, alpha: 1.0).set()

    let path = NSBezierPath()

    path.moveToPoint(NSPoint(x: 0, y: 0))

    for i in stride(from: 0, through: SIZE.width, by: 40)
    {
    path.lineToPoint(NSPoint(x: CGFloat(i + 20), y: CGFloat(arc4random_uniform(400))))
    path.lineToPoint(NSPoint(x: i + 40, y: 0))
    }

    path.stroke()
    path.fill()

    }

    let SIZE = CGSize(width: 800, height: 400)

    println("\(Process.arguments[0])")

    if Process.arguments.count != 2
    {
    println("usage: grass <file>")
    exit(1)
    }

    let grass = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(SIZE.width), pixelsHigh: Int(SIZE.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)

    drawGrassIntoBitmap(grass!)
    saveAsPNGWithName(Process.arguments[1], grass!)