     func fill(p:NSPoint, color newColor:NSColor) {
         // floodFillScanlineStack from http://lodev.org/cgtutor/floodfill.html
         
-        let oldColor = pointColor(p)
+        let pixelBuffer = UnsafeMutablePointer<UInt8>(CGBitmapContextGetData(cgContext))
+        
+        let oldColor = _pointColor(p, pixelBuffer:pixelBuffer)
         
         if oldColor == newColor { return }
-
+        
+        guard let newColorNormalized = newColor.colorUsingColorSpaceName(NSCalibratedRGBColorSpace) else {
+            print("-- cannot normalize color \(newColor)")
+            return
+        }
+        
         var stack : [NSPoint] = [p]
-//        var maxStackCount = 0
         
         while let pp = stack.popLast() {
             
             var x1 = pp.x
             
-            while(x1 >= 0 && pointColor(P(x1, pp.y)) == oldColor) {
+            while(x1 >= 0 && _pointColor(P(x1, pp.y), pixelBuffer:pixelBuffer) == oldColor) {
                 x1--
             }
             

        CGContextScaleCTM(cgContext, 1.0, -1.0)
    }
    
    func pointColor(p:NSPoint) -> NSColor {
        
        let data = CGBitmapContextGetData(cgContext)
        let dataType = UnsafePointer<UInt8>(data)
        let offset = 4 * ((Int(self.width) * Int(p.y) + Int(p.x)))
        
        let r = dataType[offset]
        let g = dataType[offset+1]
        let b = dataType[offset+2]
        let a = dataType[offset+3]
        
        return NSColor(
            calibratedRed: CGFloat(Double(r)/255.0),

            var spanAbove = false
            var spanBelow = false
            
            while(x1 < width && _pointColor(P(x1, pp.y), pixelBuffer:pixelBuffer) == oldColor ) {
                
                _setPointColor(P(x1, pp.y), pixelBuffer:pixelBuffer, normalizedColor:newColorNormalized)
                
                let north = P(x1, pp.y-1)
                let south = P(x1, pp.y+1)
                
                if spanAbove == false && pp.y > 0 && _pointColor(north, pixelBuffer:pixelBuffer) == oldColor {
                    stack.append(north)
                    spanAbove = true
                } else if spanAbove && pp.y > 0 && _pointColor(north, pixelBuffer:pixelBuffer) != oldColor {
                    spanAbove = false
                } else if spanBelow == false && pp.y < height - 1 && _pointColor(south, pixelBuffer:pixelBuffer) == oldColor {
                    stack.append(south)
                    spanBelow = true
                } else if spanBelow && pp.y < height - 1 && _pointColor(south, pixelBuffer:pixelBuffer) != oldColor {
                    spanBelow = false
                }
                
                x1++
            }
        }
    }
    
    func line(p1:NSPoint, _ p2:NSPoint, color:NSColor? = NSColor.blackColor()) {
