Skip to content

Commit

Permalink
Merge pull request #1474 from jsparkes/lispy-color-names
Browse files Browse the repository at this point in the history
Create lispy aliases for color names with spaces.
  • Loading branch information
cxxxr authored Aug 7, 2024
2 parents a9a2a04 + 46e0131 commit b5d783b
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/common/color.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -771,22 +771,38 @@
144 238 144 LightGreen
")

(let ((color-names
(flet ((parse (text)
(with-input-from-string (stream text)
(loop :for line := (read-line stream nil)
:while line
:for elt := (ppcre:register-groups-bind (r g b name)
("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([a-zA-Z0-9 ]+)" line)
(cons (string-downcase name)
(list (and r (parse-integer r))
(and g (parse-integer g))
(and b (parse-integer b)))))
:if elt
:collect elt))))
(alexandria:alist-hash-table (parse *rgb.txt*) :test 'equal))))
(defun get-rgb-from-color-name (color-name)
(gethash (string-downcase color-name) color-names)))
;; Size includes aliases
(defvar *color-names* (make-hash-table :size 848 :test 'equal))

(defun parse-rgb-txt ()
(alexandria:alist-hash-table
(with-input-from-string (stream *rgb.txt*)
(loop :for line := (read-line stream nil)
:while line
:for elt := (ppcre:register-groups-bind (r g b name)
("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([a-zA-Z0-9 ]+)" line)
(cons (string-downcase name)
(list (and r (parse-integer r))
(and g (parse-integer g))
(and b (parse-integer b)))))
:if elt
:collect elt))
:test 'equal))

;; Lisp-style color names with a dash instead of space
(defun add-lisp-color-alias (name value)
(when (> (length (ppcre:split "\\s+" name)) 1)
(let ((new-name (ppcre:regex-replace-all "\\s+" name "-")))
(setf (gethash new-name *color-names*) value))))

(defun add-lisp-color-aliases ()
(maphash #'add-lisp-color-alias *color-names*))

(defun get-rgb-from-color-name (color-name)
(when (equal (hash-table-count *color-names*) 0)
(setf *color-names* (parse-rgb-txt))
(add-lisp-color-aliases))
(gethash (string-downcase color-name) *color-names*))

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

Expand Down

0 comments on commit b5d783b

Please sign in to comment.