cl-tcod / parse-rgb.lisp

;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;;;;;;;;;;;;;;;80
;;;; parse X11's rgb.txt file to automatically generate colour definitions
;;;; for CL-TCOD.
;;;; Taken from Tamas Papp's CL-COLORS library and modified.
;;;; Original library is available at: (@url :file-name "" :display "Github")
;;;; If you want to regenerate tcod-colours.lisp:
;;;; - Install the CL-PPCRE library
;;;;   from: (@url :file-name "" :display "PCCRE website")
;;;; - At command prompt, change into the cl-tcod library's directory
;;;; - Start lisp, and at the command prompt:
;;;;      (asdf:load-system :parse-rgb)
;;;;      (parse-rgb:parse-rgb-file)

(defpackage :parse-rgb
  (:use :cl)
  (:export #:parse-rgb-file))

(in-package :parse-rgb)

(defvar *rgb-txt-file* "/etc/X11/rgb.txt"
  "Full path to rgb.txt file to parse")

(defvar *output-file-name* "tcod-colours.lisp"
  "Name of file to generate")

(defun parse-rgb-file (&key (infile *rgb-txt-file*)
			      (outfile *output-file-name*))
  "Parse a file of colour definitions in the format of the X-windows file
'rgb.txt'. Generate a file of lisp code that allows the CL-TCOD library
to use all the colours defined in the input file, by name."
  (let ((color-scanner		     ; will only take names w/o spaces
	  :extended-mode t))
    (with-open-file (s infile
		       :direction :input
		       :if-does-not-exist :error)
      (with-open-file (colornames outfile
				  :direction :output
				  :if-exists :supersede
				  :if-does-not-exist :create)
	(format colornames ";;;; This file was generated automatically ~
by parse-rgb.lisp~%~
;;;; Please do not edit directly.~%~
 (in-package :tcod)~%")
	(labels ((string-to-float (string)
		   (let ((i (read-from-string string)))
		     (assert (and (typep i 'integer) (<= i 255)))
	  (do ((line (read-line s nil nil) (read-line s nil nil)))
	      ((not line))
	    (unless (cl-ppcre:scan-to-strings comment-scanner line)
	      (multiple-value-bind (match registers)
		  (cl-ppcre:scan-to-strings color-scanner line)
		  (if (find #\space (aref registers 3))
		      (setf (aref registers 3) (substitute #\- #\space
							   (aref registers 3))))
		  (format colornames
			  "(make-colour :~A ~A ~A ~A)~%"
			  (string-downcase (aref registers 3))
			  (string-to-float (aref registers 0))
			  (string-to-float (aref registers 1))
			  (string-to-float (aref registers 2))))
		   (format *error-output* "ignoring line ~A~%" line)))))))))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.