(in-package :om)



;;;;;;;;;;;;;;;;;;;;;;;;;

;;;first get all notes and their continuation

(defmethod grp-cont-chords ((self list))
  (let ((res '()))
    (loop for i in self
          do (cond
              ((rest-p i) (push i res))
              ((and (chord-p i) (not (cont-chord-p i)))
               (push  i res))
               (t (setf (car res) (list i (car res))))))
    (loop for i in (reverse res)
            collect (if (listp i) (reverse i) i))))


(defmethod grp-cont-notes ((self voice))
  (let* ((objs (collect-chords self))
         (grpchrds (grp-cont-chords objs)))
    (loop for i in grpchrds
          collect (cond
                   ((listp i) (mat-trans (mapcar 'inside i)))
                   ((chord-p i) (inside i))
                   (t (list i))))))
    

(defmethod set-notes-color ((self voice) (pos list) color)
  (let* ((notes (flat-once (grp-cont-notes self))))
    (loop for i in (posn-match notes pos)
          do (if (listp i) 
                 (mapcar #'(lambda (x) (set-mus-color x color)) i)
               (set-mus-color i color)))))
          

;;; now if not colored (= (make-color 0 0 0))  if not --> grey

(defun black-color-p (c1)
  (let ((col1 (format nil "~a" c1)))
        (string-equal col1 "color R:0 G:0 B:0")))

(defmethod set-notes-color-if ((notes list) (pos list) color)
  (loop for i in (posn-match notes pos)
        do (if (listp i) 
               (mapcar #'(lambda (x) (if (black-color-p (mus-color x))
                                         (set-mus-color x color)
                                       (set-mus-color x (om-make-color 0.8 0.8 0.8)))) i)
             (if (black-color-p (mus-color i))
                 (set-mus-color i color)
               (set-mus-color i (om-make-color 0.8 0.8 0.8))
               ))))


(defmethod! set-color-analysis ((self voice) (positions list) (colors list))
   :icon 355
   :indoc '("voice" "list of positions" "list of omcolors")
   :initvals (list t t t)
   :doc "Sets colors of NOTE and REST objects"
  (let* ((notes (flat-once (grp-cont-notes self))))
    (loop for i in positions
          for c in colors
          do (set-notes-color-if notes i c))))

