|
771 | 771 | 144 238 144 LightGreen
|
772 | 772 | ")
|
773 | 773 |
|
774 |
| -(let ((color-names |
775 |
| - (flet ((parse (text) |
776 |
| - (with-input-from-string (stream text) |
777 |
| - (loop :for line := (read-line stream nil) |
778 |
| - :while line |
779 |
| - :for elt := (ppcre:register-groups-bind (r g b name) |
780 |
| - ("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([a-zA-Z0-9 ]+)" line) |
781 |
| - (cons (string-downcase name) |
782 |
| - (list (and r (parse-integer r)) |
783 |
| - (and g (parse-integer g)) |
784 |
| - (and b (parse-integer b))))) |
785 |
| - :if elt |
786 |
| - :collect elt)))) |
787 |
| - (alexandria:alist-hash-table (parse *rgb.txt*) :test 'equal)))) |
788 |
| - (defun get-rgb-from-color-name (color-name) |
789 |
| - (gethash (string-downcase color-name) color-names))) |
| 774 | +;; Size includes aliases |
| 775 | +(defvar *color-names* (make-hash-table :size 848 :test 'equal)) |
| 776 | + |
| 777 | +(defun parse-rgb-txt () |
| 778 | + (alexandria:alist-hash-table |
| 779 | + (with-input-from-string (stream *rgb.txt*) |
| 780 | + (loop :for line := (read-line stream nil) |
| 781 | + :while line |
| 782 | + :for elt := (ppcre:register-groups-bind (r g b name) |
| 783 | + ("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([a-zA-Z0-9 ]+)" line) |
| 784 | + (cons (string-downcase name) |
| 785 | + (list (and r (parse-integer r)) |
| 786 | + (and g (parse-integer g)) |
| 787 | + (and b (parse-integer b))))) |
| 788 | + :if elt |
| 789 | + :collect elt)) |
| 790 | + :test 'equal)) |
| 791 | + |
| 792 | +;; Lisp-style color names with a dash instead of space |
| 793 | +(defun add-lisp-color-alias (name value) |
| 794 | + (when (> (length (ppcre:split "\\s+" name)) 1) |
| 795 | + (let ((new-name (ppcre:regex-replace-all "\\s+" name "-"))) |
| 796 | + (setf (gethash new-name *color-names*) value)))) |
| 797 | + |
| 798 | +(defun add-lisp-color-aliases () |
| 799 | + (maphash #'add-lisp-color-alias *color-names*)) |
| 800 | + |
| 801 | +(defun get-rgb-from-color-name (color-name) |
| 802 | + (when (equal (hash-table-count *color-names*) 0) |
| 803 | + (setf *color-names* (parse-rgb-txt)) |
| 804 | + (add-lisp-color-aliases)) |
| 805 | + (gethash (string-downcase color-name) *color-names*)) |
790 | 806 |
|
791 | 807 | (defstruct (color (:constructor make-color (red green blue))) red green blue)
|
792 | 808 |
|
|
0 commit comments