munsell-conv.el, munsell.el: combine hue-minor and hue-major.
[elisp/munsell.git] / munsell-conv.el
1 ;; -*- coding: iso-2022-7bit; -*-
2
3 (require 'munsell-data)
4
5 (defun munsell-lookup (color)
6   (cdr (assoc color munsell-color-alist)))
7
8 (put 'munsell-split 'lisp-indent-function 2)
9 (defmacro munsell-split (color-expr vars body &optional invalid-action)
10   (let ((color-var (make-symbol "_color"))
11         (hue-var (nth 0 vars))
12         (value-var (nth 1 vars))
13         (chroma-var (nth 2 vars)))
14     `(let ((,color-var ,color-expr))
15        (if (string-match "^\\([0-9]+\\(\\.[0-9]*\\)?\\)\\(B\\|BG\\|G\\|GY\\|Y\\|YR\\|R\\|RP\\|P\\|PB\\)\\([0-9]+\\(\\.[0-9]*\\)?\\)/\\([0-9]+\\(\\.[0-9]*\\)?\\)$" ,color-var)
16            (let ((,hue-var (cons (string-to-number (match-string 1 ,color-var)) (match-string 3 ,color-var)))
17                  (,value-var (string-to-number (match-string 4 ,color-var)))
18                  (,chroma-var (string-to-number (match-string 6 ,color-var))))
19              ,body)
20          ,(or
21            invalid-action
22            `(error "invalid munsell color: %s" ,color-var))))))
23
24 (defun munsell-round (h v c hue-round value-round chroma-round)
25   (concat
26    (let ((h2 (* 5 (apply hue-round (car h) '(2.5)))))
27      (if (= (logand h2 1) 0)
28          (int-to-string (ash h2 -1))
29        (concat (int-to-string (ash h2 -1)) ".5")))
30    (cdr h)
31    (if (< v 0.9)
32        (concat "0." (int-to-string (* 2 (apply value-round v '(0.2)))))
33      (int-to-string (apply value-round v '(1))))
34    "/"
35    (int-to-string (* 2 (apply chroma-round c '(2))))))
36
37 (defun munsell-convert (color)
38   (munsell-split color (h v c)
39     (munsell-lookup (munsell-round h v c 'round 'round 'round))))
40
41 (provide 'munsell-conv)