root/trunk/thirdparty/cl-gd-0.5.6/colors-aux.lisp

Revision 2428, 6.7 kB (checked in by hhubner, 1 year ago)

Update cl-gd.

Line 
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-GD; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/gd/colors-aux.lisp,v 1.12 2007/01/01 23:41:00 edi Exp $
3
4 ;;; Copyright (c) 2003-2007, Dr. Edmund Weitz.  All rights reserved.
5
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9
10 ;;;   * Redistributions of source code must retain the above copyright
11 ;;;     notice, this list of conditions and the following disclaimer.
12
13 ;;;   * Redistributions in binary form must reproduce the above
14 ;;;     copyright notice, this list of conditions and the following
15 ;;;     disclaimer in the documentation and/or other materials
16 ;;;     provided with the distribution.
17
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 (in-package :cl-gd)
31
32 (defun current-brush (&optional (image *default-image*))
33   "Returns the GD image which is the current brush of IMAGE \(or NIL
34 if there is no current brush)."
35   (check-type image image)
36   (let ((brush (get-slot-value (img image) 'gd-image 'brush)))
37     (if (null-pointer-p brush)
38       nil
39       brush)))
40
41 (defun (setf current-brush) (brush &optional (image *default-image*))
42   "Sets BRUSH \(which must be a GD image) to be the current brush
43 for IMAGE."
44   (check-type brush image)
45   (check-type image image)
46   (gd-image-set-brush (img image) (img brush))
47   brush)
48
49 (defun current-tile (&optional (image *default-image*))
50   "Returns the GD image which is the current tile of IMAGE \(or NIL
51 if there is no current tile)."
52   (check-type image image)
53   (let ((tile (get-slot-value (img image) 'gd-image 'tile)))
54     (if (null-pointer-p tile)
55       nil
56       tile)))
57
58 (defun (setf current-tile) (tile &optional (image *default-image*))
59   "Sets TILE \(which must be a GD image) to be the current tile
60 for IMAGE."
61   (check-type tile (or image null))
62   (check-type image image)
63   (gd-image-set-tile (img image) (img tile))
64   tile)
65
66 (defun current-style (&optional (image *default-image*))
67   "Returns the current style of IMAGE as a list."
68   (check-type image image)
69   (let ((style-length (get-slot-value (img image) 'gd-image 'style-length))
70         (style (get-slot-value (img image) 'gd-image 'style)))
71     (loop for i below style-length
72           collect (let ((color (deref-array style '(:array :int) i)))
73                     (if (= color +transparent+)
74                       nil
75                       color)))))
76
77 (defun current-style* (&key (image *default-image*))
78   "Returns the current style of IMAGE as an array."
79   (check-type image image)
80   (let ((style-length (get-slot-value (img image) 'gd-image 'style-length))
81         (style (get-slot-value (img image) 'gd-image 'style)))
82     (loop with result = (make-array style-length)
83           for i below style-length
84           do (setf (aref result i)
85                      (let ((color (deref-array style '(:array :int) i)))
86                        (if (= color +transparent+)
87                          nil
88                          color)))
89           finally (return result))))
90
91 (defgeneric (setf current-style) (style &optional image)
92   (:documentation "Sets STYLE to be the current drawing style for
93 IMAGE. STYLE can be a LIST or a VECTOR. Each element of STYLE is
94 either a color or NIL \(for transparent pixels)."))
95
96 (defmethod (setf current-style) ((style list) &optional (image *default-image*))
97   (check-type image image)
98   (let ((length (length style)))
99     (with-safe-alloc (c-style (allocate-foreign-object :int length)
100                               (free-foreign-object c-style))
101       (loop for color in style
102             for i from 0
103             do (setf (deref-array c-style '(:array :int) i)
104                        (typecase color
105                          (null +transparent+)
106                          (integer color)
107                          (t 1))))
108       (gd-image-set-style (img image) c-style length)
109       style)))
110
111 (defmethod (setf current-style) ((style vector) &optional (image *default-image*))
112   (check-type image image)
113   (let ((length (length style)))
114     (with-safe-alloc (c-style (allocate-foreign-object :int length)
115                               (free-foreign-object c-style))
116       (loop for color across style
117             for i from 0
118             do (setf (deref-array c-style '(:array :int) i)
119                        (typecase color
120                          (null +transparent+)
121                          (integer color)
122                          (t 1))))
123       (gd-image-set-style (img image) c-style length)
124       style)))
125
126 (defun set-anti-aliased (color do-not-blend &optional (image *default-image*))
127   "Set COLOR to be the current anti-aliased color of
128 IMAGE. DO-NOT-BLEND \(if provided) is the background color
129 anti-aliased lines stand out against clearly."
130   (check-type color integer)
131   (check-type do-not-blend (or integer null))
132   (check-type image image)
133   (gd-image-set-anti-aliased-do-not-blend (img image)
134                                           color
135                                           (or do-not-blend -1)))
136
137 (defun resolve-c-color (color image)
138   "Accepts a CL-GD 'color' COLOR and returns the corresponding
139 argument for GD, modifying internal slots of IMAGE if needed."
140   (etypecase color
141     (brush
142       (setf (current-brush image) color)
143       +brushed+)
144     (tile
145       (setf (current-tile image) color)
146       +tiled+)
147     ((cons brush (or vector list))
148       (setf (current-brush image) (car color)
149             (current-style image) (cdr color))
150       +styled-brushed+)
151     (anti-aliased-color
152       (set-anti-aliased (color color)
153                         (do-not-blend color)
154                         image)
155       +anti-aliased+)
156     ((or vector list)
157       (setf (current-style image) color)
158       +styled+)
159     (integer
160       color)))
161
162 (defmacro with-color-argument (&body body)
163   "Internal macro used to give correct color arguments to enclosed
164 foreign functions. Assumes fixed names COLOR and IMAGE."
165   (with-unique-names (c-color-arg)
166     `(let ((,c-color-arg (resolve-c-color color image)))
167       ,@(sublis (list (cons 'color c-color-arg))
168                 body :test #'eq))))
Note: See TracBrowser for help on using the browser.