| 1 |
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-GD; Base: 10 -*- |
|---|
| 2 |
;;; $Header: /usr/local/cvsrep/gd/colors.lisp,v 1.25 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 |
(defmacro with-default-color ((color) &body body) |
|---|
| 33 |
"Executes BODY with *DEFAULT-COLOR* bound to COLOR so that you don't |
|---|
| 34 |
have to provide the COLOR keyword/optional argument to drawing |
|---|
| 35 |
functions." |
|---|
| 36 |
`(let ((*default-color* ,color)) |
|---|
| 37 |
,@body)) |
|---|
| 38 |
|
|---|
| 39 |
(defun allocate-color (red green blue &key alpha (errorp t) (image *default-image*)) |
|---|
| 40 |
"Finds the first available color index in the image IMAGE specified, |
|---|
| 41 |
sets its RGB values to those requested \(255 is the maximum for each), |
|---|
| 42 |
and returns the index of the new color table entry, or an RGBA value |
|---|
| 43 |
in the case of a true color image. In either case you can then use the |
|---|
| 44 |
returned value as a COLOR parameter to drawing functions. When |
|---|
| 45 |
creating a new palette-based image, the first time you invoke this |
|---|
| 46 |
function you are setting the background color for that image. If ALPHA |
|---|
| 47 |
\(not greater than 127) is provided, an RGBA color will always be |
|---|
| 48 |
allocated. If all +GD-MAX-COLORS+ have already been allocated this |
|---|
| 49 |
function will, depending on the value of ERRORP, either raise an error |
|---|
| 50 |
or return NIL." |
|---|
| 51 |
(check-type red integer) |
|---|
| 52 |
(check-type green integer) |
|---|
| 53 |
(check-type blue integer) |
|---|
| 54 |
(check-type alpha (or null integer)) |
|---|
| 55 |
(check-type image image) |
|---|
| 56 |
(let ((result |
|---|
| 57 |
(if alpha |
|---|
| 58 |
(gd-image-color-allocate-alpha (img image) red green blue alpha) |
|---|
| 59 |
(gd-image-color-allocate (img image) red green blue)))) |
|---|
| 60 |
(cond ((and errorp |
|---|
| 61 |
(= result -1)) |
|---|
| 62 |
(error "Can't allocate color")) |
|---|
| 63 |
((= result -1) |
|---|
| 64 |
nil) |
|---|
| 65 |
(t |
|---|
| 66 |
result)))) |
|---|
| 67 |
|
|---|
| 68 |
(defun deallocate-color (color &optional (image *default-image*)) |
|---|
| 69 |
"Marks the specified color COLOR as being available for reuse. No |
|---|
| 70 |
attempt will be made to determine whether the color index is still in |
|---|
| 71 |
use in the image IMAGE." |
|---|
| 72 |
(check-type color integer) |
|---|
| 73 |
(check-type image image) |
|---|
| 74 |
(gd-image-color-deallocate (img image) color)) |
|---|
| 75 |
|
|---|
| 76 |
(defun transparent-color (&optional (image *default-image*)) |
|---|
| 77 |
"Returns the transparent color of IMAGE \(or NIL if there is none)." |
|---|
| 78 |
(check-type image image) |
|---|
| 79 |
(gd-image-get-transparent (img image))) |
|---|
| 80 |
|
|---|
| 81 |
(defun (setf transparent-color) (color &optional (image *default-image*)) |
|---|
| 82 |
"Makes COLOR the transparent color of IMAGE. If COLOR is NIL the |
|---|
| 83 |
image won't have a transparent color. Note that JPEG images don't |
|---|
| 84 |
support transparency." |
|---|
| 85 |
(check-type color (or null integer)) |
|---|
| 86 |
(check-type image image) |
|---|
| 87 |
(gd-image-color-transparent (img image) (or color -1)) |
|---|
| 88 |
color) |
|---|
| 89 |
|
|---|
| 90 |
(defun true-color-p (&optional (image *default-image*)) |
|---|
| 91 |
"Returns true iff IMAGE is a true color image." |
|---|
| 92 |
(check-type image image) |
|---|
| 93 |
(not (zerop (get-slot-value (img image) 'gd-image 'true-color)))) |
|---|
| 94 |
|
|---|
| 95 |
(defun number-of-colors (&key (image *default-image*)) |
|---|
| 96 |
"Returns the number of color allocated in IMAGE. Returns NIL if |
|---|
| 97 |
IMAGE is a true color image." |
|---|
| 98 |
(check-type image image) |
|---|
| 99 |
(if (true-color-p image) |
|---|
| 100 |
nil |
|---|
| 101 |
(get-slot-value (img image) 'gd-image 'colors-total))) |
|---|
| 102 |
|
|---|
| 103 |
(defun find-color (red green blue &key alpha exact hwb resolve (image *default-image*)) |
|---|
| 104 |
"Tries to find and/or allocate a color from IMAGE's color |
|---|
| 105 |
palette. If EXACT is true, the color will only be returned if it is |
|---|
| 106 |
already allocated. If EXACT is NIL, a color which is 'close' to the |
|---|
| 107 |
color specified by RED, GREEN, and BLUE \(and probably ALPHA) might be |
|---|
| 108 |
returned \(unless there aren't any colors allocated in the image |
|---|
| 109 |
yet). If HWB is true, the 'closeness' will be determined by hue, |
|---|
| 110 |
whiteness, and blackness, otherwise by the Euclidian distance of the |
|---|
| 111 |
RGB values. If RESOLVE is true a color \(probably a new one) will |
|---|
| 112 |
always be returned, otherwise the result of this function might be |
|---|
| 113 |
NIL. If ALPHA \(not greater than 127) is provided, an RGBA color (or |
|---|
| 114 |
NIL) will be returned. |
|---|
| 115 |
|
|---|
| 116 |
ALPHA, EXACT, and HWB are mutually exclusive. RESOLVE can't be used |
|---|
| 117 |
together with EXACT or HWB." |
|---|
| 118 |
(check-type red integer) |
|---|
| 119 |
(check-type green integer) |
|---|
| 120 |
(check-type blue integer) |
|---|
| 121 |
(check-type alpha (or null integer)) |
|---|
| 122 |
(check-type image image) |
|---|
| 123 |
(when (< 1 (count-if #'identity (list alpha exact hwb))) |
|---|
| 124 |
(error "You can't specify two of ALPHA, EXACT, and HWB at the same |
|---|
| 125 |
time")) |
|---|
| 126 |
(when (and hwb resolve) |
|---|
| 127 |
(error "You can't specify HWB and RESOLVE at the same time")) |
|---|
| 128 |
(when (and exact resolve) |
|---|
| 129 |
(error "You can't specify EXACT and RESOLVE at the same time")) |
|---|
| 130 |
(let ((result |
|---|
| 131 |
(cond ((and resolve alpha) |
|---|
| 132 |
(gd-image-color-resolve-alpha (img image) red green blue alpha)) |
|---|
| 133 |
(resolve |
|---|
| 134 |
(gd-image-color-resolve (img image) red green blue)) |
|---|
| 135 |
(alpha |
|---|
| 136 |
(gd-image-color-closest-alpha (img image) red green blue alpha)) |
|---|
| 137 |
(exact |
|---|
| 138 |
(gd-image-color-exact (img image) red green blue)) |
|---|
| 139 |
(hwb |
|---|
| 140 |
(gd-image-color-closest-hwb (img image) red green blue)) |
|---|
| 141 |
(t |
|---|
| 142 |
(gd-image-color-closest (img image) red green blue))))) |
|---|
| 143 |
(if (= result -1) |
|---|
| 144 |
nil |
|---|
| 145 |
result))) |
|---|
| 146 |
|
|---|
| 147 |
(defun thickness (&optional (image *default-image*)) |
|---|
| 148 |
"Gets the width of lines drawn by the drawing functions. Note that |
|---|
| 149 |
this is measured in pixels and is NOT affected by |
|---|
| 150 |
WITH-TRANSFORMATION." |
|---|
| 151 |
(check-type image image) |
|---|
| 152 |
(get-slot-value (img image) 'gd-image 'thick)) |
|---|
| 153 |
|
|---|
| 154 |
(defun (setf thickness) (thickness &optional (image *default-image*)) |
|---|
| 155 |
"Sets the width of lines drawn by the drawing functions. Note that |
|---|
| 156 |
THICKNESS is measured in pixels and is NOT affected by |
|---|
| 157 |
WITH-TRANSFORMATION." |
|---|
| 158 |
(check-type thickness integer) |
|---|
| 159 |
(check-type image image) |
|---|
| 160 |
(gd-image-set-thickness (img image) thickness) |
|---|
| 161 |
thickness) |
|---|
| 162 |
|
|---|
| 163 |
(defmacro with-thickness ((thickness &key (image '*default-image*)) &body body) |
|---|
| 164 |
"Executes BODY with the current line width of IMAGE set to |
|---|
| 165 |
THICKNESS. The image's previous line width is guaranteed to be |
|---|
| 166 |
restored before the macro exits. Note that the line width is measured |
|---|
| 167 |
in pixels and is not affected by WITH-TRANSFORMATION." |
|---|
| 168 |
(with-unique-names (old-thickness) |
|---|
| 169 |
;; we rebind everything so we have left-to-right evaluation |
|---|
| 170 |
(with-rebinding (thickness image) |
|---|
| 171 |
`(let ((,old-thickness (thickness ,image))) |
|---|
| 172 |
(unwind-protect |
|---|
| 173 |
(progn |
|---|
| 174 |
(setf (thickness ,image) ,thickness)) |
|---|
| 175 |
,@body) |
|---|
| 176 |
(setf (thickness ,image) ,old-thickness))))) |
|---|
| 177 |
|
|---|
| 178 |
(defun alpha-blending-p (&optional (image *default-image*)) |
|---|
| 179 |
"Returns whether pixels drawn on IMAGE will be copied literally |
|---|
| 180 |
including alpha channel information \(return value is false) or if |
|---|
| 181 |
their alpha channel information will determine how much of the |
|---|
| 182 |
underlying color will shine through \(return value is true). This is |
|---|
| 183 |
only meaningful for true color images." |
|---|
| 184 |
(check-type image image) |
|---|
| 185 |
(not (zerop (get-slot-value (img image) 'gd-image 'alpha-blending-flag)))) |
|---|
| 186 |
|
|---|
| 187 |
(defun (setf alpha-blending-p) (blending &optional (image *default-image*)) |
|---|
| 188 |
"Determines whether pixels drawn on IMAGE will be copied literally |
|---|
| 189 |
including alpha channel information \(if BLENDING is false) or if |
|---|
| 190 |
their alpha channel information will determine how much of the |
|---|
| 191 |
underlying color will shine through \(if BLENDING is true). This is |
|---|
| 192 |
only meaningful for true color images." |
|---|
| 193 |
(check-type image image) |
|---|
| 194 |
(gd-image-alpha-blending (img image) (if blending 1 0)) |
|---|
| 195 |
blending) |
|---|
| 196 |
|
|---|
| 197 |
(defun save-alpha-p (&optional (image *default-image*)) |
|---|
| 198 |
"Returns whether PNG images will be saved with full alpha channel |
|---|
| 199 |
information." |
|---|
| 200 |
(check-type image image) |
|---|
| 201 |
(not (zerop (get-slot-value (img image) 'gd-image 'save-alpha-flag)))) |
|---|
| 202 |
|
|---|
| 203 |
(defun (setf save-alpha-p) (save &key (image *default-image*)) |
|---|
| 204 |
"Determines whether PNG images will be saved with full alpha channel |
|---|
| 205 |
information." |
|---|
| 206 |
(check-type image image) |
|---|
| 207 |
(gd-image-save-alpha (img image) (if save 1 0)) |
|---|
| 208 |
save) |
|---|
| 209 |
|
|---|
| 210 |
(defun color-component (component color &key (image *default-image*)) |
|---|
| 211 |
"Returns the specified color component of COLOR. COMPONENT can be |
|---|
| 212 |
one of :RED, :GREEN, :BLUE, and :ALPHA." |
|---|
| 213 |
(check-type color integer) |
|---|
| 214 |
(check-type image image) |
|---|
| 215 |
(funcall (ecase component |
|---|
| 216 |
((:red) #'gd-image-get-red) |
|---|
| 217 |
((:green) #'gd-image-get-green) |
|---|
| 218 |
((:blue) #'gd-image-get-blue) |
|---|
| 219 |
((:alpha) #'gd-image-get-alpha)) |
|---|
| 220 |
(img image) |
|---|
| 221 |
color)) |
|---|
| 222 |
|
|---|
| 223 |
(defun color-components (color &key (image *default-image*)) |
|---|
| 224 |
"Returns a list of the color components of COLOR. The |
|---|
| 225 |
components are in the order red, green, blue, alpha." |
|---|
| 226 |
(mapcar #'(lambda (c) (color-component c color :image image)) |
|---|
| 227 |
'(:red :green :blue :alpha))) |
|---|
| 228 |
|
|---|
| 229 |
(defun find-color-from-image (color source-image &key alpha exact hwb |
|---|
| 230 |
resolve (image *default-image*)) |
|---|
| 231 |
"Returns the color in IMAGE corresponding to COLOR in |
|---|
| 232 |
SOURCE-IMAGE. The keyword parameters are passed to FIND-COLOR." |
|---|
| 233 |
(let ((red (color-component :red color |
|---|
| 234 |
:image source-image)) |
|---|
| 235 |
(blue (color-component :blue color |
|---|
| 236 |
:image source-image)) |
|---|
| 237 |
(green (color-component :green color |
|---|
| 238 |
:image source-image)) |
|---|
| 239 |
(alpha (when alpha |
|---|
| 240 |
(color-component :alpha color |
|---|
| 241 |
:image source-image)))) |
|---|
| 242 |
(find-color red green blue |
|---|
| 243 |
:alpha alpha |
|---|
| 244 |
:exact exact |
|---|
| 245 |
:hwb hwb |
|---|
| 246 |
:resolve resolve |
|---|
| 247 |
:image image))) |
|---|