color.lisp Unix DownloadWindows Download
;; -*- mode: common-lisp; package: user -*-

(in-package :user)

;; Part of the ray tracing application.

;; This structure represents a 24-bit color pixel.  It holds three (mod 256) integers.
;; Since it costs no more storage to add a fourth byte, we do so even though we don't
;; currently use it.  It could be used to hole a Z component (transparancy) or an
;; independent intensity factor.

(defmacro rgbi-red   (color)
  `(aref (the (simple-array (unsigned-byte 8) 4) ,color) 0))
(defmacro rgbi-green (color)
  `(aref (the (simple-array (unsigned-byte 8) 4) ,color) 1))
(defmacro rgbi-blue  (color)
  `(aref (the (simple-array (unsigned-byte 8) 4) ,color) 2))
(defmacro rgbi-inten (color)
  `(aref (the (simple-array (unsigned-byte 8) 4) ,color) 3))

(defun make-rgbi (r g b #+never i)
  (let ((color (make-array 4 :element-type '(unsigned-byte 8))))
    (setf (rgbi-red   color) r
	  (rgbi-green color) g
	  (rgbi-blue  color) b
	  #+never (rgbi-inten color) #+never i
	  )
    color))

(define-compiler-macro make-rgbi (r g b)
  (let ((color (gensym)))
    `(let ((,color (make-array 4 :element-type '(unsigned-byte 8))))
       (setf (rgbi-red   ,color) ,r
	     (rgbi-green ,color) ,g
	     (rgbi-blue  ,color) ,b
	     )
       ,color)))