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