Skip to content

Commit b5d783b

Browse files
authored
Merge pull request #1474 from jsparkes/lispy-color-names
Create lispy aliases for color names with spaces.
2 parents a9a2a04 + 46e0131 commit b5d783b

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/common/color.lisp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -771,22 +771,38 @@
771771
144 238 144 LightGreen
772772
")
773773

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*))
790806

791807
(defstruct (color (:constructor make-color (red green blue))) red green blue)
792808

0 commit comments

Comments
 (0)