From 53bfe08fd52b52f54cd8612f9a073987dd35d47c Mon Sep 17 00:00:00 2001 From: Jeff Sparkes Date: Tue, 6 Aug 2024 12:53:23 -0400 Subject: [PATCH 1/2] Create lispy aliases for color names with spaces. --- src/common/color.lisp | 47 ++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/common/color.lisp b/src/common/color.lisp index 123348e67..143a17686 100644 --- a/src/common/color.lisp +++ b/src/common/color.lisp @@ -771,22 +771,37 @@ 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))) +(defvar *color-names* (make-hash-table :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) From 46e0131fe6d827b3888d98ecc585ef4e6e610c26 Mon Sep 17 00:00:00 2001 From: Jeff Sparkes Date: Tue, 6 Aug 2024 13:36:49 -0400 Subject: [PATCH 2/2] Set color hash table size since it is known. --- src/common/color.lisp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/color.lisp b/src/common/color.lisp index 143a17686..e43a3fcee 100644 --- a/src/common/color.lisp +++ b/src/common/color.lisp @@ -771,7 +771,8 @@ 144 238 144 LightGreen ") -(defvar *color-names* (make-hash-table :test 'equal)) +;; Size includes aliases +(defvar *color-names* (make-hash-table :size 848 :test 'equal)) (defun parse-rgb-txt () (alexandria:alist-hash-table