1 ;;; color-selector.el --- a graphical widget of three sliders to change faces.
3 ;; Copyright (C) 1997 Jens Lautenbacher <jens@tellux.de>
5 ;; Author: Jens Lautenbacher <jens@tellux.de>
6 ;; Keywords: extensions, faces
9 ;; This file is part of XEmacs.
11 ;; XEmacs and color-selector.el are free software; you can redistribute
12 ;; and/or modify them under the terms of the GNU General Public
13 ;; License as published by the Free Software Foundation; either
14 ;; version 2, or (at your option) any later version.
16 ;; XEmacs and color-selector.el are distributed in the hope that they
17 ;; will be useful, but WITHOUT ANY WARRANTY; without even the implied
18 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 ;; See the GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Synched up with: Not in FSF.
29 ;; ############## to test it: ##############################################
31 ;; NOTE: providing callback function is optional, but normally needed to
32 ;; integrate the selector into your program.
34 ;;(defun cs-message (selector)
35 ;; (let* ((face (color-selector-get-face selector))
36 ;; (mode (color-selector-get-mode selector))
37 ;; (clist (color-selector-get-color selector))
38 ;; (color (format "[#%02x%02x%02x]" (car clist) (cadr clist) (caddr clist))))
39 ;; (message "Changing %s of face `%s' to be %s" mode face color)))
41 ;;(color-selector-make-selector 'font-lock-function-name-face
42 ;; 'foreground 'cs-message " ")
44 ;;(color-selector-make-selector 'font-lock-function-name-face
45 ;; 'background 'cs-message " ")
54 (defvar color-selector-ok (make-glyph (concat slider-pixmap-dir
56 (defvar color-selector-reset (make-glyph (concat slider-pixmap-dir
58 (defvar color-selector-cancel (make-glyph (concat slider-pixmap-dir
60 (defvar color-selector-ok-down (make-glyph (concat slider-pixmap-dir
62 (defvar color-selector-reset-down (make-glyph (concat slider-pixmap-dir
63 "cs-reset-down.xpm")))
64 (defvar color-selector-cancel-down (make-glyph (concat slider-pixmap-dir
65 "cs-cancel-down.xpm")))
68 (defvar color-selector-button-keymap nil)
69 (if color-selector-button-keymap ()
70 (setq color-selector-button-keymap (make-keymap
71 'color-selector-button-keymap))
72 (suppress-keymap color-selector-button-keymap)
73 (define-key color-selector-button-keymap 'button1
74 'color-selector-button-activate))
76 (defun color-selector-button-activate (event)
78 (let* ((extent (event-glyph-extent event))
80 (action (extent-property extent 'button1-action))
82 ;; make the glyph look pressed
83 (cond ((setq down-glyph (extent-property extent 'button-down))
84 (setq up-glyph (extent-begin-glyph extent))
85 (set-extent-begin-glyph extent down-glyph)))
87 (setq event (next-event event))
88 (if (button-release-event-p event)
89 (setq mouse-down nil)))
90 ;; make the glyph look released
91 (if down-glyph (set-extent-begin-glyph extent up-glyph))
92 (if (eq extent (event-glyph-extent event))
94 (funcall action (extent-property extent 'button-selector))))))
99 (defun color-selector-repaint (ignore data)
100 (let* ((selector (car data))
101 (a (slider-get (extent-property selector 'slider-red)))
102 (b (slider-get (extent-property selector 'slider-green)))
103 (c (slider-get (extent-property selector 'slider-blue)))
104 (red-label (extent-property selector 'extent-red))
105 (green-label (extent-property selector 'extent-green))
106 (blue-label (extent-property selector 'extent-blue))
107 (red (make-glyph (format "%3s" a)))
108 (green (make-glyph (format "%3s" b)))
109 (blue (make-glyph (format "%3s" c)))
110 (face (extent-property selector 'mixed-face))
111 (face-to-set (extent-property selector 'selector-face))
112 (mode (extent-property selector 'selector-mode))
113 (callback (extent-property selector 'selector-callback))
114 face-r face-g face-b)
115 (set-face-background (setq face-r (glyph-face
116 (extent-begin-glyph red-label)))
117 (format "#%02x0000" a))
118 (set-face-background (setq face-g (glyph-face
119 (extent-begin-glyph green-label)))
120 (format "#00%02x00" b))
121 (set-face-background (setq face-b (glyph-face
122 (extent-begin-glyph blue-label)))
123 (format "#0000%02x" c))
124 (set-glyph-face red face-r)
125 (set-glyph-face green face-g)
126 (set-glyph-face blue face-b)
127 (set-extent-begin-glyph red-label red)
128 (set-extent-begin-glyph green-label green)
129 (set-extent-begin-glyph blue-label blue)
130 (set-face-background face (format "#%02x%02x%02x"
132 (cond ((eq mode 'foreground)
133 (set-face-foreground face-to-set (format "#%02x%02x%02x"
135 ((eq mode 'background)
136 (set-face-background face-to-set (format "#%02x%02x%02x"
138 (t (error "%s: None of `foreground' or `background'" mode)))
139 (if callback (funcall callback selector))))
143 (defun color-selector-make-selector (face mode &optional callback indent)
144 "Creates a color-selector object (and returns it) at the next line
145 in the current buffer which is supposed to change face FACE's
146 foreground or background color depending on the setting of MODE
147 \('foreground or 'background) An optional CALLBACK function can be
148 provided which will be called with the selector as an
149 argument. Optional INDENT is a string which will be inserted just
150 before the three sliders."
151 (let* ((color (map 'list '(lambda (x) (/ x 256))
152 (if (eq mode 'foreground)
153 (color-instance-rgb-components
154 (face-foreground-instance face))
155 (color-instance-rgb-components
156 (face-background-instance face)))))
157 (saved-face (make-face (gensym "color-selector-") nil t))
158 (red-face (make-face (gensym "color-selector-") nil t))
159 (blue-face (make-face (gensym "color-selector-") nil t))
160 (green-face (make-face (gensym "color-selector-") nil t))
161 (label-face (make-face (gensym "color-selector-") nil t))
162 s-r s-g s-b red-label green-label blue-label glyph selector
164 (copy-face face saved-face)
165 (set-face-foreground red-face "#ffffff")
166 (set-face-foreground green-face "#ffffff")
167 (set-face-foreground blue-face "#ffffff")
168 (set-face-background red-face (format "#%02x0000" (car color)))
169 (set-face-background green-face (format "#00%02x00" (cadr color)))
170 (set-face-background blue-face (format "#0000%02x" (caddr color)))
171 (make-face-bold red-face)
172 (make-face-bold green-face)
173 (make-face-bold blue-face)
174 (if (not (= (point) (point-at-bol)))
175 (insert-string "\n"))
176 (setq selector (make-extent (point) (point)))
177 (set-extent-property selector 'selector-is-selector t)
178 (set-extent-property selector 'end-open nil)
180 (if indent (insert-string indent))
181 (setq red-label (make-extent (point) (point)))
182 (set-extent-begin-glyph red-label
183 (setq glyph (make-glyph
184 (format "%3s" (car color)))))
185 (set-glyph-face glyph red-face)
186 (setq s-r (slider-new 100 0 255 20 'color-selector-repaint))
188 (set-extent-face (make-extent (- (point) 6) (point)) label-face)
190 (setq tmp (make-extent (point) (point)))
191 (set-extent-begin-glyph tmp color-selector-ok)
192 (set-extent-property tmp 'button-down color-selector-ok-down)
193 (set-extent-keymap tmp color-selector-button-keymap)
194 (set-extent-property tmp 'button1-action 'color-selector-destroy-selector)
195 (set-extent-property tmp 'button-selector selector)
199 (if indent (insert-string indent))
200 (setq green-label (make-extent (point) (point)))
201 (set-extent-begin-glyph green-label
202 (setq glyph (make-glyph
203 (format "%3s" (cadr color)))))
204 (set-glyph-face glyph green-face)
205 (setq s-g (slider-new 100 0 255 20 'color-selector-repaint))
207 (set-extent-face (make-extent (- (point) 6) (point)) label-face)
209 (setq tmp (make-extent (point) (point)))
210 (set-extent-begin-glyph tmp color-selector-reset)
211 (set-extent-property tmp 'button-down color-selector-reset-down)
212 (set-extent-keymap tmp color-selector-button-keymap)
213 (set-extent-property tmp 'button1-action 'color-selector-reset-selector)
214 (set-extent-property tmp 'button-selector selector)
218 (if indent (insert-string indent))
219 (setq blue-label (make-extent (point) (point)))
220 (set-extent-begin-glyph blue-label
221 (setq glyph (make-glyph
222 (format "%3s" (caddr color)))))
223 (set-glyph-face glyph blue-face)
224 (setq s-b (slider-new 100 0 255 20 'color-selector-repaint))
226 (set-extent-face (make-extent (- (point) 6) (point)) label-face)
228 (setq tmp (make-extent (point) (point)))
229 (set-extent-begin-glyph tmp color-selector-cancel)
230 (set-extent-property tmp 'button-down color-selector-cancel-down)
231 (set-extent-keymap tmp color-selector-button-keymap)
232 (set-extent-property tmp 'button1-action 'color-selector-cancel-selector)
233 (set-extent-property tmp 'button-selector selector)
235 (slider-set-data s-r (list selector))
236 (slider-set-data s-g (list selector))
237 (slider-set-data s-b (list selector))
239 (set-extent-property selector 'end-open t)
240 (set-extent-property selector 'start-open nil)
241 (set-extent-property selector 'read-only t)
242 (set-extent-property selector 'slider-red s-r)
243 (set-extent-property selector 'slider-green s-g)
244 (set-extent-property selector 'slider-blue s-b)
245 (set-extent-property selector 'extent-red red-label)
246 (set-extent-property selector 'extent-green green-label)
247 (set-extent-property selector 'extent-blue blue-label)
248 (set-extent-property selector 'mixed-face label-face)
249 (set-extent-property selector 'selector-face face)
250 (set-extent-property selector 'selector-saved-face saved-face)
251 (set-extent-property selector 'selector-mode mode)
252 (set-extent-property selector 'selector-callback callback)
253 (slider-set s-r (car color))
254 (slider-set s-g (cadr color))
255 (slider-set s-b (caddr color))
259 (defun color-selector-destroy-selector (selector)
260 "Destroys a color-selector SELECTOR"
261 (color-selector-check selector)
262 (set-extent-property selector 'read-only nil)
263 (delete-region (extent-start-position selector)
264 (extent-end-position selector)
265 (extent-object selector))
266 (delete-extent selector))
268 (defun color-selector-reset-selector (selector)
269 "Resets the color selector SELECTOR and it's attached face
270 to the original value."
271 (color-selector-check selector)
272 (let* ((face (extent-property selector 'selector-face))
273 (mode (extent-property selector 'selector-mode))
274 (saved (extent-property selector 'selector-saved-face)))
275 (cond ((eq mode 'foreground)
276 (set-face-foreground face (face-foreground saved))
277 (color-selector-adjust-selector
278 selector (map 'list '(lambda (x) (/ x 256))
279 (color-instance-rgb-components
280 (face-foreground-instance saved)))))
281 ((eq mode 'background)
282 (set-face-background face (face-background 'default))
283 (color-selector-adjust-selector
284 selector (map 'list '(lambda (x) (/ x 256))
285 (color-instance-rgb-components
286 (face-background-instance saved)))))
287 (t (error "%s: None of `foreground' or `background'" mode)))))
289 (defun color-selector-cancel-selector (selector)
290 "Resets and then destroys SELECTOR."
291 (color-selector-check selector)
292 (color-selector-reset-selector selector)
293 (color-selector-destroy-selector selector))
295 (defun color-selector-adjust-selector (selector color)
296 "Adjust color-selector SELECTOR to COLOR which is a list of three integers
297 in the range 0...255."
298 (color-selector-check selector)
299 (slider-set (extent-property selector 'slider-red) (car color))
300 (slider-set (extent-property selector 'slider-green) (cadr color))
301 (slider-set (extent-property selector 'slider-blue) (caddr color)))
303 (defun color-selector-get-color (selector)
304 "Return the current color the color-selector SELECTOR is showing
305 as a list of 3 integers in the range 0...255."
306 (color-selector-check selector)
307 (list (slider-get (extent-property selector 'slider-red))
308 (slider-get (extent-property selector 'slider-green))
309 (slider-get (extent-property selector 'slider-blue))))
311 (defun color-selector-get-mode (selector)
312 "Return the mode (background or foreground) color-selector SELECTOR is working in."
313 (extent-property selector 'selector-mode))
315 (defun color-selector-get-face (selector)
316 "Return the face color-selector SELECTOR is attached to."
317 (color-selector-check selector)
318 (extent-property selector 'selector-face))
321 (defun color-selector-p (obj)
322 "Checks whether OBJ is a color-selector object"
323 (and (extentp obj) (extent-property obj 'selector-is-selector)))
325 (defun color-selector-check (obj)
326 (or (color-selector-p obj)
327 (error "Object %s is no color-selector" obj)))
329 (provide 'color-selector)
331 ;;; color-selector.el ends here