1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Functions for color
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Oct 2003
10 ;;;; This file, part of KMRCL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
12 ;;;; KMRCL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
19 ;; The HSV colour space has three coordinates: hue, saturation, and
20 ;; value (sometimes called brighness) respectively. This colour system is
21 ;; attributed to "Smith" around 1978 and used to be called the hexcone
22 ;; colour model. The hue is an angle from 0 to 360 degrees, typically 0
23 ;; is red, 60 degrees yellow, 120 degrees green, 180 degrees cyan, 240
24 ;; degrees blue, and 300 degrees magenta. Saturation typically ranges
25 ;; from 0 to 1 (sometimes 0 to 100%) and defines how grey the colour is,
26 ;; 0 indicates grey and 1 is the pure primary colour. Value is similar to
27 ;; luninance except it also varies the colour saturation. If the colour
28 ;; space is represented by disks of varying lightness then the hue and
29 ;; saturation are the equivalent to polar coordinates (r,theta) of any
30 ;; point in the plane. The disks on the right show this for various
33 (defun hsv->rgb (h s v)
34 (declare (optimize (speed 3) (safety 0)))
36 (return-from hsv->rgb (values v v v)))
43 (let ((h-pos (/ h 60)))
44 (multiple-value-bind (h-int h-frac) (truncate h-pos)
45 (declare (fixnum h-int))
46 (let ((p (* v (- 1 s)))
47 (q (* v (- 1 (* s h-frac))))
48 (t_ (* v (- 1 (* s (- 1 h-frac)))))
79 (defun hsv255->rgb255 (h s v)
80 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
83 (return-from hsv255->rgb255 (values v v v)))
85 (locally (declare (type fixnum h s v))
91 (let ((h-pos (/ h 60)))
92 (multiple-value-bind (h-int h-frac) (truncate h-pos)
93 (declare (fixnum h-int))
96 (p (round (* 255 fv (- 1 fs))))
97 (q (round (* 255 fv (- 1 (* fs h-frac)))))
98 (t_ (round (* 255 fv (- 1 (* fs (- 1 h-frac))))))
130 (defun rgb->hsv (r g b)
131 (declare (optimize (speed 3) (safety 0)))
133 (let* ((min (min r g b))
141 (setq s (/ delta max)))
145 ((= max r) (/ (- g b) delta))
146 ((= max g) (+ 2 (/ (- b r) delta)))
147 (t (+ 4 (/ (- r g) delta))))))
153 (defun rgb255->hsv255 (r g b)
154 "Integer convert from rgb from 0-255 -> h from 0-360 and sv from 0-255"
155 (declare (fixnum r g b)
156 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
158 (let* ((min (min r g b))
164 (declare (fixnum min max delta v s)
165 (type (or null fixnum) h))
168 (setq s (round (the fixnum (* 255 delta)) max)))
173 (round (the fixnum (* 60 (the fixnum (- g b)))) delta))
176 (+ 120 (round (the fixnum (* 60 (the fixnum (- b r)))) delta))))
179 (+ 240 (round (the fixnum (* 60 (the fixnum (- r g)))) delta))))))
186 (defun hsv-equal (h1 s1 v1 h2 s2 v2 &key (limit .001))
187 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
190 ((and (null a) (null b))
192 ((or (null a) (null b))
195 (< (abs (- a b)) limit)))))
197 ((and (~= 0 v1) (~= 0 v2))
199 ((or (null h1) (null h2))
200 (when (and (~= 0 s1) (~= 0 s2) (~= v1 v2))
203 (when (~= h1 h2) (~= s1 s2) (~= v1 v2)
206 (defun hsv255-equal (h1 s1 v1 h2 s2 v2 &key (limit 1))
207 (declare (type fixnum s1 v1 s2 v2 limit)
208 (type (or null fixnum) h1 h2)
209 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
211 (declare (type (or null fixnum) a b))
213 ((and (null a) (null b))
215 ((or (null a) (null b))
218 (<= (abs (the fixnum (- a b))) limit)))))
220 ((and (~= 0 v1) (~= 0 v2))
222 ((or (null h1) (null h2))
223 (when (and (~= 0 s1) (~= 0 s2) (~= v1 v2))
226 (when (~= h1 h2) (~= s1 s2) (~= v1 v2)
229 (defun hsv-similar (h1 s1 v1 h2 s2 v2 &key
230 (hue-range 15) (value-range .2) (saturation-range 0.2)
231 (gray-limit 0.3) (black-limit 0.3))
232 "Returns T if two HSV values are similar."
234 ;; all black colors are similar
235 ((and (<= v1 black-limit) (<= v2 black-limit))
237 ;; all desaturated (gray) colors are similar for a value, despite hue
238 ((and (<= s1 gray-limit) (<= s2 gray-limit))
239 (when (<= (abs (- v1 v2)) value-range)
242 (when (and (<= (abs (hue-difference h1 h2)) hue-range)
243 (<= (abs (- v1 v2)) value-range)
244 (<= (abs (- s1 s2)) saturation-range))
248 (defun hsv255-similar (h1 s1 v1 h2 s2 v2
249 &key (hue-range 15) (value-range 50) (saturation-range 50)
250 (gray-limit 75) (black-limit 75))
251 "Returns T if two HSV values are similar."
252 (declare (fixnum s1 v1 s2 v2 hue-range value-range saturation-range
253 gray-limit black-limit)
254 (type (or null fixnum) h1 h2)
255 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
257 ;; all black colors are similar
258 ((and (<= v1 black-limit) (<= v2 black-limit))
260 ;; all desaturated (gray) colors are similar for a value, despite hue
261 ((and (<= s1 gray-limit) (<= s2 gray-limit))
262 (when (<= (abs (- v1 v2)) value-range)
265 (when (and (<= (abs (hue-difference-fixnum h1 h2)) hue-range)
266 (<= (abs (- v1 v2)) value-range)
267 (<= (abs (- s1 s2)) saturation-range))
272 (defun hue-difference (h1 h2)
273 "Return difference between two hues around 360 degree circle"
275 ((and (null h1) (null h2))
277 ((or (null h1) (null h2))
280 (let ((diff (- h2 h1)))
291 (defun hue-difference-fixnum (h1 h2)
292 "Return difference between two hues around 360 degree circle"
294 ((and (null h1) (null h2))
296 ((or (null h1) (null h2))
299 (locally (declare (type fixnum h1 h2))
300 (let ((diff (- h2 h1)))