                             last-y ((get-in @guistate/transient-state [:mouse-down-pos]) 1)]
                         (visit-pixels-for-line-segment doc-x doc-y last-x last-y)))
                     (when (= paint-tool :fill)
-                      (visit-pixels-for-fill-tool doc-x doc-y (get-in @app [:main-app :image-data])))
+                      (visit-pixels-for-fill-tool doc-x doc-y (get-in @app [:main-app :image-data]) doc-width doc-height))
                     (when (= paint-tool :picker)
                       (pick-color app doc-x doc-y doc-width))
                     (when (and (= paint-tool :selection) (not (om/get-state owner :user-is-moving-selection)))

;; =============================================================================
;; The Ned Flanders function

(defn neighborinos [idx image-data color]
  (let [left   (dec idx)
        right  (inc idx)
        top    (- idx 64)
        bottom (+ idx 64)
        result [left right top bottom]
        valid-result (filter #(and (>= % 0) (< % 4096)) result)]
    (filter #(= (nth image-data %) color) valid-result)))



      (let [current (first to-connect)
            connected (conj connected current)
            to-connect (disj to-connect current)
            candidates (set (neighborinos current image-data doc-width doc-height color))
            to-connect (sets/union to-connect (sets/difference candidates connected))]
        (recur connected to-connect)))))


(defn visit-pixels-for-fill-tool [doc-x doc-y image-data doc-width doc-height]
  (let [[orig-x orig-y] (get-in @guistate/transient-state [:mouse-down-pos])
        idx (geometry/flatten-to-index orig-x orig-y 64)
        color (nth image-data idx)
        fill-target (flood idx image-data doc-width doc-height color)]
    (doseq [i fill-target]
      (visit-pixel i))))

