3 (in-package #:vectometry
)
16 (defun rgb-color (r g b
)
17 (make-instance 'color
:red r
:green g
:blue b
))
19 (defclass color
/alpha
(color)
24 (defun rgba-color (r g b a
)
25 (make-instance 'color
/alpha
:red r
:green g
:blue b
:alpha a
))
28 (defun rgb->hsv
(r g b
)
29 (let* ((min (min r g b
))
36 (setq s
(/ delta max
)))
40 (nth-value 0 (/ (- g b
) delta
)))
42 (nth-value 0 (+ 2 (/ (- b r
) delta
))))
44 (nth-value 0 (+ 4 (/ (- r g
) delta
))))))
50 (defun hsv->rgb
(h s v
)
52 (return-from hsv-
>rgb
(values v v v
)))
54 (loop while
(minusp h
)
56 (loop while
(>= h
360)
59 (let ((h-pos (/ h
60)))
60 (multiple-value-bind (h-int h-frac
) (truncate h-pos
)
61 (declare (fixnum h-int
))
62 (let ((p (* v
(- 1 s
)))
63 (q (* v
(- 1 (* s h-frac
))))
64 (t_ (* v
(- 1 (* s
(- 1 h-frac
)))))
94 (defun hsv-color (h s v
)
95 (multiple-value-call 'rgb-color
(hsv->rgb h s v
)))
97 (defgeneric hsv-values
(color)
98 (:method
((color color
))
99 (rgb->hsv
(red color
) (green color
) (blue color
))))
101 (defgeneric rgb-values
(color)
102 (:method
((color color
))
103 (values (red color
) (green color
) (blue color
))))
105 (defgeneric darkp
(color)
107 (multiple-value-bind (hue saturation value
)
110 (and (< 0.5 saturation
)
111 (or (< hue
45) (< 205 hue
)))))))
113 (defvar *black
* (rgb-color 0 0 0))
114 (defvar *white
* (rgb-color 1 1 1))
116 (defun contrasting-text-color (color)
121 (defun add-alpha (color alpha
)
122 (multiple-value-call #'rgba-color
(rgb-values color
) alpha
))
124 (defun float-octet (float)
125 "Convert a float in the range 0.0 - 1.0 to an octet."
126 (values (round (* float
255.0d0
))))
128 (defgeneric html-code
(color)
130 (format nil
"#~2,'0X~2,'0X~2,'0X"
131 (float-octet (red color
))
132 (float-octet (green color
))
133 (float-octet (blue color
)))))
135 (defmethod alpha ((color color
))
138 (defun set-fill-color (color)
139 (vecto:set-rgba-fill
(red color
)
144 (defun set-stroke-color (color)
145 (vecto:set-rgba-stroke
(red color
)
150 (defun html-color (code)
151 (multiple-value-bind (size divisor
)
156 (let* ((start (1+ (* i size
)))
157 (end (+ start size
)))
158 (/ (parse-integer code
:start start
:end end
:radix
16)
160 (rgb-color (value-at 0) (value-at 1) (value-at 2)))))
163 (defun gray-color (value)
164 (rgb-color value value value
))
166 (defun graya-color (value alpha
)
167 (rgba-color value value value alpha
))