(require 'munsell-data) (defun munsell-lookup (color) (cdr (assoc color munsell-color-alist))) (put 'munsell-split 'lisp-indent-function 2) (defmacro munsell-split (color-expr vars body &optional invalid-action) (let ((color-var (make-symbol "_color")) (hue-var (nth 0 vars)) (value-var (nth 1 vars)) (chroma-var (nth 2 vars))) `(let ((,color-var ,color-expr)) (if (string-match "^\\(N\\|\\([0-9]+\\(\\.[0-9]*\\)?\\)\\(B\\|BG\\|G\\|GY\\|Y\\|YR\\|R\\|RP\\|P\\|PB\\)\\)\\([0-9]+\\(\\.[0-9]*\\)?\\)/\\([0-9]+\\(\\.[0-9]*\\)?\\)$" ,color-var) (let ((,hue-var (if (match-beginning 2) (cons (string-to-number (match-string 2 ,color-var)) (match-string 4 ,color-var)) (cons 0 (match-string 1 ,color-var)))) (,value-var (string-to-number (match-string 5 ,color-var))) (,chroma-var (string-to-number (match-string 7 ,color-var)))) ,body) ,(or invalid-action `(error "invalid munsell color: %s" ,color-var)))))) (defun munsell-round (h v c hue-round value-round chroma-round) (concat (let ((h2 (* 5 (apply hue-round (car h) '(2.5))))) (if (= (logand h2 1) 0) (int-to-string (ash h2 -1)) (concat (int-to-string (ash h2 -1)) ".5"))) (cdr h) (if (< v 0.9) (concat "0." (int-to-string (* 2 (apply value-round v '(0.2))))) (int-to-string (apply value-round v '(1)))) "/" (int-to-string (* 2 (apply chroma-round c '(2)))))) (defun munsell-convert (color) (munsell-split color (h v c) (munsell-lookup (munsell-round h v c 'round 'round 'round)))) (provide 'munsell-conv)