1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: kmrcl -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Functions for color
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Oct 2003
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
21 ;; The HSV colour space has three coordinates: hue, saturation, and
22 ;; value (sometimes called brighness) respectively. This colour system is
23 ;; attributed to "Smith" around 1978 and used to be called the hexcone
24 ;; colour model. The hue is an angle from 0 to 360 degrees, typically 0
25 ;; is red, 60 degrees yellow, 120 degrees green, 180 degrees cyan, 240
26 ;; degrees blue, and 300 degrees magenta. Saturation typically ranges
27 ;; from 0 to 1 (sometimes 0 to 100%) and defines how grey the colour is,
28 ;; 0 indicates grey and 1 is the pure primary colour. Value is similar to
29 ;; luninance except it also varies the colour saturation. If the colour
30 ;; space is represented by disks of varying lightness then the hue and
31 ;; saturation are the equivalent to polar coordinates (r,theta) of any
32 ;; point in the plane. The disks on the right show this for various
35 (defun hsv->rgb (h s v)
36 (declare (optimize (speed 3) (safety 0)))
38 (return-from hsv->rgb (values v v v)))
45 (let ((h-pos (/ h 60)))
46 (multiple-value-bind (h-int h-frac) (truncate h-pos)
47 (declare (fixnum h-int))
48 (let ((p (* v (- 1 s)))
49 (q (* v (- 1 (* s h-frac))))
50 (t_ (* v (- 1 (* s (- 1 h-frac)))))
81 (defun hsv255->rgb255 (h s v)
82 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
85 (return-from hsv255->rgb255 (values v v v)))
87 (locally (declare (type fixnum h s v))
93 (let ((h-pos (/ h 60)))
94 (multiple-value-bind (h-int h-frac) (truncate h-pos)
95 (declare (fixnum h-int))
98 (p (round (* 255 fv (- 1 fs))))
99 (q (round (* 255 fv (- 1 (* fs h-frac)))))
100 (t_ (round (* 255 fv (- 1 (* fs (- 1 h-frac))))))
132 (defun rgb->hsv (r g b)
133 (declare (optimize (speed 3) (safety 0)))
135 (let* ((min (min r g b))
143 (setq s (/ delta max)))
148 (nth-value 0 (/ (- g b) delta)))
150 (nth-value 0 (+ 2 (/ (- b r) delta))))
152 (nth-value 0 (+ 4 (/ (- r g) delta))))))
153 (setq h (the fixnum (* 60 h)))
159 (defun rgb255->hsv255 (r g b)
160 "Integer convert from rgb from 0-255 -> h from 0-360 and sv from 0-255"
161 (declare (fixnum r g b)
162 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
164 (let* ((min (min r g b))
170 (declare (fixnum min max delta v s)
171 (type (or null fixnum) h))
174 (setq s (truncate (the fixnum (* 255 delta)) max)))
179 (truncate (the fixnum (* 60 (the fixnum (- g b)))) delta))
182 (+ 120 (truncate (the fixnum (* 60 (the fixnum (- b r)))) delta))))
185 (+ 240 (truncate (the fixnum (* 60 (the fixnum (- r g)))) delta))))))
192 (defun hsv-equal (h1 s1 v1 h2 s2 v2 &key (limit .001))
193 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
196 ((and (null a) (null b))
198 ((or (null a) (null b))
201 (< (abs (- a b)) limit)))))
203 ((and (~= 0 v1) (~= 0 v2))
205 ((or (null h1) (null h2))
206 (when (and (~= 0 s1) (~= 0 s2) (~= v1 v2))
209 (when (~= h1 h2) (~= s1 s2) (~= v1 v2)
212 (defun hsv255-equal (h1 s1 v1 h2 s2 v2 &key (limit 1))
213 (declare (type fixnum s1 v1 s2 v2 limit)
214 (type (or null fixnum) h1 h2)
215 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
217 (declare (type (or null fixnum) a b))
219 ((and (null a) (null b))
221 ((or (null a) (null b))
224 (<= (abs (the fixnum (- a b))) limit)))))
226 ((and (~= 0 v1) (~= 0 v2))
228 ((or (null h1) (null h2))
229 (when (and (~= 0 s1) (~= 0 s2) (~= v1 v2))
232 (when (~= h1 h2) (~= s1 s2) (~= v1 v2)
235 (defun hsv-similar (h1 s1 v1 h2 s2 v2 &key
236 (hue-range 15) (value-range .2) (saturation-range 0.2)
237 (gray-limit 0.3) (black-limit 0.3))
238 "Returns T if two HSV values are similar."
240 ;; all black colors are similar
241 ((and (<= v1 black-limit) (<= v2 black-limit))
243 ;; all desaturated (gray) colors are similar for a value, despite hue
244 ((and (<= s1 gray-limit) (<= s2 gray-limit))
245 (when (<= (abs (- v1 v2)) value-range)
248 (when (and (<= (abs (hue-difference h1 h2)) hue-range)
249 (<= (abs (- v1 v2)) value-range)
250 (<= (abs (- s1 s2)) saturation-range))
254 (defun hsv255-similar (h1 s1 v1 h2 s2 v2
255 &key (hue-range 15) (value-range 50) (saturation-range 50)
256 (gray-limit 75) (black-limit 75))
257 "Returns T if two HSV values are similar."
258 (declare (fixnum s1 v1 s2 v2 hue-range value-range saturation-range
259 gray-limit black-limit)
260 (type (or null fixnum) h1 h2)
261 (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)))
263 ;; all black colors are similar
264 ((and (<= v1 black-limit) (<= v2 black-limit))
266 ;; all desaturated (gray) colors are similar for a value, despite hue
267 ((and (<= s1 gray-limit) (<= s2 gray-limit))
268 (when (<= (abs (- v1 v2)) value-range)
271 (when (and (<= (abs (hue-difference-fixnum h1 h2)) hue-range)
272 (<= (abs (- v1 v2)) value-range)
273 (<= (abs (- s1 s2)) saturation-range))
278 (defun hue-difference (h1 h2)
279 "Return difference between two hues around 360 degree circle"
281 ((and (null h1) (null h2))
283 ((or (null h1) (null h2))
286 (let ((diff (- h2 h1)))
297 (defun hue-difference-fixnum (h1 h2)
298 "Return difference between two hues around 360 degree circle"
300 ((and (null h1) (null h2))
302 ((or (null h1) (null h2))
305 (locally (declare (type fixnum h1 h2))
306 (let ((diff (- h2 h1)))